BookmarkSubscribeRSS Feed
monday89
Fluorite | Level 6

Hello. I have the following dataset per subject ID where i need to calculate difference in hours based on each day. Logic:

  • For day = 0, where i need to take the MAX DRUG_stop_DTM - Enrolled_DTM.
  • Next days (EXCEPT THE LAST DAY for that subjectID), the difference in hours needs to be static set to 24 hours. 
  • Last day: final_stop_DTM - first drug_start_dtm for that last day. If the "Final_stop_dtm" is missing then use max drug_stop_dtm for that day 

 

Have:

PatientID Enrolled_DTM days Drug_start_DTM Drug_stop_STM WEIGHT final_stop_DTM
1232 26MAR2020:13:30:00 0 26MAR20:13:30:00 26MAR20:23:59:59 74.2 28MAR2020:21:56:00
1232 26MAR2020:13:30:00 1 27MAR20:00:00:00 27MAR20:23:59:59 74.2 28MAR2020:21:56:00
1232 26MAR2020:13:30:00 1 27MAR20:03:46:00   74.2 28MAR2020:21:56:00
1232 26MAR2020:13:30:00 1 27MAR20:20:04:00   74.2 28MAR2020:21:56:00
1232 26MAR2020:13:30:00 2 28MAR20:00:00:00 28MAR20:13:00:00 74.2 28MAR2020:21:56:00
1232 26MAR2020:13:30:00 2 28MAR20:04:11:00   74.2 28MAR2020:21:56:00
1232 26MAR2020:13:30:00 2 28MAR20:04:46:00   74.2 28MAR2020:21:56:00
4456 02JAN2020:16:11:00 0 02JAN20:18:16:00 02JAN20:23:59:59 80.9 .
4456 02JAN2020:16:11:00 1 03JAN20:00:00:00 03JAN20:15:50:00 80.9 .
4456 02JAN2020:16:11:00 1 03JAN20:15:50:00 03JAN20:23:59:59 80.9 .
4456 02JAN2020:16:11:00 2 04JAN20:00:00:00 04JAN20:21:11:00 80.9 .

 

want;

PatientID days WEIGHT hours_difference
1232 0 74.2 10.5
1232 1 74.2 24
1232 2 74.2 22
4456 0 80.9 7.82
4456 1 80.9 24
4456 2 80.9 21.18

 

What I have done so far is break the dataset by days. for day = 0 but then i wasn't sure how to do the "last" by group per patient

 

any help is appreciated. 

 

1 REPLY 1
LeonidBatkhan
Lapis Lazuli | Level 10

Hi monday89,

Here is how you can set up you logic:

proc sort data=A out=B; 
   by PatientID Days;
run;

data C;
   set B;
   by PatientID Days;
   if first.Days then
   do;
      /* do your first observation (Days=0) processing */
   end;
   else
   if last.Days then
   do;
      /* do your last day processing */
   end;
   else
   do;
      /* do your processing for other days except last */
   end;
run;

Hope this helps.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 744 views
  • 0 likes
  • 2 in conversation