BookmarkSubscribeRSS Feed
monday89
Fluorite | Level 6

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;

3 REPLIES 3
sustagens
Pyrite | Level 9

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 🙂

kelxxx
Quartz | Level 8

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.

andreas_lds
Jade | Level 19

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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 609 views
  • 1 like
  • 4 in conversation