data have;
informat start end mmddyy10.;
format start end date9.;
input id drug start end;
cards;
1 a 1/1/2005 2/17/2005
1 a 2/7/2005 3/3/2005
1 a 3/7/2005 5/5/2005
1 a 5/30/2005 8/9/2005
;run;
I want to create a continuous period allowing only 7 days gap between the end date of the period and the start of the next one
output
id start end
1 1/1/2005 5/15/2005
Thank you for the response. I adjusted the dates. I want to allow a gap of 7 days but if there is overlap in the dates such as in the first and the second row (there is an overlap of 10 days) I want to add the 10 days to the end date as refected in the output.
You didn't show multiple ID's, but I presume the dataset is sorted by ID/START, so this program checks for a change in ID:
data have;
informat start end mmddyy10.;
format start end date9.;
input id drug :$1. start end;
cards;
1 a 1/1/2005 2/17/2005
1 a 2/7/2005 3/3/2005
1 a 3/7/2005 5/5/2005
1 a 5/30/2005 8/9/2005
;run;
data want (drop=_:);
set have (keep=id);
by id;
merge have
have (firstobs=2 keep=start rename=(start=_nxtstart));
retain _start;
if first.id or start > lag(end)+7 then _start=start;
if last.id or _nxtstart>end+7;
start=_start;
run;
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.