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

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