Hello. I have the following dataset per subject ID where i need to calculate difference in hours based on each day. Logic:
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.
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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
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!
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.
Ready to level-up your skills? Choose your own adventure.