DATA Step, Macro, Functions and more

Assign a macro variable inside a macro.

Reply
N/A
Posts: 0

Assign a macro variable inside a macro.

What is wrong with this stupid example:

%macro test;
%let x=10;
%mend;

%test;
%put &x;

In the log I get (after the code listing):

WARNING: Apparent symbolic reference X not resolved
&x

I expected to get the value 10.

Don't ask me why I want to assign a macro variable inside a macro.
Super User
Posts: 5,424

Re: Assign a macro variable inside a macro.

Posted in reply to deleted_user
Assigning av macro variable inside a macro creates a variable that is local to the macro.
To be able to refer to it outside the macro you need to add a %global before the assignment.

/Linus
Data never sleeps
Super Contributor
Super Contributor
Posts: 3,174

Re: Assign a macro variable inside a macro.

Some useful reading from the SAS support http://support.sas.com/ website for you to consider.

Scott Barry
SBBWorks, Inc.


SAS Macro Language: Reference - Scopes of Macro Variables
http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/a002047080.htm
Super Contributor
Super Contributor
Posts: 3,174

Re: Assign a macro variable inside a macro.

** SAS forum burp - duplicate removed **
Super Contributor
Posts: 281

Re: Assign a macro variable inside a macro.

Posted in reply to deleted_user
If you move the %put statement inside the macro, everything works.

Another responder has explained how %global also fixed the problem, but I would recommend instead of %global, you do all macro commands involving your macro variables INSIDE the macro.
SAS Employee
Posts: 58

Re: Assign a macro variable inside a macro.

Posted in reply to deleted_user
Exactly. And there are some good examples of how it works in the doc link that Scott posted, above.
Ask a Question
Discussion stats
  • 5 replies
  • 179 views
  • 0 likes
  • 5 in conversation