data a;
input patientid start : mmddyy10. end : mmddyy10. ;
format start end mmddyy10.;
datalines;
1 1/1/2006 4/1/2006
2 1/2/2006 12/2/2007;
run;
I have data that contains one row per patient with start and end date. I want to create 30 days interval following the start date until the end date. The output would be :
1 1/1/2006 31/12006
1 31/1/2006 3/2/2006
1 3/2/2006 4/1/2006
2 1/2/2006 2/1/2006
and so on
data a;
input patientid start : mmddyy10. end : mmddyy10. ;
format start end date9.;
datalines;
1 1/1/2006 4/1/2006
2 1/2/2006 12/2/2007
;
run;
data want;
set a(rename=end=_end);
do while(start<_end);
end=intnx('days',start,30);
output;
start=end;
end;
format start end mmddyy10.;
drop _:;
run;
data a;
input patientid start : mmddyy10. end : mmddyy10. ;
format start end date9.;
datalines;
1 1/1/2006 4/1/2006
2 1/2/2006 12/2/2007
;
run;
data want;
set a(rename=end=_end);
do while(start<_end);
end=intnx('days',start,30);
output;
start=end;
end;
format start end mmddyy10.;
drop _:;
run;
Thank you!
Here's one way to approach the problem. It's untested code, so you will have to verify whether the results are correct:
data want;
set have;
original_end = end;
do while (end - start > 30);
end = start + 30;
output;
start = end ;
end = original_end;
end;
output;
drop original_end;
run;
Notice how it's a little harder to read because END is both a SAS statement and a variable name. If it's within your control, try to avoid that situation.
Please be consistent with your date formatting. I suggest to use the yymmddd10. format, as that is ISO-compliant.
As it is, some of your dates in then output seem to be DMY order, while others are MDY.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.