Date: Wed, 22 Dec 1999 20:33:33 +0000
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Roland <roland.rashleigh-berry@VIRGIN.NET>
Subject: Re: Return Values in a Macro
Content-Type: text/plain; charset=us-ascii
Walter Smith wrote:
> Rolands illustration is exactly on track!! Macro code - even in a data
> step - resolves to text, generally text that is SAS code - data step or
> proc step.
> Data step macro example:
> %macro boxcox(x,lambda);
> (%str(&x)**&lambda -1)/&lambda
> data a;
> set b;
> zz = %boxcox(xx,.7);
> The above would resolve to:
> data a;
> set b;
> zz = (xx**.7 -1)/.7;
> The resolved text then goes to the compiler to compile the data step
> and the macro processor is out of the picture. While the data step is
> executing (processing observations), the macro processor is NOT active.
> Perhaps the most common beginners error in writing macro functions, is
> including SAS statements that the beginner expects to execute before
> the macro terminates. Any non-macro statements resolve to text.
> Rolands example stands as an example of a macro function.
> An example of the above error is:
> %macro mymacro(parm);
> options mprint;
> %if &parm=X %then RED;
> %if &parm=Y %then BLUE;
> %if %mymacro(X)=RED %then %do;
> /*more stuff*/
> The above macro function would never resolve to the values RED or BLUE
> because the SAS options statement is included in the resolution.
> Instead the above invocation would resolve (in part) as:
> %if options mprint; RED=RED %then %do;...%end;
> Not what was expected or desired.
> Hope this is useful - :)
Yep. Macros reduce themselves to whatever the macro code reduces itself
to. If you want that value from the macro such as
then all your %words macro has to do is put the answer you want in a
macro variable and then resolve it right at the end without a ";" after
it. What you get is the text of the answer so you effectively get
%let blah, blah blah;
You can write functions that way where the macro substitutes complex SAS
Pub time, I think.