I want to create a macro variable which takes the date of the day before, and hence, its value changes each day. I am using the following code:
call symputx('PREVIOUS_DATE' , put(intnx('day', today(), -1, 's'), date9.));
But the problem is that if yesterday was a Sunday, then I would require the macro to take the date of the day before yesterday. For example today id 26APR2021, and yesterday 25APR2021 was a sunday, so I want this macro to take value 24APR2021.
Is there a way to automate this using an if clause or anything else?
If you simply want to get the previous day if it is Sunday, you can use the WEEKDAY function.
If you need to take holidays into concern, please refer to the HOLIDAY function.(Only common holidays in the U.S. and Canada.)
data _null_;
dt=intnx('day', today(), -1, 's');
wk=weekday(dt);
if wk=1 then dt=dt-1;
call symputx('PREVIOUS_DATE' , put(dt, date9.));
run;
If you simply want to get the previous day if it is Sunday, you can use the WEEKDAY function.
If you need to take holidays into concern, please refer to the HOLIDAY function.(Only common holidays in the U.S. and Canada.)
data _null_;
dt=intnx('day', today(), -1, 's');
wk=weekday(dt);
if wk=1 then dt=dt-1;
call symputx('PREVIOUS_DATE' , put(dt, date9.));
run;
Interval WEEKDAY<weekend days>W allows you to do this (with SAS the week starts Sunday so it's WEEKDAY17W). Documentation here.
data _null_;
call symputx('dt',put(intnx('WEEKDAY17W',today(),-1),date9.));
stop;
run;
%put &=dt;
Run above today on Monday April 26.
33 %put &=dt; DT=23APR2021
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.