BookmarkSubscribeRSS Feed
MikeZdeb
Rhodochrosite | Level 12

Sure, added DAY to the data set  ...

 

data z (drop=first s: diff);
array s(0:30) s0-s30;
do until(last.id);
  set x;
  by id;
  if first.id then first = date;
  s(date - first) = date;
end;
diff = coalesce(of s30-s0)-s0;
do after=0 to ifn(diff gt 14, diff, 14);
  date = s(after);
  day = first+after;
  output;
end;
format day date9.;
run;

 

MikeZdeb
Rhodochrosite | Level 12

Hi, just one more tweak ... if you want to make sure that you always get the full duration of all the IDs in your data set you could use a macro variable that contains the maximum difference between first and last days within an ID in your data ...

 

 

proc sql noprint;
select max(diff) into :max trimmed from
(select max(date)-min(date) as diff from x group by id);
quit;

 

data z (drop=first s: diff);
array s(0:&max) s0-s&max;
do until(last.id);
  set x;
  by id;
  if first.id then first = date;
  s(date - first) = date;
end;
diff = coalesce(of s&max-s0)-s0;
do after=0 to ifn(diff gt 14, diff, 14);
  date = s(after);
  day = first+after;
  output;
end;
format day date9.;
run;

grace999
Obsidian | Level 7

Thanks to all of your guys.  @art297 @MikeZdeb Both of your code are solutions!

art297
Opal | Level 21

@grace999: The code first reads all of the records for a given patient and puts each surgery date in the array surgeries.

 

It then uses that array to accomplish the desired output. After all of a given patient's records have been read, the code sets DAYS to 14 or greater if the last surgery date is more than 14 days after the first surgery date:

 if last.patient_id then do;
    days=14;
    if i gt 1 and surgeries(i)-surgeries(1) gt  14 then
      days=surgeries(i)-surgeries(1);
    do After_surgery_days=0 to days;
      day=surgeries(1)+After_surgery_days;
      if day in surgeries then Surgery_date=day;
      else call missing(Surgery_date);
      output;
    end;
  end;

While it goes through the loop to output the desired records, its sets day to equal the date of the record being output.

It then checks to see if that date was a date when a surgery occured. If it did, it sets Surger_date to that date.

 

Art, CEO, AnalystFinder.com

grace999
Obsidian | Level 7

@art297 Thank you so much for explaining your code! I only used array for variables, not for records as you did for surgeries! It's good to know.

Astounding
PROC Star

With apologies for not testing this sooner, here are a bunch of corrections.  This should work now:

 

data want;

   set have;

   by patient_ID;

   if first.patient_ID then after_surgery_days=0;

   else after_surgery_days + 1;

   output;

   if last.patient_ID then do after_surgery_days = after_surgery_days + 1 to 14;

      surgery_date = .;

      output;

   end;

   else do;

      next_record = _n_ + 1;

       set have (keep=surgery_date rename=(surgery_date = next_date)) point=next_record ;

      gap = next_date - surgery_date;

   if gap > 1 then do gap = 2 to gap;

         after_surgery_days + 1;

         surgery_date = .;

         output;

      end;

   end;

   drop next_date next_record gap;

run;

 

grace999
Obsidian | Level 7

@Astounding Thank you so much for giving me another solution for my question! I really appreciate it.

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!

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
  • 21 replies
  • 1692 views
  • 5 likes
  • 4 in conversation