I need to test some date logic I am using for creating date variables and we have specific scenarios if it is Sunday and the 1st day of the month.
If I want to test each day but I need to know how do I override the system values for weekday and day to test my logic?
here is what my logic looks like:
data _null_;
if weekday(date()) = 1 then do; /* weekday = sunday */ /* code should not run on sundays unless it is Sat EOM */
if day(date()) ^= 1 then do; /* day of month not the 1st day of the month */ /* code should not run on sundays unless it is Sat EOM */
wod2 = date()-1;
wod1 = date()-2;
end;
/* Saturday EOM processing - Code should run on Sunday if it is the 1st day of the month - pick up Saturday file (-1) */
else do;
wod2 = date()-1;
wod1 = date()-2;
end;
end;
/* if monday and the 2nd of the month, code should not run because there is no sunday file due to SAT EOM Batch */
if weekday(date()) = 2 then do; /* weekday = Monday */
if day(date()) ^= 2 then do; /* day of month not the 2nd day of the month */ /* BAU Monday pull -1(Sunday) and -3(Friday) files */
wod2 = date()-1;
wod1 = date()-3;
end;
else do; /* if is monday and the 2nd of the month, code should not run */
wod2 = date()-2;
wod1 = date()-3;
end;
end;
/* if tuesday and the 3rd of the month, after no run monday 2nd of month, code should pull for Monday(-1) and Sunday(-2) */
if weekday(date()) = 3 then do; /* weekday = Tuesday */
if day(date()) ^= 3 then do; /* day of month not the 3rd day of the month - then just -1 */
wod2 = date()-1;
wod1 = date()-2;
end;
else do; /* if is the 3rd then -1 and -3 */
wod2 = date()-1;
wod1 = date()-3;
end;
end;
/* BAU Processing *//* any other day of the week will pull dates previous days(-1) and (-2)*/
if weekday(date()) > 3 then do;
wod2 = date()-1;
wod1 = date()-2;
end;
CALL SYMPUT ("wod2",put(wod2,YYMMDDN8.));
CALL SYMPUT ("wod1",put(wod1,YYMMDDN8.));
run;
%put &wod1;
%put &wod2;
can you override those system values?
Thanks,
Elliott
One way would be to replace all of the DATE() calls with a macro reference. such as
if weekday(&dd) = 1
Then before the code define the macro variable as:
%let dd = %sysfunc(date());
Then when you want to use a specific date use something like
%let dd = "01JAN2023"d; (or any of a number of ways to set a date value .
Look a calendar for likely dates to have the day of week/ day of month as you want to test.
I don't really know what you mean by "override" the WEEKDAY though. It is a function. Given a value that is a date it returns the weekday. Any other behavior would be pretty detrimental to code.
One way would be to replace all of the DATE() calls with a macro reference. such as
if weekday(&dd) = 1
Then before the code define the macro variable as:
%let dd = %sysfunc(date());
Then when you want to use a specific date use something like
%let dd = "01JAN2023"d; (or any of a number of ways to set a date value .
Look a calendar for likely dates to have the day of week/ day of month as you want to test.
I don't really know what you mean by "override" the WEEKDAY though. It is a function. Given a value that is a date it returns the weekday. Any other behavior would be pretty detrimental to code.
Thank you this is exactly what I was looking for.
Cheers
Elliott
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.