Hi SAS-Experts,
i have a problem and have no idea how to fix it.
I want to define a macro-variable with an if-condition.
I mean i have e.g. the macro-variable "current_month" that is 3. In the next step i want to convert it into a macro called "month_test" that include the month as a name.
I tried it with the attached code but get the error "Statement is not valid..."
%let current_Month = 3; %let month_test = &test_a;
%macro testmonth(Month=); %if &Month = 1 %then %do; &test_a = Jan %end; %else %if &Month = 2 %then %do; &test_a = Feb %end; %else %if &Month = 3 %then %do; &test_a = Mar %end; %else &test_a = Dec;; run; %mend testmonth; %testmonth(Month=¤t_Month);
I hope u understand what i mean. I would be realy happy, if u could help me.
Thanks
Google finds the answer quickly
Not a direct answer to why your macro code is producing errors ... but ...
Don't use a macro at all here. That's a huge amount of typing, when this will get the job done with much less typing and no macros.
%let current_month=3;
data _null_;
call symputx('test_a',put(mdy(¤t_month,1,2021),monname3.));
run;
%put &=test_a;
Google finds the answer quickly
Right now it is only generating part of a statement. If you want it to only generate part of statement then you have to call the macro so that the generated code can be using in a statement. For example in a %PUT statement.
386 %put %testmonth(Month=¤t_Month); WARNING: Apparent symbolic reference TEST_A not resolved. &test_a = Mar
What code is it that you want the macro to generate?
What is the macro variable TEST_A that the code is referencing? What types of values will it contain?
It is confusing to reference a macro variable in the middle of a macro that is not either an input or local. At a minimum you should add a comment to explain where the macro variable should come from and what types of values it should have.
Since there are only 12 months you could just write your own logic for converting month number to whatever string you want. For example you could just type out the 12 strings yourself and use %SCAN() to pick the right one.
%macro german_month(date);
%scan(Jan ... Dez,%sysfunc(month(&date)))
%mend;
Then call the macro were you want to generate that three letter string.
%let varname=%german_month("&sysdate9"d)_test ;
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!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.