What happens in January? You'll get a 0....
%Let PrevMonth = %sysfunc(intnx(month, %sysfunc(today()), -1, s), mmddyy2.);
Use INTNX to move the date one month back.
Use Today to get the current date
SYSFUNC has a second parameter which is the format, use the mmddyy2 format to get just the month number with the leading zero. The 2, takes only the first two characters.
The -1 portion needs an %eval or equivalent type of function.
You have a -1 inside the outer call to %sysfunc. In effect: %sysfunc(<some result> -1) and %sysfunc does not know how to use that.
Consider:
%Let PrevMonth = %sysfunc (month( %sysfunc( today() ) )); %put Prevmonth here is: &prevmonth; %let PrevMonth = %eval(%sysfunc (month( %sysfunc( today() ) )) -1); %put PrevMonth here is: &prevmonth;
The macro language generally does not "do arithmetic" unless specifically called to with %eval (integers) or %sysevalf (decimals)
If you truly need 04 and not 4 then you need yet another step to place the 0.
@ChrisWoo wrote:
May i know why my macro variable hit error =“ expected close parenthesis after macro function invocation not found”
I wanna get the value 04 assuming now is MAY2023
/*Here my code*/
%Let PrevMonth = %sysfunc (month(%sysfunc(today()))-1);
Proc sql;
Create table abc (date num format=z2.);
Insert into abc values (&PrevMonth);
What happens in January? You'll get a 0....
%Let PrevMonth = %sysfunc(intnx(month, %sysfunc(today()), -1, s), mmddyy2.);
Use INTNX to move the date one month back.
Use Today to get the current date
SYSFUNC has a second parameter which is the format, use the mmddyy2 format to get just the month number with the leading zero. The 2, takes only the first two characters.
The macro language does not do arithmetic. But %SYSFUNC() is pushing what you pass to SAS to run. So you can use arithmetic in the arguments passed to the SAS functions by %SYSFUNC().
This function
%put %sysfunc(month( %sysfunc(today())-1 ));
Will return the month number for yesterday instead of for today.
If you want it to add a leading zero when the month is before October then add the optional format specification to the outer %SYSFUNC() call.
9206 %put %sysfunc(month( %sysfunc(today())-1 )); 5 9207 %put %sysfunc(month( %sysfunc(today())-1 ),z2.); 05
Each %SYSFUNC call needs two right ). One to end the function you are calling and one to end the %SYSFUNC call.
You have one in the wrong place.
Let's work our way from the innermost out.
%sysfunc(today())
%sysfunc(today())-1
%sysfunc(month( %sysfunc(today())-1 ))
Do you want the number of the month of the previous day? That is what it looks like your code is trying to do.
%put %sysfunc(month( %sysfunc(today())-1 ),z2.);
Or do you want the number of the previous month?
%put %sysfunc(month( %sysfunc(intnx(month, %sysfunc(today()) ,-1)) ),Z2.);
Results:
9211 %put %sysfunc(month( %sysfunc(today())-1 ),z2.); 05 9212 %put %sysfunc(month( %sysfunc(intnx(month, %sysfunc(today()) ,-1)) ),Z2.); 04
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.