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

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 1981 views
  • 0 likes
  • 3 in conversation