## Calculate difference of 2 dates except not counting weekends and holidays

Occasional Contributor
Posts: 14

# Calculate difference of 2 dates except not counting weekends and holidays

I have the following data and I need to calculate the difference between FINISHED and STARTED minus any weekends (any Saturdays and any Sundays) and also minus any holidays that might exist between the two dates.

 Obs Started Finished 1 Wednesday, August 28, 2013 Thursday, November 14, 2013 2 Tuesday, June 18, 2013 Monday, November 25, 2013 3 Tuesday, July 23, 2013 Friday, November 8, 2013 4 Wednesday, September 11, 2013 Friday, November 15, 2013 5 Wednesday, September 18, 2013 Thursday, December 5, 2013 6 Wednesday, July 24, 2013 Tuesday, December 3, 2013 7 Friday, October 4, 2013 Monday, November 11, 2013 8 Tuesday, August 27, 2013 Wednesday, November 27, 2013 9 Wednesday, October 2, 2013 Wednesday, November 6, 2013 10 Thursday, July 11, 2013 Monday, November 4, 2013

I know how to calculate the number of days difference between the two variables, but I don't know how to do this other additional step.

For my purposes, the holidays which exist in the above time periods are only the following holidays:

 Thursday, July 04, 2013 Independence Day Monday, September 02, 2013 Labor Day Monday, October 14, 2013 Columbus Day Monday, November 11, 2013 Veterans' Day Thursday, November 28, 2013 Thanksgiving Wednesday, December 25, 2013 Christmas Day Tuesday, December 31, 2013 New Year's Eve
Super Contributor
Posts: 578

## Re: Calculate difference of 2 dates except not counting weekends and holidays

I think the general solution is to build a dataset consisting of the days you consider to be "work" days and then summing those.  There's a support doc on how to do it.

26044 - Determine the number of business days between two SAS dates

`proc sql;  create table final_sql as  select startdt format=date9.,  stopdt format=date9.,  (select count(*)  from calendar  where dt between stopdt and startdt  and type = 'Workday') as workdays  from test;quit;`

Frequent Contributor
Posts: 106

## Re: Calculate difference of 2 dates except not counting weekends and holidays

check this out:

data holiday;

input holiday date9.;

cards;

24Dec2013

25Dec2013

31Dec2013

01Jan2014

;

data real_date;

input real_date date9.;format real_date date9.;

cards;

20Dec2013

21Dec2013

22Dec2013

23Jan2013

24Dec2013

25Dec2013

26DEC2013

27DEC2013

28DEC2013

29DEC2013

30Dec2013

31DEC2013

01JAN2014

02JAN2014

05JAN2014

10JAN2014

12JAN2014

14JAN2014

15JAN2014

;

proc sql noprint;

select holiday into :holiday separated by ',' from holiday;

quit;

data test;

set real_date(where=(weekday(real_date) not in (1,7) and real_date not in (&holiday.)));

Diff=intck('day','14JAN2014'd,real_date);

run;

PROC Star
Posts: 8,165

## Re: Calculate difference of 2 dates except not counting weekends and holidays

Discussion stats
• 3 replies
• 2186 views
• 1 like
• 4 in conversation