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!

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!

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
  • 6 replies
  • 719 views
  • 1 like
  • 4 in conversation