Hi I have the following dataset. What I would like to do is FIRST instance and LAST INSTANCE per patient, I would like the duration to be the same. But for the middle, i would like to set up duration = 24.
patientID | time | first_date_time | stop_date_time | duration | weight |
111 | 1 | 1/2/2020 1:20 | 1/2/2020 23:59 | 22 | 24 |
111 | 2 | 1/3/2020 0:00 | 1/3/2020 21:23 | 21 | 24 |
111 | 3 | 1/4/2020 2:00 | 1/4/2020 18:18 | 16 | 24 |
222 | 1 | 2/1/2020 9:20 | 2/1/2020 23:59 | 14 | 19 |
222 | 2 | 2/2/2020 0:00 | 2/2/2020 19:12 | 19 | 19 |
222 | 3 | 2/3/2020 3:00 | 2/3/2020 20:23 | 17 | 19 |
222 | 3 | 2/4/2020 1:00 | 2/4/2020 23:23 | 22 | 19 |
This:
patientID | time | first_date_time | stop_date_time | duration | weight | duration_final |
111 | 1 | 1/2/2020 1:20 | 1/2/2020 23:59 | 22 | 24 | 22 |
111 | 2 | 1/3/2020 0:00 | 1/3/2020 21:23 | 21 | 24 | 24 |
111 | 3 | 1/4/2020 2:00 | 1/4/2020 18:18 | 16 | 24 | 24 |
222 | 1 | 2/1/2020 9:20 | 2/1/2020 23:59 | 14 | 19 | 14 |
222 | 2 | 2/2/2020 0:00 | 2/2/2020 19:12 | 19 | 19 | 24 |
222 | 3 | 2/3/2020 3:00 | 2/3/2020 20:23 | 17 | 19 | 24 |
222 | 3 | 2/4/2020 1:00 | 2/4/2020 23:23 | 22 | 19 | 22 |
It didnt work:
data want; set have;
by PatientID time;
if first. time then do; duration_final = duration end;
else if last. time then do; duration_final = duration end;
else do; duration_final = 24 end;
run;
I would do the first. and last. indicators on patient ID instead of time.
data want; set have; by patientID time; if first.patientid or last.patientid then duration_final=duration; else duration_final=24; run;
Also I would only do; and end; if I am performing multiple "then"s, in your case because you are only doing one "then" scenario, I don't see a need for it. KISS 🙂
Hello,
It doesn't work because you forgot to put semicolons following the instructions (before end) and it must change
first.patient_id /last.patient_id instead of first.time / last.time
P/s: the loop "do...end" is not necessary if there is only one instruction. i think "by time" is not necessary too.
Have a nice day.
Why is duration_final = 24 for the last observation with patientID = 111? This does not match the description.
The log should contain syntax-errors for the lines 3-5 of your data-step. @kelxxx already explained the reason: a semicolon is required before "end;".
Keeping do...end-blocks is highly recommended, properly formatted those blocks increase readability and help avoiding common mistakes, when adding another statement that should be executed conditionally.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.