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;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 580 views
  • 0 likes
  • 3 in conversation