BookmarkSubscribeRSS Feed
SASd15
Calcite | Level 5

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

 

 

3 REPLIES 3
mkeintz
PROC Star

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;
--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
SASd15
Calcite | Level 5

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

 

art297
Opal | Level 21
data want;
  set have;
  by id state;
  days=ifn(first.state,0,dif(date));
run;

Art, CEO, AnalystFinder.com

 

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!

How to Concatenate Values

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.

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
  • 1342 views
  • 0 likes
  • 3 in conversation