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