I extracted some code(from SAS communities) to calculate the nth business day of the month. It is a little off. I was testing the code to make sure it returned the 3rd day of the month. I noticed Jul ,Aug, and Oct were giving me the 4th day instead. All these months have 31 days so wondering if that is the reason.
I want the 3 day of the month and will adjust for holidays for Jan, Jul and Sept only when it falls on a weekend. Can anyone confirm what is happening in these months and maybe how to accommodate the 1 day difference.
Does SAS have a US holilday calendar?
data test;
format business_day1 business_day2 business_day3 business_day4 business_day5 date9.;
business_day1 = intnx('weekday', intnx('month', '23jun2024'd, 0, 'b'), 3);
business_day2 = intnx('weekday', intnx('month', '18jul2024'd, 0, 'b'), 3);
business_day3 = intnx('weekday', intnx('month', '06aug2024'd, 0, 'b'), 3);
business_day4 = intnx('weekday', intnx('month', '11sep2024'd, 0, 'b'), 3);
business_day5 = intnx('weekday', intnx('month', '25oct2024'd, 0, 'b'), 3);
run;
INTNX WEEKDAY increments by business days, so you need to start on the last day of the previous month for your calculations to always work correctly. For example 1 Jul 2024 is a Monday and the first business day in the month. If you increment by three business days that is 4 Jul. Try this:
data test;
format business_day1 business_day2 business_day3 business_day4 business_day5 date9.;
business_day1 = intnx('weekday', intnx('month', '23jun2024'd, -1, 'E'), 3);
business_day2 = intnx('weekday', intnx('month', '18jul2024'd, -1, 'E'), 3);
business_day3 = intnx('weekday', intnx('month', '06aug2024'd, -1, 'E'), 3);
business_day4 = intnx('weekday', intnx('month', '11sep2024'd, -1, 'E'), 3);
business_day5 = intnx('weekday', intnx('month', '25oct2024'd, -1, 'E'), 3);
put _All_;
run;
I extracted some code(from SAS communities) to calculate the nth business day of the month. It is a little off. I was testing the code to make sure it returned the 3rd day of the month. I noticed Jul ,Aug, and Oct were giving me the 4th day instead. All these months have 31 days so wondering if that is the reason. I want the 3 day of the month and will adjust for holidays for Jan, Jul and Sept only when it falls on a weekend. Can anyone confirm what is happening in these months and maybe how to accommodate the 1 day difference. Does SAS have a US holilday calendar? data test; format business_day1 business_day2 business_day3 business_day4 business_day5 date9.; business_day1 = intnx('weekday', intnx('month', '23jun2024'd, 0, 'b'), 3); business_day2 = intnx('weekday', intnx('month', '18jul2024'd, 0, 'b'), 3); business_day3 = intnx('weekday', intnx('month', '06aug2024'd, 0, 'b'), 3); business_day4 = intnx('weekday', intnx('month', '11sep2024'd, 0, 'b'), 3); business_day5 = intnx('weekday', intnx('month', '25oct2024'd, 0, 'b'), 3); run;
The 'weekday' interval calculates the nth business day, skipping weekends. The discrepancy likely arises because the INTNX function starts counting business days from the first day of the month. If the first day of the month falls on a weekend, the calculation might push the nth business day forward.
Did you read this post?
https://blogs.sas.com/content/sgf/2020/01/27/shifting-a-date-by-a-given-number-of-workdays/
Try making a dataset with your company holidays and then load into a hash and use it to skip some weekdays. So something like this.
data holidays ;
length Year Holiday 8 HolidayName $30 ;
do year=2023 to 2024 ;
do HolidayName = 'newyear','mlk','uspresidents','memorial'
,'usindependence','labor', 'columbus','veterans'
, 'thanksgiving','christmas' ;
Holiday=holiday(HolidayName,year);
output;
end;
end;
format Holiday date9.;
run;
proc print;
run;
data test;
input date :date.;
array business_day[5];
if _n_=1 then do;
length Holiday 8 HolidayName $30 ;
format Holiday date9.;
declare hash h(dataset:'holidays');
h.definekey('Holiday');
h.definedata('HolidayName');
h.definedone();
end;
day=1;
Holiday = intnx('weekday',date,0);
do until (day>5);
do until(h.find());
Holiday = intnx('weekday', Holiday,1);
end;
business_day[day]=Holiday;
day+1;
end;
format date business_day: date9.;
drop day Holiday;
cards;
01jan2024
02jul2024
01sep2024
01oct2024
21dec2024
;
proc print;
run;
Result:
business_ business_ business_ business_ business_ Obs date day1 day2 day3 day4 day5 HolidayName 1 01JAN2024 02JAN2024 03JAN2024 04JAN2024 05JAN2024 08JAN2024 2 02JUL2024 03JUL2024 05JUL2024 08JUL2024 09JUL2024 10JUL2024 usindependence 3 01SEP2024 03SEP2024 04SEP2024 05SEP2024 06SEP2024 09SEP2024 labor 4 01OCT2024 02OCT2024 03OCT2024 04OCT2024 07OCT2024 08OCT2024 5 21DEC2024 23DEC2024 24DEC2024 26DEC2024 27DEC2024 30DEC2024 christmas
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.