Hi all,
Need some help in rename the variables.
I am going to add prefix ahead of each variable,eg (listed below the sample dataset): rename C2 to SC2; C10_1 to SC10_1...; I have 618 variables in my dataset,all of them need to add the same prefix ahead the original variable name.
The name of my dataset is 'ru2014';
Thanks.
Jing Yuan
id C2 C10_1 C11_1 C12_1 C13_1 C14_1
011039 2 1 2 2 2 2
011040 2 2 2 2 2 2
011042 2 2 2 2 2 2
011042 2 2 2 2 2 2
011044 2 2 2 2 2 2
How to rename variables is a question regularly asked. Searching the communities brought up quite a few hits for me. Below code is based on what suggested here
data work.have;
set sashelp.class;
run;
proc sql noprint ;
select cats(name,'=S',name) into :renames separated by ' '
from dictionary.columns
where libname='WORK' and memname='HAVE'
;
quit;
proc datasets nolist library=work;
modify have;
rename &renames;
run;
quit;
Another solution by @Steelers_In_DC:
I can't test it but this should work:
data have;
infile cards dsd;
input id $ C2 C10_1 C11_1 C12_1 C13_1 C14_1;
cards;
011039,2,1,2,2,2,2
011040,2,2,2,2,2,2
011042,2,2,2,2,2,2
011042,2,2,2,2,2,2
011044,2,2,2,2,2,2
;
run;
proc sql;
CREATE TABLE TEST1 AS
select trim(name) || '=' || 'S' || name
into :renamelist separated by ' ' from
(select name
from dictionary.columns
where libname='WORK' and memname='HAVE' and upcase(name) in
(select upcase(name)
from dictionary.columns
where libname='WORK' and memname='HAVE' and upcase(name) ne 'ID'));
quit;
%PUT &renamelist;
data have;
set merge have test1(rename=(&renamelist));
by id;
run;
Do all of the variables you want to rename start with C? If so, are there any variables that start with C that you do not wish to rename?
If you have variables other than those that start with C, how do we determine which ones need to be renamed?
Maybe this approach is sufficient (or tells us what's missing):
Data ru2014;
Input id C2 C10_1 C11_1 C12_1 C13_1 C14_1;
Datalines;
011039 2 1 2 2 2 2
011040 2 2 2 2 2 2
011042 2 2 2 2 2 2
011042 2 2 2 2 2 2
011044 2 2 2 2 2 2
;
Run;
Proc SQL NoPrint;
Select Catt(Name,'=S',Name) Into :Rename_List Separated By ' ' From
(Select Name From Dictionary.Columns Where UpCase(Libname) eq "SAS_WORK" AND UpCase(MemName) eq "RU2014" And UpCase(Substr(Name,1,1)) eq "C"); * IMPORTANT: check your libname!!!;
Quit;
%Put **&Rename_List.**;
Data Want;
Set ru2014;
Rename &Rename_List.;
Run;
I can't test it but this should work:
data have;
infile cards dsd;
input id $ C2 C10_1 C11_1 C12_1 C13_1 C14_1;
cards;
011039,2,1,2,2,2,2
011040,2,2,2,2,2,2
011042,2,2,2,2,2,2
011042,2,2,2,2,2,2
011044,2,2,2,2,2,2
;
run;
proc sql;
CREATE TABLE TEST1 AS
select trim(name) || '=' || 'S' || name
into :renamelist separated by ' ' from
(select name
from dictionary.columns
where libname='WORK' and memname='HAVE' and upcase(name) in
(select upcase(name)
from dictionary.columns
where libname='WORK' and memname='HAVE' and upcase(name) ne 'ID'));
quit;
%PUT &renamelist;
data have;
set merge have test1(rename=(&renamelist));
by id;
run;
For renaming variables can I suggest to use Proc Datasets instead of a data step approach (which unnecessarily processes all the data and re-creates the target table )
How to rename variables is a question regularly asked. Searching the communities brought up quite a few hits for me. Below code is based on what suggested here
data work.have;
set sashelp.class;
run;
proc sql noprint ;
select cats(name,'=S',name) into :renames separated by ' '
from dictionary.columns
where libname='WORK' and memname='HAVE'
;
quit;
proc datasets nolist library=work;
modify have;
rename &renames;
run;
quit;
Another solution by @Steelers_In_DC:
I can't test it but this should work:
data have;
infile cards dsd;
input id $ C2 C10_1 C11_1 C12_1 C13_1 C14_1;
cards;
011039,2,1,2,2,2,2
011040,2,2,2,2,2,2
011042,2,2,2,2,2,2
011042,2,2,2,2,2,2
011044,2,2,2,2,2,2
;
run;
proc sql;
CREATE TABLE TEST1 AS
select trim(name) || '=' || 'S' || name
into :renamelist separated by ' ' from
(select name
from dictionary.columns
where libname='WORK' and memname='HAVE' and upcase(name) in
(select upcase(name)
from dictionary.columns
where libname='WORK' and memname='HAVE' and upcase(name) ne 'ID'));
quit;
%PUT &renamelist;
data have;
set merge have test1(rename=(&renamelist));
by id;
run;
Whilst the above posts give some excellent answers to the problem, I would ask why you have a dataset with columns that look like that. Would it not make your life easier to have column Names to be be of the same form and array-able, and then have the column Label have any display information, e.g.:
COL1 COL2 COL3 ...
"C2" "C10_1" "C11_1" ...
In this method you can then use array processing, and simple logic across variables without having to "know" the specific variable setup.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.