/* setup some holiday data */
DATA WORK.usholidays;
length
HolidayDate 8
Comment $ 27;
format
HolidayDate DATE9.
Comment $CHAR27.;
infile datalines
dlm=','
missover
dsd;
input
HolidayDate : mmddyy12.
Comment : $CHAR27.;
datalines;
1/1/2010,New Year's Day
1/18/2010,Birthday of Martin Luther King Jr.
5/31/2010,Memorial Day
7/4/2010,Independence Day
7/5/2010,Independence Day (obs)
9/6/2010,Labor Day
10/11/2010,Columbus Day
11/8/2010,Veterans Day
11/25/2010,Thanksgiving Day
12/25/2010,Christmas Day
1/1/2010,New Year's Day
1/17/2011,Birthday of Martin Luther King Jr.
5/30/2011,Memorial Day
7/4/2011,Independence Day
9/5/2011,Labor Day
10/10/2011,Columbus Day
11/11/2011,Veterans Day
11/24/2011,Thanksgiving Day
12/25/2011,Christmas Day
2/21/2012,Washington's Birthday
1/1/2012,New Year's Day
1/2/2012,New Year's Day
1/17/2012,Birthday of Martin Luther King Jr.
2/20/2012,Washington's Birthday
5/28/2012,Memorial Day
7/4/2012,Independence Day
9/3/2012,Labor Day
10/8/2012,Columbus Day
11/12/2012,Veterans Day
11/22/2012,Thanksgiving Day
12/25/2012,Christmas Day
;
run;
/*
* Mimic the NETWORKDAYS example here:
* https://office.microsoft.com/en-us/excel-help/networkdays-HP005209190.aspx
*/
proc fcmp outlib=work.myfuncs.dates;
function networkdays(d1,d2,holidayDataset $,dateColumn $);
/* make sure the start date < end date */
start_date = min(d1,d2);
end_date = max(d1,d2);
/* read holiday data into array */
/* array will resize as necessary */
array holidays[1] / nosymbols;
if (not missing(holidayDataset) and exist(holidayDataset)) then
rc = read_array(holidayDataset, holidays, dateColumn);
else put "NOTE: networkdays(): No Holiday data considered";
/* INTCK computes transitions from one day to the next */
/* To include the start date, if it is a weekday, then */
/* make the start date one day earlier. */
if (1 < weekday(start_date)< 7) then start_date = start_date-1;
diff = intck('WEEKDAY', start_date, end_date);
do i = 1 to dim(holidays);
if (1 < weekday(holidays[i])< 7) and
(start_date <= holidays[i] <= end_date) then
diff = diff - 1;
end;
return(diff);
endsub;
run; quit;
options cmplib=work.myfuncs;
/* test with one value */
data _null_;
start_date = '31DEC2010'd;
end_date = '31MAY2011'd;
days = networkdays(start_date, end_date, "work.usholidays","holidaydate");
put days=;
run;
/* test with data set of values */
data test;
length dates 8;
format dates date9.;
infile datalines dsd;
input dates : date9.;
workdaysSince = networkdays(dates, today(), "work.usholidays","holidaydate");
datalines;
01NOV2010
21NOV2010
01DEC2010
01APR2011
;
title "As of &SYSDATE";
proc print data=test; run;