For this (or any) approach to work, your BEGDA values must be actual SAS dates, not character strings. So you will need to learn how SAS handles dates, if you don't already know.
Under that assumption, here's a way:
proc sort data=have;
by name begda;
run;
data want;
set have;
by name;
prior_end = lag(endda);
output;
if first.name=0 and (begda > prior_end + 1) then do;
endda = begda - 1;
begda = prior_end + 1;
ill='N';
output;
end;
drop prior_end;
run;
Then optionally:
proc sort data=want;
by name begda;
run;
Use the lag() function to access previous records.
proc sort data=want;
by name;
run;
data want;
set have;
by name;
prevend = lag(endda);
output;
if not first.name and prevend < begda
then do;
ill = 'N';
endda = begda - 1;
begda = prevend;
output;
end;
drop prevend;
run;
proc sort data=want;
by name begda;
run;
data have;
input NAME $ (BEGDA ENDDA) (:mmddyy10.) ILL $;
format BEGDA ENDDA mmddyy10.;
datalines;
Jones 2/11/2015 2/16/2015 Y
Jones 3/7/2015 3/12/2015 Y
Fields 11/5/2017 12/23/2017 Y
Fields 1/1/2018 1/14/2018 Y
Smith 3/12/2014 5/12/2014 Y
Smith 5/15/2014 5/17/2014 Y
Smith 7/20/2016 7/27/2016 Y
;
data want;
set have;
by name notsorted;
retain _date;
if first.name then do; _date=ENDDA;output;end;
else if _date ne BEGDA-1 then do;
_BEGDA=BEGDA;_ENDDA=ENDDA;_ll=ill;
BEGDA=_date+1; ENDDA=_BEGDA-1;ill='N';
output;
BEGDA=_BEGDA;
ENDDA=_ENDDA;
ill=_ll;
output;
_date=ENDDA;
end;
drop _:;
run;
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.