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;
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!
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.