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!
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.
Ready to level-up your skills? Choose your own adventure.