BookmarkSubscribeRSS Feed
model_coder
Calcite | Level 5
Is there a function in SAS that I can use to check if a given date is a business day and if its not a business day, then find the previous or the next business day?
6 REPLIES 6
Cynthia_sas
SAS Super FREQ
Hi:
In the simplest form, the WEEKDAY function can take a SAS date value as an argument and return the day of the week, as a number (1=Sunday, 2=Monday, etc, 7=Saturday). Using that returned value as a starting point, you can write logic to advance the date until the weekday function returns a value between 2-6 (Monday-Friday).

If by "business day" you mean something more sophisticated (such as taking holidays into account) and finding the next date, then you might need to use logic such as found in this Tech Support note:
http://support.sas.com/kb/26/044.html

cynthia
model_coder
Calcite | Level 5
Thanks Cynthia. I already implemented a code that somewhat matches your suggestion. But then I ran into the issue of holidays. I guess we need to implement something more sophisticated as suggested in your tech note.
Thanks for help!
JLD
SAS Employee JLD
SAS Employee

To find the next business day , use INTNX('WEEKDAY',...)  - see code below.

  if Thisdate = intnx('WEEKDAY',Thisdate,0) then NextWorkday=Thisdate; else NextWorkday=intnx('weekday',Thisdate,1);

To find the previous business day, simply use

    PreviousBusinessday = intnx('weekday',Thisdate,0) ;

This does not take into account holidays, only Saturday & Sunday.

In Saudi Arabia, where weekend is on Thursday & Friday, use this code instead

intnx('weekday56w',Thisdate,0) ;  see SAS(R) 9.2 Language Reference: Dictionary, Fourth Edition

Ron_MacroMaven
Lapis Lazuli | Level 10

Yes, SAS does have a holiday function

http://www.sascommunity.org/wiki/Generating_Holiday_Lists

Ron Fehd sas.comm.wiki maven

art297
Opal | Level 21

And, if you need to expand the holiday function to incorporate holidays that aren't already included in the holiday function, take a look at the customizable holiday function at: http://www.sascommunity.org/wiki/Sometimes_One_Needs_an_Option_with_Unusual_Dates

prasadborra
Calcite | Level 5

proc fcmp outlib = work.funcs.dt;
/*use the function compiler procedure*/

function nextworkday(startday, interval);/*define a custom function*/
x= intnx("weekday1w",startday,interval);/*interval is the number of days after which you want to check for a business day*/

if x not in &holidays then
return(x);
else return(nextworkday(x,interval-(interval-1)));/*use recursion to check if day next day after interval falls on a holiday*/
endsub;
quit;

options cmplib=work.dt;
%let holidays=("01JAN2018"d, "16FEB2018"D, "17FEB2018"D, "30MAR2018"D, "01MAY2018"D, "29MAY2018"D, "15JUN2018"D, "09AUG2018"D, "22AUG2018"D, "06NOV2018"D, "25DEC2018"D);/*These are singapore holidays for FY '18*/
%let startdate="01jan2018"d;
%let enddate="31dec2018"d;

data test;
do i= intnx("month",&startdate,0,"b") to intnx("month",&enddate,0,"e");
j=put(i,date9.);
z=nextworkday(i,3);
format z date9.;
output;
end;
run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 24921 views
  • 1 like
  • 6 in conversation