LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (May 2004, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Wed, 5 May 2004 08:42:50 -0400
Reply-To:     Quentin McMullen <quentin_mcmullen@BROWN.EDU>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Quentin McMullen <quentin_mcmullen@BROWN.EDU>
Subject:      Re: Help: Macro variable resolution

On Tue, 4 May 2004 17:40:12 -0400, Chang Y. Chung <chang_y_chung@HOTMAIL.COM> wrote:

>On Tue, 4 May 2004 21:07:38 +0000, Steve Jones <st_jones77@YAHOO.COM> >wrote: > >>Hi everyone, >>I am puzzled while the following little macro doesn't work. >>The problem is with the resolution of &&vic&i.&postfix. >>I noticed that if I change the order to &postfix.&&vic&i >>everything works fine. >>I'd appreciate any pointers. >>Below is an example data set, the macro itself, and how >>I called it. >> >>Thanks for all pointers. >> >>Steve. >>====================================================================== >> >>data zz; >> input x1 x2 x3 y; >> datalines; >> 1 2 3 4 >> 4 5 6 7 >> 7 8 9 3 >> ; >>run; >> >>%macro renamvar(indata=, outdata=, vars=, depvar=, postfix=); >> %let i = 1; >> %do %while (%scan(&vars, &i) ne %str() ); >> %let vic&i = %scan(&vars, &i); >> %let i = %eval(&i+1); >> %end; >> %let lastvar = %eval(&i-1); >> >> data &outdata(keep= &depvar >> %do i = 1 %to &lastvar; >> &&vic&i.&postfix. >> %end; >> ); >> set &indata; >> rename >> %do i = 1 %to &lastvar; >> &&vic&i = &&vic&i.&postfix. >> %end;; >> run; >>%mend renamvar; >> >>%renamevar( indata = zz, >> outdata = yy, >> vars = x1, >> depvar = respondr, >> postfix = _c >> ); >> >> >> >> >>-- >>Posted via Mailgate.ORG Server - http://www.Mailgate.ORG > > >Hi, Steve, > >I found three errors and the fixes: >(1) when you call the macro, use the correct name, %renamvar, with no "e"; >(2) when you call the macro, make sure the depvar is in the indata; >(3) use one more dot in the construct like "&&vic&i..&postfix.", >not "&&vic&i.&postfix." > >Cheers, >Chang

Hi Steve,

I agree with Chang. Just to explain a bit more (because I didn't catch it until I read Chang's note), when you code: &&vic&i.&postfix. and i=1 and postfix=_c, you get:

&&vic&i.&postfix. -> &vic1_c -> SAS tries to resolve &vic1_c and can't find it. The important point is that the . you put after &i gets eaten when SAS resolves &i (this is a good thing).

But if you add the second dot, you get: &&vic&i..&postfix. -> &vic1._c -> x1_c

Note that the dot after &postfix is unnecessary; I tend to leave it off, but many people prefer to include it as a matter of coding style.

Kind Regards, --Quentin


Back to: Top of message | Previous page | Main SAS-L page