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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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

6 REPLIES 6
Shmuel
Garnet | Level 18

Try - 

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

     res = end_date - start_date - weeks*7;

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

PeterClemmensen
Tourmaline | Level 20

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

Shmuel
Garnet | Level 18

@PeterClemmensen - 

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

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

Astounding
PROC Star

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;

 

cbt2119
Obsidian | Level 7

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!

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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