Determine the number of business days between two dates. Do not count specific holidays as business days.
Featured Step | DATA step |
Featured Step Options and Statements | HOLIDAY, INTCK, and WEEKDAY functions |
Data set DAY_RANGES has two date variables, DAY1 and DAY2.
DAY_RANGES Obs day1 day2 1 07/01/2008 07/05/2008 2 06/23/2008 06/27/2008 3 05/19/2008 05/19/2009 4 04/01/2008 04/05/2011 5 01/23/2009 01/25/2009
Output 9.15 BUSINESS_DAYS Data SetExample 9.15 BUSINESS_DAYS Obs day1 day2 totaldays workdays holidays 1 07/01/2008 07/05/2008 4 2 1 2 06/23/2008 06/27/2008 4 4 0 3 05/19/2008 05/19/2009 365 255 6 4 04/01/2008 04/05/2011 1099 771 18 5 01/23/2009 01/25/2009 2 0 0 |
The following DATA step calculates the number of business days between two dates by using the INTCK function and the "WEEKDAY" interval. It also includes code to test whether the range of days includes holidays that occur on business days. For each weekday holiday that is found within the range of days, the number of business days is reduced by 1.
Data set DAY RANGES has two date variables, DAY1 and DAY2. The DATA step determines the following within the range of days between DAY1 and DAY2:
the number of days (variable TOTALDAYS)
the number of business days minus any holidays that occur on a business day (variable WORKDAYS)
the number of holidays (variable HOLIDAYS) whether or not they occur during the work week
The DATA step uses an iterative DO loop to look for holidays that occur on business days within the range of days between DAY1 and DAY2. It uses function WEEKDAY to determine whether a holiday occurs on a business day.
SAS function HOLIDAY returns the dates of specific U.S. and Canadian holidays. The first argument to HOLIDAY is a character value that references one of the specific holidays. The second argument to HOLIDAY is the year. For the full list of holidays that HOLIDAY recognizes, see SAS documentation.
Temporary array HOLIDAY_LIST contains the list of holiday keywords for which the business day total should be adjusted if the holiday occurs on a business day.
Create data set BUSINESS_DAYS. Read the observations in DAY_RANGES. List the holidays that you want the DATA step to check whether they fall on business days. Specify the holidays for which to check if they occur on a business day. Specify the values in the form known to function HOLIDAY.
Calculate the total number of days. Calculate the total number of business days Monday through Friday. Initialize the number of holidays. Check all the holidays in HOLIDAY_LIST between the year of DAY1 and the year of DAY2. Execute this DO group when the current element of HOLIDAY_LIST falls within the range of days between DAY1 and DAY2. Increment HOLIDAYS by 1 since the current element of HOLIDAY_LIST is within the range of days between DAY1 and DAY2. If the current element of HOLIDAY_LIST for the year that is being processed occurs on a weekday, decrement WORKDAYS by 1.
data business_days; set day_ranges; array holiday_list{6} $ 25 _temporary_ ('NEWYEAR','MEMORIAL','USINDEPENDENCE','LABOR', 'THANKSGIVING','CHRISTMAS'), drop i y; totaldays=intck('days',day1,day2); workdays=intck('weekday',day1,day2); holidays=0; do y=year(day1) to year(day2); do i=1 to dim(holiday_list); if (day1 le holiday(holiday_list{i},y) le day2) then do; holidays=holidays+1; if (2 le weekday(holiday(holiday_list{i},y)) le 6) then workdays=workdays-1; end; end; end; run;
18.218.129.92