running
%let var = 1;
%macro mac_&var.;
%put emmanuel macro;
%mend;
%mac_1;
gives
1 %let var = 1;
2
3 %macro mac_&var.;
ERROR: Expected semicolon not found. The macro will not be compiled.
ERROR: A dummy macro will be compiled.
4 %put emmanuel macro;
5 %mend;
6
7 %mac_1;
-
180
WARNING: Apparent invocation of macro MAC_1 not resolved.
ERROR 180-322: Statement is not valid or it is used out of proper order.
what's wrong with it? can't i use macro variables in the definition of macros?
As the %MACRO Statement Documentation says:
"you cannot use a text expression to generate a macro name in a %MACRO statement."
As the %MACRO Statement Documentation says:
"you cannot use a text expression to generate a macro name in a %MACRO statement."
If you can describe why you thought you needed this sort of naming we may be able to provide alternate approaches to accomplish something similar.
Is there any way I can do this ?
;;;;;
options mprint mlogic symbolgen;
options mautocomploc;
%let macroname=temp;
%sysmacdelete ¯oname.;
%macro ¯oname.(var1,var2);
%put Macro started: ¯oname.;
%put ===============================;
/* macro code here */
%put Macro ended: ¯oname.;
%put =============================;
%mend ¯oname.;
data _null_;
attrib a format=1.;
/* check log for the following message from the put statement */
put "Macro ran successfully - delimiters (e.g. single/double quotes ok";
a=%sysmacexist(¯oname.);
put a=;
if a=1 then
put "¯oname. created successfully";
else
put "¯oname. not created - check for errors";
run;
The code outside of the macro works but the macro create fails with:
11049 %macro ¯oname.(var1,var2); ERROR: Invalid macro name &. It should be a valid SAS identifier no longer than 32 characters. ... 11281 %mend ¯oname.; WARNING: Extraneous text on %MEND statement ignored for macro definition .
Not sure why you posted a new question into an old thread. But the answer is still the same. You cannot use macro variables to generate the %MACRO statement. I doubt there is any real problem that requites that you do that, but if insist on dynamically generating code to create a %MACRO statement then use some other method of code generation. Such as writing the code to a file and %INCLUDING the file.
One approach could be as follows:
%let macroname=temp;
%sysmacdelete ¯oname. / nowarn;
data _null_;
length code $ 32767;
retain code " ";
input;
code = catx(" ",code,_infile_);
if _N_ = 2 then code = catx(" ", code, symget("macroname"));
if strip(_infile_) =: '%mend' then
do;
put code;
rc = resolve(code);
end;
cards4;
%macro
/*¯oname.*/
(var1,var2);
%put Macro started: &SYSMACRONAME.;
%put ===============================;
/* macro code here */
%put Macro ended: &SYSMACRONAME.;
%put =============================;
%mend;
;;;;
run;
data _null_;
attrib a format=1.;
/* check log for the following message from the put statement */
put "Macro ran successfully - delimiters (e.g. single/double quotes ok";
a=%sysmacexist(¯oname.);
put a=;
if a=1 then
put "¯oname. created successfully";
else
put "¯oname. not created - check for errors";
run;
Bart
or even simpler:
data _null_;
length code $ 32767;
code = '
(var1,var2);
%put Macro started: &SYSMACRONAME.;
%put ===============================;
/* macro code here */
%put Macro ended: &SYSMACRONAME.;
%put =============================;
%mend;
';
rc = resolve('%macro ' !! " ¯oname." !! code);
run;
B.
You seem to confused.
Did you mean to do something like this instead?
%macro mac_1;
%put First Macro ;
%mend;
%macro mac_2;
%put Second Macro ;
%mend;
....
%let var = 1;
%mac_&var. ;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
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.