BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
5 REPLIES 5
LinusH
Tourmaline | Level 20
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
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
** SAS forum burp - duplicate removed **
Paige
Quartz | Level 8
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.
PatrickG
SAS Employee
Exactly. And there are some good examples of how it works in the doc link that Scott posted, above.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 834 views
  • 0 likes
  • 5 in conversation