BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
ChrisWoo
Obsidian | Level 7
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);
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

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.

 

 

View solution in original post

8 REPLIES 8
Reeza
Super User

The -1 portion needs an %eval or equivalent type of function. 

ballardw
Super User

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);

 

ChrisWoo
Obsidian | Level 7
Hi ballardw,

I got my value as 4 by using %eval, may I know what’s that additional step to make it 04? By formatting it ?
Reeza
Super User

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.

 

 

ChrisWoo
Obsidian | Level 7
thanks mate. I was initially thinking to modify manually every January of the year.... haha
Tom
Super User Tom
Super User

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

Tom
Super User Tom
Super User

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 ))

 

Tom
Super User Tom
Super User

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 8 replies
  • 1225 views
  • 2 likes
  • 4 in conversation