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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!

Register now

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
  • 984 views
  • 0 likes
  • 3 in conversation