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;
I want to test for first.id and last.id, but I couldn't do MERGE HAVE HAVE (firstobs=2 keep=start rename=(start=_nxtstart)); BY ID; because the 2nd ID group and all subsequent ID groups, would not have _NXTSTART one observation after the current obs. So the solution is to
Use SET HAVE (keep=id); BY ID; which sets up the dummy first.id and last.id as needed, and
MERGE HAVE HAVE (firstobs=2 keep=start rename=(start=_nxtstart); WITHOUT a following BY ID statement. This keeps _nxtstart one obs ahead of start, even when changing ID groups.
... View more