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

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