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

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
1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

5 REPLIES 5
Astounding
PROC Star

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.

hwangnyc
Quartz | Level 8

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? 

Astounding
PROC Star

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.

hwangnyc
Quartz | Level 8

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

Astounding
PROC Star

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.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 5 replies
  • 1123 views
  • 0 likes
  • 2 in conversation