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

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?

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

As the %MACRO Statement Documentation says:

 

"you cannot use a text expression to generate a macro name in a %MACRO statement."

View solution in original post

11 REPLIES 11
PeterClemmensen
Tourmaline | Level 20

As the %MACRO Statement Documentation says:

 

"you cannot use a text expression to generate a macro name in a %MACRO statement."

ballardw
Super User

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.

 

 

Quodly
Obsidian | Level 7
sorry for my late reply. i wanted to define macros inside a macro loop, each macro named differently. i did it another way now. thanks anyway!
Tom
Super User Tom
Super User
You do not ever need (or want) to redefine a MACRO inside of a loop. Perhaps instead you meant that you want to define a new macro VARIABLE in a loop?
%do i=1 %to %sysfunc(countw(&list,%str( ))); %let element=%scan(&list,&I,%str( )); .... %end;
Or you meant to CALL a macro inside the loop?
%do i=1 %to %sysfunc(countw(&list,%str( ))); %sub_macro(%scan(&list,&I,%str( ))); %end;

ishmo65
Obsidian | Level 7

Is there any way I can do this ?

;;;;;
options mprint mlogic symbolgen;
options mautocomploc;
%let macroname=temp;

%sysmacdelete &macroname.;

%macro &macroname.(var1,var2);
	%put Macro started: &macroname.;
	%put ===============================;

/* macro code here */

	%put Macro ended: &macroname.;
	%put =============================;

%mend &macroname.;

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(&macroname.);
	put a=;
	if a=1 then
		put "&macroname. created successfully";
	else
		put "&macroname. not created - check for errors";
run;

The code outside of the macro works but the macro create fails with:

11049  %macro &macroname.(var1,var2);
ERROR: Invalid macro name &.  It should be a valid SAS identifier no longer than 32 characters.
...
11281  %mend &macroname.;
WARNING: Extraneous text on %MEND statement ignored for macro definition .
ishmo65
Obsidian | Level 7
My solution (which I've finalised since my last response is to hardcode the macro name as:
%macro temp;
conclude it with:
%mend temp;
then check temp is created:
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(temp);
put a=;
if a=1 then
put "Macro temp created successfully";
else
put "Macro temp not created - check for errors";
run;
and then rename the macro:
proc catalog cat=work.sasmacr;
change temp=&macroname. /et=macro;
run;
Then use the original final step to check the rename has worked:
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(&macroname.);
put a=;
if a=1 then
put "Macro &macroname. created successfully";
else
put "Macro &macroname. not created - check for errors";
run;
ishmo65
Obsidian | Level 7
That didn't wok - running the macro resulted in the following errors:
ERROR: The macro name XYZ and the internal macro header name TEMP in the SASMACR macro catalog differ. Recompile this macro and try again.
ERROR: Macro library damaged. Cannot execute macro XYZ on V9.x compiled using V9.3x on DDMMYY.
Tom
Super User Tom
Super User

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.

yabwon
Onyx | Level 15

One approach could be as follows:



%let macroname=temp;

%sysmacdelete &macroname. / 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 
/*&macroname.*/
(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(&macroname.);
	put a=;
	if a=1 then
		put "&macroname. created successfully";
	else
		put "&macroname. not created - check for errors";
run;

 

Bart

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



yabwon
Onyx | Level 15

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 ' !! " &macroname." !! code);
run;

B.

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



Tom
Super User Tom
Super User

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. ;

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
  • 11 replies
  • 6124 views
  • 1 like
  • 6 in conversation