Edit: I posted this without first seeing Astounding's solution. Our approaches are virtually identical. I thought this would be fun...this is what I came up with: data have;
length id year deathy deathm deathd 8;
infile datalines truncover;
input id year deathy deathm deathd;
datalines;
1 2010
1 2011
1 2012
1 2013
1 2014
1 2015 2015 3 17
2 2010
2 2011
2 2012
2 2013
2 2014
2 2015
2 2016
2 2017
3 2010
3 2011 2011 3 1
;
run;
data want;
length id year deathy deathm deathd start finish 8;
infile datalines truncover;
input id year deathy deathm deathd start finish;
datalines;
1 2010 . . . 1 365
1 2011 . . . 366 730
1 2012 . . . 731 1096
1 2013 . . . 1097 1462
1 2014 . . . 1463 1828
1 2015 2015 3 17 1829 1907
2 2010 . . . 1 365
2 2011 . . . 366 730
2 2012 . . . 731 1096
2 2013 . . . 1097 1462
2 2014 . . . 1463 1828
2 2015 . . . 1829 2194
2 2016 . . . 2195 2560
2 2017 . . . 2561 2926
3 2010 . . . 1 365
3 2011 2011 3 1 366 428
;
run;
data test;
set have;
by id year;
* derive date from year ;
date=mdy(1,1,year);
* derive number of days in the year ;
num_days=intck('day',intnx('year',date,0,'B'),intnx('year',date,1,'B'));
* if death, derive number of days to death ;
if nmiss(deathy,deathm,deathd)=0 then
num_days=intck('day',intnx('year',date,0,'B'),mdy(deathm,deathd,deathy));
* reset if first ID ;
if first.id then do;
start=1;
finish=num_days;
end;
else do;
start=finish+1;
finish+num_days; * automatically retained ;
end;
drop date;
* drop num_days;
run;
proc compare base=want comp=test;
run; However, it doesn't exactly match your desired output. Perhaps you can run with this and fix the code? Or perhaps your desired output is slightly off? Hope this helps... P.S.: In the future, can you post your problems with a self-contained data step containing datalines for your data? This will save us from having to cut-and-paste your data into a working data step.
... View more