- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The -1 portion needs an %eval or equivalent type of function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I got my value as 4 by using %eval, may I know what’s that additional step to make it 04? By formatting it ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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 ))
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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