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

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,

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

View solution in original post

6 REPLIES 6
art297
Opal | Level 21

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

collinelliot
Barite | Level 11

I thought about this, but what about saturdays?

Astounding
PROC Star

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.

collinelliot
Barite | Level 11

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;
ballardw
Super User

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.

 

art297
Opal | Level 21

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

 

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

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 3540 views
  • 5 likes
  • 5 in conversation