Date: Thu, 9 Dec 2004 21:22:44 -0600
Reply-To: "Dunn, Toby" <Toby.Dunn@TEA.STATE.TX.US>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Dunn, Toby" <Toby.Dunn@TEA.STATE.TX.US>
Subject: Re: Assembled Macro Variables
Content-Type: text/plain; charset="iso-8859-1"
Rob,
Dennis is a smart guy and knows his stuff... and his fix worked. I ran the same code below and recieved no error just a line in the log with 'junk'. Shut down your SAS session and try the macros with Dennis suggestions and it should work like a charm.
Toby
________________________________
From: SAS(r) Discussion on behalf of Rob Rohrbough
Sent: Thu 12/9/2004 7:49 PM
To: SAS-L@LISTSERV.UGA.EDU
Subject: Re: Assembled Macro Variables
Dennis,
Thanks, I thought tried every combination of ampersands. Here is what I got
with your suggestion:
1 %macro one(x);
2 %global one&x;
3 %let one&x=junk;
4 %mend;
5 %macro two(x);
6 %put &&one&x;
7 %symdel one&x;
8 %mend;
9 %one(test);
10 %two(test);
junk
WARNING: Attempt to delete macro variable ONE failed. Variable not found.
ERROR: Macro variable name & must start with a letter or underscore.
WARNING: Attempt to delete macro variable X failed. Variable not found.
11 %put &onetest;
junk
12 %symdel onetest;
It appears that the reference in the %PUT statement works. However, what
works for the &GLOBAL and &LET statements does not work for the %SYMDEL
statement. I would expect the same syntax to work for the &SYMDEL statement
since the ampersand is not required in any of the three statements. I don't
know if this is intended behavior or if SI just made a mistake implementing
the newer statement. I will put the question to the Institute.
Thanks again,
Rob
-----Original Message-----
From: Dennis Diskin [mailto:diskin@snet.net]
Sent: Thursday, December 09, 2004 6:32 PM
To: Rob Rohrbough; SAS-L@LISTSERV.UGA.EDU
Subject: Re: Assembled Macro Variables
Hi Rob,
I've often gotten confused myself, but I think your problem is in the
%put &one&&x;
You should use
%put &&one&x;
to generate the indirect reference. Thgik of it as &(&one&x) although the
parens won't work.
HTH,
Dennis Diskin
Rob Rohrbough <Rob@ROHRBOUGH-SYSTEMS.COM> wrote:
Here is one for you macro mavens:
I would like to pass a value from one macro to another in a global macro
variable generated with a parameter the user passes. Thankfully I can
generate the macro variable. However, I can read it only via a SYMGET
function from a datastep in the second macro. I will show you two logs, the
first of which works as a work-around:
23 %macro one(x);
24 %global one&x;
25 %local onotes;
26 %let onotes=%sysfunc(getoption(notes, keyword));
27 options nonotes;
28 data _null_;
29 call symput("one&x", "junk");
30 run;
31 options &onotes;
32 %mend;
33 %macro two(x);
34 %local onotes;
35 %let onotes=%sysfunc(getoption(notes, keyword));
36 options nonotes;
37 data _null_;
38 length x $10;
39 x = symget("one&x");
40 put x=;
41 run;
42 options &onotes;
43 %mend;
44 %one(test);
45 %two(test);
X=junk
Notice that I can assemble the name not only in the datastep call routine
and function, but also in the %GLOBAL statement. Without being able to
assemble it in the %GLOBAL statement, I do not believe this would work; the
first datastep would produce a local macro variable. I hope that still
works in v9! Here is what I would like to be able to do:
61 %macro one(x);
62 %global one&x;
63 %let one&x=junk;
64 %mend;
65 %macro two(x);
66 %put &one&&x;
67 %symdel one&x;
68 %mend;
69 %one(test);
70 %two(test);
WARNING: Apparent symbolic reference ONE not resolved.
&onetest
WARNING: Attempt to delete macro variable ONE failed. Variable not found.
ERROR: Macro variable name & must start with a letter or underscore.
WARNING: Attempt to delete macro variable X failed. Variable not found.
71 %put &onetest;
junk
72 %symdel onetest;
As you can see, syntax similar to the %GLOBAL statement works for the %LET
statement. However, I cannot come up with syntax that works to reference
the generated variable in the %PUT and %SYMDEL statements. As you can see
from the hard-coded %PUT and %SYMDEL statements in open code, the macro
definition worked. However, to generalize the second macro, I cannot
hard-code the global macro variable name. Can you help?
TIA,
Rob
Rob Rohrbough, Consultant Rohrbough Systems Design, Inc.
SAS Certified Professional V6/V8 A SAS Alliance Consulting Partner
robREMOVECAPS@FORNOSPAMrohrbough-systems.com Omaha, NE