Desktop productivity for business analysts and programmers

Difference between two dates

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 8
Accepted Solution

Difference between two dates

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,


Accepted Solutions
Solution
‎03-30-2017 04:41 PM
Grand Advisor
Posts: 10,211

Re: Difference between two dates

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


All Replies
Esteemed Advisor
Posts: 7,294

Re: Difference between two dates

[ Edited ]

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

Super Contributor
Posts: 284

Re: Difference between two dates

I thought about this, but what about saturdays?

Respected Advisor
Posts: 4,976

Re: Difference between two dates

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.

Super Contributor
Posts: 284

Re: Difference between two dates

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;
Solution
‎03-30-2017 04:41 PM
Grand Advisor
Posts: 10,211

Re: Difference between two dates

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.

 

Esteemed Advisor
Posts: 7,294

Re: Difference between two dates

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

 

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 369 views
  • 5 likes
  • 5 in conversation