DATA Step, Macro, Functions and more

Calculate Duration Between Events

Accepted Solution Solved
Reply
Contributor
Posts: 55
Accepted Solution

Calculate Duration Between Events

Hi Everyone, 

 

I have data that follows cases over time. What I would like to do is:

 

1. Calculate the number of days between each session.

2. Calculate The number of datys in the program, so the difference between the first and last session.

 

I'm not sure what the best way to approach this is, so any ideas / suggestions would be greatly appreciated.

 

Thanks in advance! 

 

ChildIDPreAdmHisActionDate_HWFirstSessionLastSessionSHSession
329-Jun-0901Most Recent Session
322-Dec-08002nd Most Recent
38-Sep-08003rd Most Recent
38-Jul-08004th Most Recent
311-Apr-08005th Most Recent
319-Feb-08006rd Most Recent
324-Jan-08107th Most Recent
410-Dec-1001Most Recent Session
423-Sep-10002nd Most Recent
49-Mar-10003rd Most Recent
41-Jul-09004th Most Recent
422-Dec-08005th Most Recent
47-Oct-08006rd Most Recent
49-Sep-08007th Most Recent
48-Jul-08008th Most Recent
410-Apr-08009rd Most Recent
425-Jan-080010th Most Recent
424-Jan-081011th Most Recent
522-Mar-1001Most Recent Session
522-Dec-09002nd Most Recent
520-Oct-09003rd Most Recent
516-Jun-09004th Most Recent
522-Dec-08005th Most Recent
53-Sep-08006rd Most Recent
517-Jun-08007th Most Recent
518-Mar-08008th Most Recent
527-Feb-08009rd Most Recent
527-Feb-08109rd Most Recent
914-Jul-0901Most Recent Session
919-Sep-08002nd Most Recent
98-Jul-08003rd Most Recent
98-Apr-08004th Most Recent
912-Feb-08105th Most Recent
1017-Jul-0901Most Recent Session
1022-Dec-08002nd Most Recent
107-Oct-08003rd Most Recent
1014-Jul-08004th Most Recent
1017-Apr-08005th Most Recent
1015-Feb-08106rd Most Recent
1130-Jun-0901Most Recent Session
1122-Dec-08002nd Most Recent
112-Oct-08003rd Most Recent
112-Jul-08004th Most Recent
1117-Mar-08005th Most Recent
1111-Feb-08106rd Most Recent

Accepted Solutions
Solution
‎11-09-2016 01:21 PM
Super User
Posts: 5,516

Re: Calculate Duration Between Events

One necessary condition is that your dates should be SAS dates, not character strings.  That may already be taken care of, but I just wanted to point it out.

 

It's a much easier problem to handle if  you reverse the order:

 

proc sort data=have;

by ChildId PreAdmHisActionDate_HW;

run;

 

Then the calculations are straightforward:

 

data want;

set have;

by ChildID;

session_diff = dif(PreAdmHisActionDate_HW);

if first.ChildID then do;

   total_days=0;

   session_diff=.;

end;

total_days + session_diff;

run;

 

The variable SESSION_DIFF is one of the variables you were asking for.  The variable TOTAL_DAYS is a cumulative version of SESSION_DIFF for that ID.  So the final value (when LastSession=1) would be the other variable you asking for.

View solution in original post


All Replies
Solution
‎11-09-2016 01:21 PM
Super User
Posts: 5,516

Re: Calculate Duration Between Events

One necessary condition is that your dates should be SAS dates, not character strings.  That may already be taken care of, but I just wanted to point it out.

 

It's a much easier problem to handle if  you reverse the order:

 

proc sort data=have;

by ChildId PreAdmHisActionDate_HW;

run;

 

Then the calculations are straightforward:

 

data want;

set have;

by ChildID;

session_diff = dif(PreAdmHisActionDate_HW);

if first.ChildID then do;

   total_days=0;

   session_diff=.;

end;

total_days + session_diff;

run;

 

The variable SESSION_DIFF is one of the variables you were asking for.  The variable TOTAL_DAYS is a cumulative version of SESSION_DIFF for that ID.  So the final value (when LastSession=1) would be the other variable you asking for.

Contributor
Posts: 55

Re: Calculate Duration Between Events

Posted in reply to Astounding

Astounding - That worked perfectly, thanks so much! When you get a moment can you explain to me how the Diff function knew to take the next row and subtract or is that what the diff function just does? subtracts row by row? 

Super User
Posts: 5,516

Re: Calculate Duration Between Events

That's what DIF is built to do ... subtract the previous value from the current value.

 

There are tricky aspects that almost always require that the DIF function executes on every observation.  That's why the DIF value gets calculated but then re-set to missing on the earliest date for a ChildID.

Contributor
Posts: 55

Re: Calculate Duration Between Events

Posted in reply to Astounding

Got it until that last part. What happens if there is missing data?

Super User
Posts: 5,516

Re: Calculate Duration Between Events

The problem is a little different.  By executing on every observation, sometimes DIF calculates the difference between the first date for a ChildID and the last date for the previous ChildID.  Logically, those values should not be included in your calculations.  That's why the program sets them to missing.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 5 replies
  • 259 views
  • 0 likes
  • 2 in conversation