Date: Thu, 5 Oct 2000 14:33:52 -0700
Reply-To: "Lund, Pete" <Peter.Lund@CFC.WA.GOV>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: "Lund, Pete" <Peter.Lund@CFC.WA.GOV>
Subject: renaming variables macro
Content-Type: text/plain; charset="iso-8859-1"
A number of people have requested the "full" version of the rename macro I
posted earlier. This one allows for both a suffix and a prefix to be
declared and does a bit more error checking. I also made a little change
that eliminates one extraneous step. I hope that the comments make it
clear. If you care to use it, please let me know if you find any bugs.
Thanks much.
Please excuse any indentation problems - cutting and pasting from the
enhanced editor into Outlook does not always keep things the "way they
were."
/* Macro: RenameList */
/* Programmer: Pete Lund */
/* Date: September 2000 */
/* Purpose: Renames all the variables in a dataset by */
/* adding a prefix and/or suffix to the existing */
/* variable names. */
/* */
/* Note: V7+ only - could be used w/ V6 if the name of */
/* the macro and a number of the macro variables */
/* were changed but the 8 character limit makes */
/* it more of a challenge to add characters to */
/* existing names without being too long. */
/* */
/* Parameters: */
/* DSN - the name of the dataset (libref.member) */
/* PRE - prefix attached to existing variable names */
/* SUF - suffix to add to the end of variable names */
/* EXCEPT - variables to exclude - separate with spaces */
/* */
/* Example: */
/* Dataset TEST1 contains variables: id, age, sex */
/* Dataset TEST2 contains variables: id, age, sex */
/* */
/* data combine; */
/* merge test1(%RenameList(dsn=test1,suf=_1,except=id)) */
/* test2(%RenameList(dsn=test1,suf=_2,except=id));*/
/* by id; */
/* run; */
/* */
/* Dataset COMBINE contains variables: */
/* id, age_1, sex_1, age_2, sex_2 */
%macro RenameList(dsn=,pre=,suf=,except=);
%local i dsid varlist numvars numexcept;
/* Make sure that the dataset exists */
%if %sysfunc(exist(&dsn)) eq 0 %then
%do;
%put WARNING: (RenameList) &DSN does not exist.;
%let varlist = ;
%goto Quit;
%end;
/* Make sure that a prefix or suffix was passed. */
%if &Pre eq %str() and &Suf eq %str() %then
%do;
%put WARNING: (RenameList) A prefix and/or suffix value must be
passed.;
%let varlist = ;
%goto Quit;
%end;
/* If it does exist, open the dataset. */
%let dsid=%sysfunc(open(&dsn,i));
/* Look through the variables in the dataset and create a macro*/
/* variable called VarList. It will contain rename pairs for */
/* all the variables in the dataset, adding the passed prefix */
/* and/or suffix. */
%let NumVars = %sysfunc(attrn(&dsid,nvars));
%do i = 1 %to &NumVars;
%let varlist = &varlist%str(
)%sysfunc(varname(&dsid,&i))=&Pre.%sysfunc(varname(&dsid,&i))&Suf;
%end;
/* Close the dataset. */
%let rc = %sysfunc(close(&dsid));
/* Clean out the variables that are not to be renamed. */
%if %length(&except) gt 0 %then
%do;
/* Upper case everything so there is no inadvertant misses. */
%let VarList = %upcase(&VarList);
%let Except = %upcase(&Except);
%let Pre = %upcase(&Pre);
%let Suf = %upcase(&Suf);
/* We want to count how many hits we get (Found) and how many */
/* Except= variables there were (NumExcept). When we're done, if */
/* these two variables don't have the same value, then we know a */
/* variable that doesn't exist in the dataset was passed in the */
/* Except= list. */
%let Found = 0;
%let NumExcept = %eval((%length(&except) -
%length(%sysfunc(compress(&except,%str( )))) + 1);
/* Loop through all the Except= variables. Set up a string (remove) */
/* that contains the "oldname=newname" value for that variable and */
/* convert it to a blank space with the TRANWRD function. Check the */
/* value of VarList before and after the remove operation. If it */
/* changed, increment the Found counter. */
%do i = 1 %to &NumExcept;
%let remove = %scan(&except,&i,%str(
))=&pre.%scan(&except,&i,%str( ))&Suf;
%let PreRemove = &VarList;
%let varlist = %sysfunc(tranwrd(&varlist,&remove,%str()));
%if %quote(&PreRemove) ne %quote(&VarList) %then %let Found =
%eval(&Found + 1);
%end;
/* If excepted variables are passed and do not exist in the dataset, */
/* write a warning to the log and reset the varlist to null. */
%if &Found ne &NumExcept %then
%do;
%put WARNING: (RenameList) Except list contains variables not in
the dataset.;
%put WARNING: (RenameList) Rename aborted.;
%let varlist = ;
%goto Quit;
%end;
%end;
%if &varlist ne %str( ) %then %let VarList = RENAME=(&VarList);
%Quit:
/* Write out the variable name list. */
&varlist
%mend;