Before SAS9.2 you can determine the date 2 working days before the rundate with the INTNX() function as displayed by
209 %put wd_minus2 = %sysfunc( intnx( weekday, "&sysdate"d, -2), weekdate );
wd_minus2 = Friday, January 28, 2011
Of more use would be the date as a sas date constant, like
%let wd_minus2 = %sysfunc( intnx( weekday, "&sysdate"d, -2), date9 );
Then it can be used like
if opnDate LT "&wd_minus2"d or opnDate GT "&wd_minus2"d
if you would like it as a julian date formatted string then adapt the above
%let wd_minus2J = %sysfunc( intnx( weekday, "&sysdate"d, -2), julian7 );
but that won't work as a date constant.
However, interval WEEKDAY does not provide logic to step over holiday dates
if you have SAS9.2 you can enjoy the new system option INTERVALDS.
This enbles you to define and use custom intervals which could combine weekends and your dates from a table
like:
libname my (work);
data my.hols;
input date :date9. ;
format date weekdate. ;
list;cards ;
27Jan11
26jan11
24jan11
1feb2011
;
* limit period of interest to last 20 weekdays;
%let tod = &sysdate ;
%let frd = %sysfunc( intnx( weekday, "&tod"d, -20 ), date9 ) ;
data span ;
format begin end weekdate. ;
keep begin end ;
* build the diary - holidays first ;
array dates(%sysevalf("&frd"d):%sysevalf("&tod"d)) $1 temporary ;
do while( not eof1 ) ;
set my.hols( where=( date between "&frd"d and "&tod"d )) end=eof1 ;
dates(date) ='H' ;
end ;
* now mark weekends ;
s1 = "&frd"d + 7 - weekday( "&frd"d ) ;* the first Saturday ;
do date= s1 to "&tod"d by 7 ; * saturday to saturday ;
dates( date) = 'W' ;
if "&tod"d GT date then dates( date+1) ='W' ; * Sunday ;
end ;
* now build custom intervals ;
do date= "&frd"d to "&tod"d ;
if dates(date) = ' ' then do;
if oldDay ne . then do;
begin = oldDay ;
end = date-1 ;
output ;
oldDay = date ;
end ;
else oldDay = coalesce( oldDay, date-1 ) ;
end ;
end ;
* check if diary finished on non-working day ;
if oldDay ne . then do;
begin = oldDay ;
end = "&tod"d ;
output ;
end ;
stop ;
format begin end weekdate. ;
run ;
option intervalDS = ( myWorkingDay = span );
%put last_wd_ex_hols = %sysfunc( intnx( myWorkingDay, "&sysdate"d, -2 ), weekdate) ;
%put last_wd_inc_all = %sysfunc( intnx( weekday , "&sysdate"d, -2 ), weekdate) ;
here is the log of the final results
207 %put last_wd_ex_hols = %sysfunc( intnx( myWorkingDay, "&sysdate"d, -2 ), weekdate);
last_wd_ex_hols = Tuesday, January 25, 2011
208 %put last_wd_inc_all = %sysfunc( intnx( weekday , "&sysdate"d, -2 ), weekdate);
last_wd_inc_all = Friday, January 28, 2011
demonstrating the effect of marking today, 26th January and 27th as holidays.
peterC
Message was edited by: Peter Crawford 24-Dec-2011 to reinstate plain text formating that was lost when the Forum posting was converted to Communities
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.