Date: Wed, 22 Dec 1999 20:33:33 +0000
Reply-To: roland.rashleigh-berry@virgin.net
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Roland <roland.rashleigh-berry@VIRGIN.NET>
Organization: N/A
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:
>
> <pre>
> %macro boxcox(x,lambda);
> (%str(&x)**&lambda -1)/&lambda
> %mend;
>
> data a;
> set b;
> zz = %boxcox(xx,.7);
> run;
>
> The above would resolve to:
>
> data a;
> set b;
> zz = (xx**.7 -1)/.7;
> run;
> </pre>
>
> 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:
>
> <pre>
> %macro mymacro(parm);
> options mprint;
> %if &parm=X %then RED;
> %if &parm=Y %then BLUE;
> %mend;
>
> %if %mymacro(X)=RED %then %do;
> /*more stuff*/
> %end;
> </pre>
>
> 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
%let word0=%words(&mstring);
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 word0=&answer;
%macro words(string);
%let blah, blah blah;
%let num=&answer;
&num
%mend;
You can write functions that way where the macro substitutes complex SAS
code.
Idiotically simple.
Roland
Pub time, I think.