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

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=&current_Month);

I hope u understand what i mean. I would be realy happy, if u could help me.

 

Thanks

 

1 ACCEPTED SOLUTION
6 REPLIES 6
PaigeMiller
Diamond | Level 26

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(&current_month,1,2021),monname3.));
run;
%put &=test_a;
--
Paige Miller
Hansi_12345
Fluorite | Level 6
I tested your code and it works well. My problem is, that i have the names of the month in german.
So Mar = Mrz, May = Mai, Oct = Okt, Dec = Dez. Is there a solution for this? Maybe another format?
Tom
Super User Tom
Super User

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=&current_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.

Hansi_12345
Fluorite | Level 6
Its difficult to explain. I just needed the macro for another code. The datas i used have e.g. the variable name "mrz_test" "apr_test" and so on. With the new macro i want to address this variable names via &test_a.test. And i dont want to give test_a the value "mrz" directly, because i already have so many different macros in my code.

Thanks to PaigeMiller i managed to get the english month-names (Mar, May, Oct, Dec) but not the german ones (Mrz, Mai, Okt, Dez). So i changed all variable names into english and now i'm happy :D. My Code do what it is made for.
Tom
Super User Tom
Super User

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 ;

 

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 6 replies
  • 1044 views
  • 3 likes
  • 3 in conversation