Hi Experts,
I tried to find by reading lot of stuff for renaming multiple variables dynamically but I couldn't find right solution for it.
In my dataset I have 159 variables named in short form (ex. AR_1) and I want to rename all the variables with there respective longforms.
Good news is I have a KEY dataset which is having short and long form names in vertical format for all of the (i.e. two columns short and long form for all 159 variables (which are observations in this dataset) ).
I tried using transpose and applying a user defined format(created using KEY dataset) but I want to know whether there is any way we can rename variables dynamically using some macro or some proc sql methods.(ex. in case if I dont have KEY dataset).
Thanks.
Harshad.
Do your long form variables meet SAS variable naming restrictions, i.e. 32 characters or less, no spaces, and starts with character?
If they don't you'll want to set them as labels instead.
If so I usually do something like the following:
proc sql noprint;
select catx("=", old_name, new_name)
into :rename_list separated by " "
from key_dataset;
quit;
proc datasets library=work;
modify my_data;
rename &rename_list;
run;
Hi, Harshad
Give this a try;
Tom
data NameLookup;
length OldName NewName $16;
input OldName NewName;
cards;
Name XX_Name
Sex YY_Sex
Age II_Age
Height AA_Height
Weight BB_Weight
run;
data _null_;
set NameLookup end=LastRec;
if _n_ = 1 then
call execute('proc sql; create table NewClass as select ');
if ^LastRec then
call execute(Oldname || ' as ' || NewName || ',');
else call execute(Oldname || ' as ' || NewName);
if LastRec then
call execute(' from sashelp.class; quit;');
run;
Of course, if you just want to modify the original file, you could use 's approach to use proc datasets:
/* create a dataset for testing */
data have;
set sashelp.class;
run;
data _null_;
set NameLookup end=LastRec;
if _n_ = 1 then
call execute('proc datasets library=work nolist; modify have;');
call execute('rename '|| Oldname || '=' || NewName ||';');
if LastRec then call execute('quit;');
run;
Do your long form variables meet SAS variable naming restrictions, i.e. 32 characters or less, no spaces, and starts with character?
If they don't you'll want to set them as labels instead.
If so I usually do something like the following:
proc sql noprint;
select catx("=", old_name, new_name)
into :rename_list separated by " "
from key_dataset;
quit;
proc datasets library=work;
modify my_data;
rename &rename_list;
run;
Why not uis the variable label for the long names,
Keeps as easier coding for the programmer and having the longer name on all type of reports.
All answers are correct and exactly what I was seeking.
Thanks everyone.
Cheers ...!!!
Harshad.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.
Find more tutorials on the SAS Users YouTube channel.