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;
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: 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
@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.
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;
@Astounding Thank you so much for giving me another solution for my question! I really appreciate it.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.