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;
prior_end = lag(endda);
if first.name=0 and (begda > prior_end + 1) then do;
endda = begda - 1;
begda = prior_end + 1;
proc sort data=want;
by name begda;
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;
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.