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
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;
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;
Thank you very much Andreas. Things are starting to make a lot more sense!
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!
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.