BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
RowanB1986
Fluorite | Level 6

Hi,

 

I'm struggling to find a simple way of conditionally setting a variable to the number of the previous month despite having spent a long time trawling through forum answers and trying to interpret answers so apologies for the schoolboy question.

 

Please can someone explain to me why the following code is returning the error "Required operator not found in expression: &x = 0?:

 

%macro MonthNumber(x);
	%if &x = 0 %then 12;
	%else Month(Date())-1;
%mend MonthNumber;

%LET MonthNo = Month(Date())-1;
%LET END_DATE = intnx('month',MDY(%MonthNumber(&MonthNo),1,Year(Date())) ,0,'end');

Any help is much appreciated,

 

Rowan

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

You can't use normal sas-function ins macro-code, you have to wrap all function in %sysfunc-calls. Also note that quoting string in macro is seldom necessary.

 

It is, as always, a good idea to start without any macro code, so use a data-null-step and call symputx to create macro-variables.

 

The code is untested.

 

data _null_;
  lastMonthDate = intnx('month', today(), -1, 'same');
  lastMonthNum = month(lastMonthDate);
  call symputx('lastMonth', lastMonthNum);
run;

%put &=lastMonth;

View solution in original post

2 REPLIES 2
andreas_lds
Jade | Level 19

You can't use normal sas-function ins macro-code, you have to wrap all function in %sysfunc-calls. Also note that quoting string in macro is seldom necessary.

 

It is, as always, a good idea to start without any macro code, so use a data-null-step and call symputx to create macro-variables.

 

The code is untested.

 

data _null_;
  lastMonthDate = intnx('month', today(), -1, 'same');
  lastMonthNum = month(lastMonthDate);
  call symputx('lastMonth', lastMonthNum);
run;

%put &=lastMonth;
RowanB1986
Fluorite | Level 6

Thank you very much Andreas. Things are starting to make a lot more sense!

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!

How to Concatenate Values

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.

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
  • 2 replies
  • 717 views
  • 0 likes
  • 2 in conversation