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 (June 2004, week 4)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Sun, 27 Jun 2004 14:39:32 -0400
Reply-To:     Jim Groeneveld <jim.groeneveld@VITATRON.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Jim Groeneveld <jim.groeneveld@VITATRON.COM>
Subject:      Re: Trick for Mass Renaming of Variables
Comments: To: Kevin Myers <KevinMyers@AUSTIN.RR.COM>

Hi Kevin,

Just a few (*untested*) suggestions:

1. get a list of current names in a dataset a. from PROC CONTENTS DATA=... OUT=... (KEEP=Name), b. or PROC DATASETS, c. or via SQL d. or SASHELP.

2.a. write an ascii file via FILE with lines of type: 'oldname = newname' and %INCLUDE that file later after the RENAME keyword in PROC DATASETS (don't forget the double semicolon). 2.b. make macro variable list of old names and one of new names using: NameList = /* initialize empty */; CALL EXECUTE ('%LET NameList = &NameLIST ' || Name || ';'); from a data step. In PROC DATASETS do something like: RENAME %LET NR = 1; %LET OldName = %SCAN (%NameList, ^Nr, %STR( )); /* %LET NewName = %SCAN (%NameList, ^Nr, %STR( )); */ %DO %WHILE (&OldName NE AND &NewName NE); &OldName = &NewName /* &NewName may be COL&Nr */ %END; ; /* semicolon ends RENAME */ 2.c. or build the whole PROC DATASETS call from the data step reading the names, thus IF _N_ EQ 1 THEN build the who part intil the RENAME and the first oldname = newname specification. only that specification for the remaining variable names and adding the closing code after the last one.

I assume you know methods to specify a constant character prefix (COL) and a variable numeric postfix, e.g. using an incremental macro variable.

There may be numerous other ways to do it, maybe even quicker, more efficient, maybe all within SQL.

BTW, doesn't RENAME _CHARACTER_ _NUMERIC_ = .............. help?

Regards - Jim (via the web interface).

On Sun, 27 Jun 2004 10:33:32 -0500, Kevin Myers <KevinMyers@AUSTIN.RR.COM> wrote:

>Hello, > >I'm looking for something simple and elegant that will allow all variables >of a data set to be renamed to a list of generic names with a given prefix >and a numeric suffix, e.g. if there are 100 variables in the data set and >the prefix is "col" then the variables should be renamed col1-col100. >Conceptually what I want to do is something similar to the following data >step pseudo-code: > >rename _all_=col1-col100; > >However, there are several problems with that: > >1. Using a data step requires all of the data to be read and re-written, >when all that should be necessary is to modify the data set's variable >descriptor info. > >2. The variable list _all_ includes automatic variables that aren't actually >part of the data set variables that need to be changed, such as _N_. > >Alternatively, I could use PROC TRANSPOSE to transpose the data then >transpose it back (using appropriate parameters and statements for the >proc), which would have the desired effect but would be extremely slow, and >would required reading and re-writing the data multiple times. > >I have considered (started to write) a macro for this purpose using PROC SQL >to get a list of column names and then to loop over that list generating >RENAME statements for PROC DATASETS. I'm confident that I can get that >approach to work fairly quickly, but it seems like there ought to be a >simpler way... BTW, PROC DATASETS doesn't seem to allow variable list >syntax in its RENAME statement. > >Can anyone think of a simpler approach? > >Thanks, >s/KAM


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