Hello
I want to create a macro variable that calculate YYYY-MM-DD value by the following rule:
If today is Sunday-Friday then
%let YYYYMMDD =%sysfunc(putn(%sysevalf(%sysfunc(today())-1), YYMMDD10.));
If today is Saturday then
%let YYYYMMDD =%sysfunc(putn(%sysevalf(%sysfunc(today())-2), YYMMDD10.));
What is the way to do it please?
You can use the weekday() function together with %IF logic.
Try this:
%let yyyymmdd = %sysfunc(int(%sysevalf(%sysfunc(today())-1-(%sysfunc(weekday(%sysfunc(today())))=1))),yymmdd10.);
%put &=yyyymmdd.;
Bart
Or with FCMP, which is easier to read and modify:
proc FCMP outlib=work.f.p;
function fff();
return( today()-1-(weekday(today())=1) );
endsub;
run;
options append=(cmplib=work.f);
%let yyyymmdd=%sysfunc(fff(),yymmdd10.);
%put &=yyyymmdd.;
Bart
Generic style comment: If the macro variable is to ever be used in comparisons with date values or calculations then do not format it.
The time to use formatted macro date values is for text that humans read such as title statements or possibly filenames.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.