Date: Fri, 19 Jun 2009 11:04:38 -0700
Reply-To: Chris Neff <caneff@GMAIL.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Chris Neff <caneff@GMAIL.COM>
Organization: http://groups.google.com
Subject: Re: Suppress macro warning temporarily? Or how do I write this
Content-Type: text/plain; charset=ISO-8859-1
Thanks everyone.
Chang:
I really appreciate the ideas. I think I'll go with the second case,
because in either case the user is going to have to do something
slightly
different for a macro variable array than another one anyway, so I
might as
well avoid stuff like %nrstr if I can. Thanks for all the help,
-Chris
On Jun 19, 12:08 pm, chang_y_ch...@HOTMAIL.COM (Chang Chung) wrote:
> On Thu, 18 Jun 2009 13:02:08 -0700, Chris Neff <can...@GMAIL.COM> wrote:
> >I have written a macro to be able to calculate the dot product between
> >two different arrays. i.e. I call
> >"%dot_prod(x,y,n=3) "
> >And it replaces that macro call with "x1*y1 + x2*y2 + x3*y3".
>
> >However I would also like to be able to use it in the case where one
> >of the arrays is an array of macro variables, i.e. like
> > "x1 * &beta1 + x2 * &beta2 + x3 * &beta3"
> >I can do this easily with my macro by calling
> > "%dot_prod(x, &beta, n=3)"
> >However, because &beta doesn't get resolved until somewhere in the
> >macro, I get this warning in the log:
> > "WARNING: Apparent symbolic reference BETA not resolved."
> >Any ideas about either a) writing the macro better to maintain
> >flexibility but not get the warning, or b) just be able to suppress
> >that specific warning for those macro calls? I would hate to turn off
> >warnings all together.
> > %macro dot_prod(x, y, n);
> > &&x.1 * &&y.1
> > %do i = 2 %to &n;
> > + &&x.&i * &&y.&i
> > %end;
> > %mend dot_prod;
>
> Hi,
>
> A minor correction first. The macro arguments are resolved before their
> values are passed into the macro. So, if you make a macro call like:
> %dot_prod(&best), then the warning occurs even before any lines inside of
> the macro is executed. Thus, it is impossible to pass "&best" without some
> macro quoting, and the reponsibility of quoting it appropriately is on the
> user of the macro, not the macro author. (macro author has to document this,
> though)
>
> Interesting is the idea of making a macro so that it can accept either: (1)
> a literal prefix, and (2) the macro array name. Chris used a novel macro
> expression: (&&x.&i) and was able to do this, except a warning message. In
> close inspection, however, reveals that this is equivalent to the simpler
> form, (&x.&i).
>
> Given above two, we can have below, which works without a warning:
> %macro dot_prod(x, y, n);
> %unquote(&x.1)*%unquote(&y.1)
> %do i = 2 %to &n;
> %*; + %unquote(&x.&i)*%unquote(&y.&i)
> %end;
> %mend dot_prod;
> /* case 1: where x and y are just prefixes */
> %put %dot_prod(x,y,2);
> /* on log
> x1*y + x2*y2
> */
> /* case 2: when x is the & followed by macro array root name. */
> /* notice that we need some macro quoting */
> %symdel beta; /* make sure we have no macro var, beta */
> %let beta1=aa; /* these are two elements of the macro array, beta */
> %let beta2=bb;
> %put %dot_prod(%nrstr(&beta), y, 2); %*-- ! --*;
> /* on log
> aa*y1 + bb*y2
> */
>
> For me, however, this is a tad too cute and too much burden on the user's
> side. An alternative is to make separate parameters: one pair for just
> prefixes, another pair for the macro array root names, something like:
> %macro dot_prod(x=x, y=y, n=1, xarr=, yarr=);
> %local i plus;
> %do i = 1 %to &n;
> %if &i>1 %then +;
> %if %superq(xarr)= %then &x&i; %else &&&xarr.&i;
> %*;*
> %if %superq(yarr)= %then%*;&y&i; %else%*;&&&yarr.&i;
> %end;
> %mend dot_prod;
> /* case one */
> %put NOTE: %dot_prod(x=x,y=y,n=2);
> /* case two */
> %symdel beta;
> %let beta1=aa;
> %let beta2=bb;
> %put NOTE: %dot_prod(xarr=beta, n=2);
> /* on log
> NOTE: x1*y1 + x2*y2
> NOTE: aa*y1 + bb*y2
> */
>
> Hope this helps a bit.
> Cheers,
> Chang- Hide quoted text -
>
> - Show quoted text -
|