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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

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
  • 521 views
  • 2 likes
  • 4 in conversation