Date: Tue, 10 Jun 2008 05:38:56 -0700
Reply-To: karma <dorjetarap@GOOGLEMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: karma <dorjetarap@GOOGLEMAIL.COM>
Organization: http://groups.google.com
Subject: Re: Macro variable not resolving outside macro program
Content-Type: text/plain; charset=ISO-8859-1
On 10 Jun, 12:01, RolandRB <rolandbe...@hotmail.com> wrote:
> On Jun 10, 12:44 pm, guya.carpen...@gmail.com wrote:
>
>
>
>
>
> > The macro program below works. The %put within the program shows that
> > a macro variable (buzz1 buzz2 etc) is created with the data that I
> > want.
>
> > However, the %puts outside the macro program do not work. Can anybody
> > see a silly error I have made with this?
>
> > Thanks.
>
> > %macro buzz (randno,var);
> > data _null_;
> > set buzzwords (where=(count=&&rand&randno));
> > call symputx("buzz&randno",&var);
> > run;
> > %put !BUZZ&randno! = !&&buzz&randno!;
> > %mend buzz;
> > %buzz (1,first)
> > %buzz (2,second)
> > %buzz (3,third)
> > %buzz (4,fourth);
>
> > %put &buzz1 &buzz2 &buzz3 &buzz4..;
>
> You have not made a mistake. This is due to the scope of the macro
> variable. The macro variables created inside a macro are not
> accessible outside the macro unless you declare them as "global".- Hide quoted text -
>
> - Show quoted text -
I was convinced that you symput created global variables by default so
I gave this a test.
The rules that call symput uses to decide whether to create a macro
variable in the global or local symbols table seems a bit fuzzy --
unless explicitly declared.
For example:
%macro test;
data _null_;
call symput("tests1",5);
run;
%put _user_;
%mend test;
%test;
shows in the log:
GLOBAL TESTS1 5
However, if the name for the macro variable created in call symput is
created from a local macro variable, then the resulting macro variable
seems to be created in the local symbols table too. :
%macro test;
%let i=2;
data _null_;
call symput("tests&i",5);
run;
%put _user_;
%mend test;
%test;
log:
TEST TESTS2 5
TEST I 2
Consequently if it is comprised (soley) of a global macro variable,
then call symput creates it as a global:
%macro test;
data _null_;
call symput("tests&i",5);
run;
%put _user_;
%mend test;
%let i=3;
%test;
GLOBAL TESTS3 5
If the variable name consists of both a local and a global macro
variable, then it takes the scope of the local variable.
%macro test;
%let x=2;
data _null_;
call symput("tests&i&x",5);
run;
%put _user_;
%mend test;
%let i=3;
%test;
log:
TEST TESTS32 5
I couldn't find any documentation from SAS to confirm this, but it
does seem that the best safeguard is to declare the scope explicitly
anytime you use call symput. Anyone know anything about this?