BookmarkSubscribeRSS Feed
lmyers2
Obsidian | Level 7

Hello

 

I'm trying to keep a row that has a date closest to but after a test result date, Below is sample data. Is it possible to do this while the data is in long form?

 

Have:

Patient      ComorbidityScore  Test Result

Patient 1   Date 1                      Date X

                 Date 2                       Date X

                  Date 3                      Date X

Patient 2    Date 1                     Date Y

                  Date 2                     Date Y

Want:

Patient      ComorbidityScore   Test Result

Patient 1   Date 3                      Date X                                   

Patient 2    Date 1                      Date Y

2 REPLIES 2
Shmuel
Garnet | Level 18

You are looking for:

  where ComorbidityScore > test_result and min(ComorbidityScore - test_result) 

  group by patient

to be used within sql.

Kurt_Bremser
Super User

Sort by patient and the date, then do

data want;
set have;
by patient;
retain flag;
if first.patient then flag = 1;
if flag and comorbidityscore > testresult
then do;
  output;
  flag = 0;
end;
drop flag;
run;