BookmarkSubscribeRSS Feed
lmyers2
Obsidian | Level 7

How would you use the data step to find a specific value after a certain criteria is met. I have a long file that has timestamps that is sorted (sample below). I'm looking to find how many patients get "CPAP" any time after a row of HF. The answer here would be patient 1 and 2 so 2 of the 3 patients.

 

Patient     Tier     oxygen         Timestamp

1               1            NC                 Time

1               3            HF                  Time

1               3            CPAP             Time

2               3            HF                  Time

2               3            CPAP             Time

3               1            NC                  Time

5 REPLIES 5
PeterClemmensen
Tourmaline | Level 20

So what does your desired result look like? Do you want all observations for the ID's that satisfy this?

PeterClemmensen
Tourmaline | Level 20

If so then do

 

data have;
input Patient Tier oxygen $ Timestamp $;
datalines;
1 1 NC   Time
1 3 HF   Time
1 3 CPAP Time
2 3 HF   Time
2 3 CPAP Time
3 1 NC   Time
;

data want(drop = f:) ;
   do until(last.Patient);
      set have;
      by Patient;
      if oxygen = 'HF' then f1 = 1;
      if oxygen = 'CPAP' and f1 then f2 = 1; 
      output;
   end;

   do until (last.Patient);
      set have;
      by Patient;
      if f2 then output;
   end;
run;
lmyers2
Obsidian | Level 7
Thanks - does this take into account the order? Meaning the do loop will find HF then will continue looking for cpap lower down in a long file that is already sorted by time?

I think this code almost works but I get an error-73-322 “expecting an =“ on last.patient that doesn’t make sense...
Kurt_Bremser
Super User
It will process the dataset in the order it is stored. I found a mistake in my code I will correct.
In you have problems, please post the complete log into the appropriate text box.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 5 replies
  • 417 views
  • 0 likes
  • 3 in conversation