添加基于日期比较的新索引列

问题描述 投票:0回答:2

假设有以下两个数据集:

第一个包含每个 ID 的完整日期列表和指示患者是否患有合并症 (1) 或不患有 (0) 的索引。然后,还有另一个数据集,其中包含第一个数据集的日期子集和 RefDate(1 或 0)。我想做的是以下操作:

对于每个 ID:

如果 DB1 中的合并症 = 1(!) 在 Start_Therapy 之前或 = 之前,其中 DB2 RefDate = 1,则在 DB1 中添加等于 1 的索引,否则无论 DB1 中的合并症是否 = 1,索引都应为 0。 如果 DB1 中的合并症 = 0,则应保持为 0。 预先感谢您!

所需输出 = DB3

   data DB1;
     input ID Start_Therapy Comorbidity;
     format Start_Therapy date9.;
   cards;
   0001 01OCT2015  1
   0001 06DEC2016  1
   0001 08NOV2020  1
   0002 11JAN2014  0
   0002 16JUN2014  0
   0002 14MAY2015  1
   0002 30JUN2015  1
   0002 25FEB2016  0
   0003 11SEP2012  0
   0003 24AUG2014  1
   0003 10DEC2014  1
   0004 03JAN2014  0
   0004 09FEB2014  1
   0004 03AUG2015  1
   0004 18MAY2016  0
   ;
   
   data DB2;
     input ID Start_Therapy  RefDate;
     format Start_Therapy date9.;
   cards;
   0001 06DEC2016  1
   0001 08NOV2020  0
   0002 16JUN2014  1
   0002 30JUN2015  0
   0003 24AUG2014  1
   0003 10DEC2014  0
   0004 03AUG2015  1
   ;
   
   data DB3;
     input ID  Start_Therapy  Comorbidity Index;
     format Start_Therapy date9.;
   cards;
   0001 01OCT2015  1  1
   0001 06DEC2016  1  1
   0001 08NOV2020  1  0
   0002 11JAN2014  0  1
   0002 16JUN2014  0  1
   0002 14MAY2015  1  0
   0002 30JUN2015  1  0
   0002 25FEB2016  0  0
   0003 11SEP2012  0  0
   0003 24AUG2014  1  1
   0003 10DEC2014  1  0
   0004 03JAN2014  0  0
   0004 09FEB2014  1  1
   0004 03AUG2015  1  1
   0004 18MAY2016  0  0
   ;
sas
2个回答
0
投票

你的一些例子对于 ID=2 来说似乎有缺陷。日期 11JAN2014 和 16JUN2014 的 como 0,因此结果中的 INDEX 不会为 0。

使用存在子查询来标记您的条件。

示例:

proc sql;
  create table want as
  select *, 
  case 
    when comorbidity = 0 then 0
    else exists ( 
      select * from DB2 
      where refdate=1
        and DB2.ID = DB1.ID
        and DB2.start_therapy >= DB1.start_therapy
    )
  end as index
  from DB1
  ;

0
投票
 proc sql;
     create table db3 as
     select db1.*, coalesce(b.refdate, 0) as Index
     from db1 left join 
(select * from db2 where refdate = 1) b on (db1.id = b.id and db1.start_therapy <= b.start_therapy)
     order by id, start_therapy
     ;
quit;
Obs    ID    Start_Therapy    Comorbidity    Index                       
                                                                        
 1     1      01OCT2015           1           1                         
 2     1      06DEC2016           1           1                         
 3     1      08NOV2020           1           0                         
 4     2      11JAN2014           0           1                         
 5     2      16JUN2014           0           1                         
 6     2      14MAY2015           1           0                         
 7     2      30JUN2015           1           0                         
 8     2      25FEB2016           0           0                         
 9     3      11SEP2012           0           1                         
10     3      24AUG2014           1           1                         
11     3      10DEC2014           1           0                         
12     4      03JAN2014           0           1                         
13     4      09FEB2014           1           1                         
14     4      03AUG2015           1           1                         
15     4      18MAY2016           0           0 
© www.soinside.com 2019 - 2024. All rights reserved.