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 (October 2003, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Sun, 26 Oct 2003 22:10:49 -0800
Reply-To:     AxN <axn00@HOTMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         AxN <axn00@HOTMAIL.COM>
Organization: http://groups.google.com
Subject:      Yet another "rename" query
Content-Type: text/plain; charset=ISO-8859-1

Hello everyone,

I have been looking through the archives for useful tips, but I don't seem to be able to adapt all the great information I found in my own problem.

I am trying to rename some varaibles in a data set, by adding a prefix (x_ in the example below). Since I seemed to be doing this somewhat frequently, and since there was no underlying pattern, I though that this would be a good way to learn macros, and wrote the following macro and example program. SAS (Version 8.12 on UNIX, I run these in batch mode) complains about the do loop, I think - I can't make out exactly where the problem lies, and would appreciate any suggestions.

AxN ======================================= %macro mymacro(indata,outdata,myvar) ; %let newvar = ; %let count = 1 ; %let word = %qscan(&myvar, &count) ; %let newword = %str(x_&word) ; %let newvar = %str(&newword) ; %let count = %eval(&count + 1) ; %put word = "&word", newword = "&newword", newvar = "&newvar", count = "&count" ; %do %while (&word ne %str()) ; %let word = %qscan(&myvar, &count) ; %if &word ne %str() %then %do ; %let newword = %str(x_&word) ; %let newvar = %str(&newvar &newword) ; %let count = %eval(&count + 1) ; %put word = "&word", newword = "&newword", newvar = "&newvar", count = "&count" ; %end ; %end ; %global mycount ; %let mycount = &count ; %put myvar = "&myvar", newvar = "&newvar", count = "&count", mycount = "&mycount" ;

data &outdata(drop=jj) ; set &indata ; array old(*) &myvar ; array new(*) &newvar ; %put myvar = "&myvar", newvar = "&newvar" ; do jj = 1 to dim(old) ; rename old(jj) = new(jj) ; end ; run ;

%mend mymacro ;

data first ; old1 = 1 ; old2 = 2*old1 + rannor(20031023) ; old3 = 3*old2 + rannor(20031024) ; old4 = 0 ; run ;

%mymacro(first, second, old1 old2 old3)

proc contents data=first ; run ;

proc contents data=second ; run ;


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