Date: Mon, 27 Oct 2003 09:26:28 +0100
Reply-To: "Groeneveld, Jim" <jim.groeneveld@VITATRON.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Groeneveld, Jim" <jim.groeneveld@VITATRON.COM>
Subject: Re: Yet another "rename" query
Content-Type: text/plain; charset="iso-8859-1"
Hi AxN,
Fist of all, could you perhaps be a little less anonymous? It would be nice to know who to talk to.
What is it exactly that SAS is complaining about? You should have sent that part of your log as well. Anyway, your code seems somewhat illogical and redundant. You could at least simplify the %DO loop as follows:
%do %while (&word ne %str()) ;
%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 ;
And as far as the data step DO loop concerns: that is an illegal way to rename variables, you can not rename array elements, but should rename real variables or variable lists instead. Thus your code
array old(*) &myvar ;
array new(*) &newvar ;
%put myvar = "&myvar", newvar = "&newvar" ;
do jj = 1 to dim(old) ;
rename old(jj) = new(jj) ;
end ;
could be changed into:
RENAME &MyVar = &NewVar;
Or even better, change the whole data step
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 ;
into
data &outdata (RENAME=(&MyVar = &NewVar));
set &indata ;
run ;
Regards - Jim.
--
. . . . . . . . . . . . . . . .
Jim Groeneveld, MSc.
Biostatistician
Science Team
Vitatron B.V.
Meander 1051
6825 MJ Arnhem
Tel: +31/0 26 376 7365
Fax: +31/0 26 376 7305
Jim.Groeneveld@Vitatron.com
www.vitatron.com
I wish I had a webcam to show you the boring view from my window.
[common disclaimer]
-----Original Message-----
From: AxN [mailto:axn00@HOTMAIL.COM]
Sent: Monday, October 27, 2003 07:11
To: SAS-L@LISTSERV.UGA.EDU
Subject: Yet another "rename" query
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 ;