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
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;
Try -
weeks = intck('week', start_date, end_date);
res = end_date - start_date - weeks*7;
result = left(weeks) || ' Weeks, ' || left(res) || ' Days';
@Shmuel wouldn't you have to subtract 1 from weeks? 🙂
weeks = intck('week', start_date, end_date);
result into 1 for dates 01JAN2017 to 09JAN2017, which means it calculate whole weeks of 7 days.
Ah yes, I see 🙂
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;
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!
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.
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.