DATA Step, Macro, Functions and more

&macro statement

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 19
Accepted Solution

&macro statement

Hi. I'm trying to use a macro variable in the name of a %macro statement. For example:

 

%let app=hello;

%macro analyze_byvar_&&app.;

.

.

.

%mend analyze_byvar_&&app.;

 

However, when I invoke the macro the name does not resolve.

 

Any suggestions?

 

 


Accepted Solutions
Solution
‎08-24-2017 01:16 PM
PROC Star
Posts: 283

Re: &macro statement

Posted in reply to CurtisSmithDCAA

names the macro. A macro name must be a SAS name, which you supply; you cannot use a text expression to generate a macro name in a %MACRO statement. In addition, do not use macro reserved words as a macro name.

View solution in original post


All Replies
Solution
‎08-24-2017 01:16 PM
PROC Star
Posts: 283

Re: &macro statement

Posted in reply to CurtisSmithDCAA

names the macro. A macro name must be a SAS name, which you supply; you cannot use a text expression to generate a macro name in a %MACRO statement. In addition, do not use macro reserved words as a macro name.

Super User
Posts: 5,513

Re: &macro statement

Posted in reply to CurtisSmithDCAA

The reference to a macro variable works when invoking a macro.  This should be fine:

 

%analyze_byvar_&app.

 

However, it doesn't work when naming a macro.

Super User
Posts: 11,343

Re: &macro statement

Posted in reply to CurtisSmithDCAA

In almost any approach that I can imagine whatever you may be attempting it would likely be much better to have a single macro and have the value you are calling APP as a parameter to the macro. Since you do not show any parameters I suspect you need to delve a little further into macro language documentation.

 

If you had shown what that macro is supposed to actually do we might make more constructive comments.

 

Show us log where you compiled the macro. I suspect that it did not compile.

PROC Star
Posts: 7,480

Re: &macro statement

Posted in reply to CurtisSmithDCAA

@CurtisSmithDCAA: I disagree! If you need to create a macro whose name includes a macro variable you could always simply create it as an include file. e.g.:

 

%let app=hello;
filename testit '/folders/myfolders/testit.sas';
data _null_;
  length forinc $80;
  file testit;
  forinc=catt('%macro analyze_byvar_',"&app",';');
  put forinc;
  put 'x=1;';
  forinc=catt('%mend analyze_byvar_',"&app",';');
  put forinc;
run;

%include testit;
data test;
  input y;
  %analyze_byvar_hello;
  cards;
1
2
3
;

Art, CEO, AnalystFinder.com

 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 4 replies
  • 125 views
  • 5 likes
  • 5 in conversation