BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
huhuhu
Obsidian | Level 7

Hello,

 

I'm wondering if there is a way to calculate the difference between two dates in different variables listed in different rows. My data looks like below:

MemberID   InDate     OutDate

1                  1/1/2019   1/3/2019

1                  1/4/2019   1/7/2019

2                  1/15/2019   1/16/2019

2                  1/20/2019    1/25/2019

 

Result that I'd like to have is

MemberID   InDate     OutDate            IntervalDays

1                  1/1/2019   1/3/2019           .

1                  1/4/2019   1/7/2019             1 (difference between 1/4/2019 and 1/3/2019)

2                  1/15/2019   1/16/2019          .

2                  1/20/2019    1/25/2019         4(difference between 1/20/2019 and 1/16/2019)

 

Any advice will be helpful!!

Thank you!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

Please try

 

data have;
input MemberID   InDate:mmddyy10.     OutDate:mmddyy10.;
format InDate OutDate date9.;
cards;
1 1/1/2019   1/3/2019
1 1/4/2019   1/7/2019
2 1/15/2019  1/16/2019
2 1/20/2019  1/25/2019
;

proc sort data=have;
by memberid indate outdate;
run;

data want;
set have;
by memberid indate outdate;
intervaldays=InDate-lag(OutDate);
if first.memberid then intervaldays=.;
run;
Thanks,
Jag

View solution in original post

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

Please try

 

data have;
input MemberID   InDate:mmddyy10.     OutDate:mmddyy10.;
format InDate OutDate date9.;
cards;
1 1/1/2019   1/3/2019
1 1/4/2019   1/7/2019
2 1/15/2019  1/16/2019
2 1/20/2019  1/25/2019
;

proc sort data=have;
by memberid indate outdate;
run;

data want;
set have;
by memberid indate outdate;
intervaldays=InDate-lag(OutDate);
if first.memberid then intervaldays=.;
run;
Thanks,
Jag

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 681 views
  • 3 likes
  • 3 in conversation