Help using Base SAS procedures

Add line with missing period - SAS EG

Reply
Occasional Contributor
Posts: 5

Add line with missing period - SAS EG

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!
Super User
Posts: 6,751

Re: Add line with missing period - SAS EG

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;

Super User
Posts: 10,209

Re: Add line with missing period - SAS EG

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
PROC Star
Posts: 1,769

Re: Add line with missing period - SAS EG

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;
Ask a Question
Discussion stats
  • 3 replies
  • 203 views
  • 0 likes
  • 4 in conversation