Getting residual time of date differences in a different unit

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

Getting residual time of date differences in a different unit

Hi SAS Community,

 

 

I am trying to get a difference in between two dates, a start date and an end date, in weeks and the residual days. Is there a sas formula for this that wont require me to do a lot of work-around? I'm finding a lot of resources for computing date difference variables in one unit or one unit plus time, but not two different units (weeks + days).

 

For example:

Start date: 03-Aug-2006

End date: 11-Oct-2006

Calculated result I want: 9 weeks, 6 days.

 

 

Thanks!

Cara


Accepted Solutions
Solution
‎08-17-2017 10:44 AM
Super User
Posts: 6,637

Re: Getting residual time of date differences in a different unit

[ Edited ]

It depends on your concept of "a lot of workaround".  You could try:

 

data want;

set have;

n_days = end_date - start_date;

n_weeks = int(n_days / 7);

n_days = mod(n_days, 7);

final_variable = catx(' ', n_weeks, 'weeks,', n_days, 'days');

run;

 

As a general rule of thumb, I recommend avoiding INTCK unless you are certain of what it does, and certain that it does what you want.  Here is a test program to illustrate some of the intricacies:

 

data _null_;

start_date = '11Aug2017'd;

end_date = '14Aug2017'd;

weeks = intck('week', start_date, end_date);

put weeks=;

start_date = '07Aug2017'd;

end_date = '19Aug2017'd;

weeks = intck('week', start_date, end_date);

put weeks=;

start_date = '08Aug2017'd;

end_date = '20Aug2017'd;

weeks = intck('week', start_date, end_date);

put weeks=;

run;

 

View solution in original post


All Replies
Trusted Advisor
Posts: 1,831

Re: Getting residual time of date differences in a different unit

[ Edited ]

Try - 

     weeks = intck('week', start_date, end_date);

     res = end_date - start_date - weeks*7;

     result = left(weeks) || ' Weeks, ' || left(res) || ' Days';

PROC Star
Posts: 1,217

Re: Getting residual time of date differences in a different unit

@Shmuel wouldn't you have to subtract 1 from weeks? Smiley Happy

Trusted Advisor
Posts: 1,831

Re: Getting residual time of date differences in a different unit

@draycut - 

  weeks = intck('week', start_date, end_date); 

  result into 1 for dates 01JAN2017 to 09JAN2017, which means it calculate whole weeks of 7 days.

PROC Star
Posts: 1,217

Re: Getting residual time of date differences in a different unit

Ah yes, I see Smiley Happy

Solution
‎08-17-2017 10:44 AM
Super User
Posts: 6,637

Re: Getting residual time of date differences in a different unit

[ Edited ]

It depends on your concept of "a lot of workaround".  You could try:

 

data want;

set have;

n_days = end_date - start_date;

n_weeks = int(n_days / 7);

n_days = mod(n_days, 7);

final_variable = catx(' ', n_weeks, 'weeks,', n_days, 'days');

run;

 

As a general rule of thumb, I recommend avoiding INTCK unless you are certain of what it does, and certain that it does what you want.  Here is a test program to illustrate some of the intricacies:

 

data _null_;

start_date = '11Aug2017'd;

end_date = '14Aug2017'd;

weeks = intck('week', start_date, end_date);

put weeks=;

start_date = '07Aug2017'd;

end_date = '19Aug2017'd;

weeks = intck('week', start_date, end_date);

put weeks=;

start_date = '08Aug2017'd;

end_date = '20Aug2017'd;

weeks = intck('week', start_date, end_date);

put weeks=;

run;

 

Occasional Contributor
Posts: 16

Re: Getting residual time of date differences in a different unit

Posted in reply to Astounding

This works well - thanks for posting. It's still a bit clumsy (was hoping sas had a function similar to Excel's Datedif function) but works well. Thanks!

☑ This topic is solved.

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

Discussion stats
  • 6 replies
  • 199 views
  • 1 like
  • 4 in conversation