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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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