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

Hi,

I am trying to compute the difference in days between two dates that are located in two different rows and in different columns by id. For example,

 

id     date1               date2               diff

1      03/01/1991     08/31/1992      09/01/1992 - 08/31/1992    (or 08/31/1992 - 09/01/1992)

1      09/01/1992     07/31/1992      08/01/1996 - 07/31/1992

1      08/01/1996     06/30/1997      07/01/1997 - 06/30/1997

1      07/01/1997     07/31/1997

2     09/01/1993     01/15/1994     

3     01/01/1994     01/01/1995       01/01/1995 - 01/01/1995

3     01/01/1995     05/02/1999    

 

I will appreciate your help. Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

It's not necessarily simple, but it doesn't take a lot of code.  Assuming your dates are SAS dates, not character strings:

 

data want;

set have;

by id;

set have (firstobs=2 keep=date1 rename=(date1=next_date)) have (drop=_all_);

if last.id=0 then diff = next_date - date2;

drop next_date;

run;

View solution in original post

1 REPLY 1
Astounding
PROC Star

It's not necessarily simple, but it doesn't take a lot of code.  Assuming your dates are SAS dates, not character strings:

 

data want;

set have;

by id;

set have (firstobs=2 keep=date1 rename=(date1=next_date)) have (drop=_all_);

if last.id=0 then diff = next_date - date2;

drop next_date;

run;

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!

Discussion stats
  • 1 reply
  • 750 views
  • 1 like
  • 2 in conversation