选择SAS中的特定行

时间:2012-09-25 08:51:36

标签: sas datastep

我有来自单个病人的多次记录的以下数据。

ID                       date           day     result
BELF000000084785    20111110    1   164
BELF000000084785    20111202    2   172
BELF000016833470    20070122    2   226
BELF000016833470    20070522    3   398
BELF000016959442    20080429    1   242
BELF000016959442    20080820    1   126
BELF000016959442    20090225    2   302
BELF000016959442    20090424    2   320
BELF000017061714    20080626    1   152
BELF000017801424    20110803    1   298
BELF000017801424    20110913    2   252
BELF000017801424    20111022    3   234
BELF000024405407    20080218    1   232
BELF000024405407    20080318    2   190
BELF000024405410    20070122    3   380
BELF000024405410    20070218    1   506
BELF000024405410    20070320    2   480
BELF000024405410    20070626    3   346
BELF000024408114    20070423    1   296
BELF000024408114    20070711    3   294

我想在第1天提取患者的记录;如果第1天和第2天都可用,第1天第2天和第3天全部可用。并通过远离其他。任何sas帮助。

2 个答案:

答案 0 :(得分:0)

没有重复ID - 日级别你可以跳过最后一天:

proc sort data=sample;
by ID day;
run;

data result;
set sample;
by ID;
if not last.ID then output;
run;

你想用duplicite ID做什么 - 不同日期的日记录?

答案 1 :(得分:0)

我认为这正是OP所要求的。我不知道这是否真的是你想要的,但这需要进一步澄清。

data have;
informat date YYMMDD8.;
format ID $17.;
input ID $ date day result ;
datalines;
BELF000000084785    20111110    1   164
BELF000000084785    20111202    2   172
BELF000016833470    20070122    2   226
BELF000016833470    20070522    3   398
BELF000016959442    20080429    1   242
BELF000016959442    20080820    1   126
BELF000016959442    20090225    2   302
BELF000016959442    20090424    2   320
BELF000017061714    20080626    1   152
BELF000017801424    20110803    1   298
BELF000017801424    20110913    2   252
BELF000017801424    20111022    3   234
BELF000024405407    20080218    1   232
BELF000024405407    20080318    2   190
BELF000024405410    20070122    3   380
BELF000024405410    20070218    1   506
BELF000024405410    20070320    2   480
BELF000024405410    20070626    3   346
BELF000024408114    20070423    1   296
BELF000024408114    20070711    3   294
;;;;
run;

data want;
do _n_ = 1 by 1 until (last.ID);
 set have;
 by ID notsorted;
 if first.ID then do; 
    lastday=0; 
    done=0; 
 end;
 if (first.ID) and (day ne 1) then lastday=-99;
 else if day ne lastday+1 then done=1;
 else if not done then lastday=day;
end;
do _n_ = 1 by 1 until (last.ID);
 set have;
 by ID notsorted;
 if first.ID then curday=0;
 curday+1;
 if curday le lastday then output;
end;
run;

结果:

Obs      ID        date day result 
1 BELF000000084785  18941 1 164 
2 BELF000000084785  18963 2 172 
3 BELF000016959442  17651 1 242 
4 BELF000017061714  17709 1 152 
5 BELF000017801424  18842 1 298 
6 BELF000017801424  18883 2 252 
7 BELF000017801424  18922 3 234 
8 BELF000024405407  17580 1 232 
9 BELF000024405407  17609 2 190 
10 BELF000024408114  17279 1 296