BookmarkSubscribeRSS Feed
Davoz7
Calcite | Level 5
Hey
I’m a bit struggeling with this one. I have a list in sas that shows periods of illness. It is sorted by name and startdate (BEGDA)

NAME BEGDA ENDDA ILL
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
.... .... ....
I need to find a way to add a line per individual with the missing period(=the period between two illnesses). For Jones this should be the output:

NAME BEGDA ENDDA ILL
Jones 2/11/2015 2/16/2015 Y
Jones 2/17/2015 3/6/2015 N
Jones 3/7/2015 3/12/2015 Y

Is there a way to code this into a program or something like that. I’m new in sas so any help would be welcome.
Thanks!
3 REPLIES 3
Astounding
PROC Star

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;

Kurt_Bremser
Super User

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;
novinosrin
Tourmaline | Level 20
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;

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

What is Bayesian Analysis?

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1321 views
  • 0 likes
  • 4 in conversation