Hello!
I'm trying to calculate the difference between two dates but i don't want to take into account Sundays, how can this be done?
eg. Say i'm trying to find the days between March, 2nd and March, 9th; the output should be '6'.
Any help will be much appreciated.
Greets,
Please see:
data example; days = intck('weekday1W','02MAR2017'd,'09MAR2017'd); run;
the weekday1w interval with the intck function considers a week to be six days with Sunday as a weekend day.
Note: The following is really @ballardw's suggestion, but the system wouldn't let me delete my incorrect post. I hadn't read the request sufficiently and didn't realize that only Sundays were to be excluded. Art T
Use the INTCK function. But if you want 6 days given your example, you'll have to add 1. e.g.:
data have; informat date1 date2 date9.; input date1 date2; dif=INTCK('WEEKDAY1W',date1,date2,'C'); cards; 02mar2017 09mar2017 24feb2017 09mar2017 ;
Art, CEO, AnalystFinder.com
I thought about this, but what about saturdays?
There's probably a short way, but here's the long way.
data want;
set have;
n_days = 0;
do i=date1 to date2;
if weekday(i) ne 1 then n_days + 1;
end;
if weekday(date1) > 1 or weekday(date2) > 1 then n_days = n_days - 1;
run;
There are various ways to count, depending on whether or not the starting date is a Sunday or the ending date is a Sunday. The last line in the program is just one way to account for that. You may want to set up your own rules.
I don't know if there's a slicker/more efficient solution, but this is what I'd do. Adjust the end value if you want it to be inclusive of that date. I had to put the -1 to get your desired result.
data want;
start = '02MAR2017'd;
end = '09MAR2017'd;
do i = start to end -1;
all_cnt + 1;
non_sunday_cnt + (weekday(i) ne 7);
end;
run;
Please see:
data example; days = intck('weekday1W','02MAR2017'd,'09MAR2017'd); run;
the weekday1w interval with the intck function considers a week to be six days with Sunday as a weekend day.
A nice list of the available intervals that can be used with the intck function can be found at: http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003065889.htm
Art, CEO, AnalystFinder.com
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.