I would like to calculate difference between the dates in the following data set:
Patient # visit date
123 12-02-16
123 12-05-16
123 12-10-16
456 01-02-16
456 01-07-16
456 01-14-16
To create result:
Patient # # of days
123 0
123 3
123 5
456 0
456 5
456 7
Use the DIF function, which is effectively x=lag(x). Use it inside an IFN function, to substiitute a zero when at first.id:
data have;
input id date :mmddyy10.; format date date9.;
datalines;
123 12-02-2016
123 12-05-2016
123 12-10-2016
456 01-02-2016
456 01-07-2016
456 01-14-2016
run;
data want;
set have;
by id;
days=ifn(first.id,0,dif(date));
run;
Thank you for your response
If i had 2 fields, how would i change the data step?
INPUT:
ID State Date
123 MD 1-1-2016
123 MD 1-3-2016
123 TX 2-4-2016
123 TX 2-7-2016
Need Output
ID State Days
123 MD 0
123 MD 2
123 TX 0
123 TX 3
data want; set have; by id state; days=ifn(first.state,0,dif(date)); run;
Art, CEO, AnalystFinder.com
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.