Hi all,
Can someone help me. I want to rename all the variables in a dataset that have a certain value in the name. I have hundreds of variables. For example. address2018, city2018, state2018. Is there anyway I can change them all to address2019, city2019, state2019.
Thanks in advance,
Jenny
Hi and welcome to the SAS Communities 🙂
Here is one way
data somedata;
input address2018 $ city2018 $ state2018 $ var1-var3;
datalines;
a b c 1 2 3
a b c 1 2 3
a b c 1 2 3
a b c 1 2 3
a b c 1 2 3
a b c 1 2 3
;
data _null_;
set sashelp.vcolumn end=lr;
where libname='WORK' and memname='SOMEDATA';
if _n_ = 1 then
call execute('proc datasets lib=work nolist; modify somedata;');
if find(name, '2018') ne 0 then
call execute('rename '|| name || '=' || tranwrd(name, '2018', '2019') ||';');
if lr then call execute('quit;');
run;
proc contents data=somedata;run;
Hi and welcome to the SAS Communities 🙂
Here is one way
data somedata;
input address2018 $ city2018 $ state2018 $ var1-var3;
datalines;
a b c 1 2 3
a b c 1 2 3
a b c 1 2 3
a b c 1 2 3
a b c 1 2 3
a b c 1 2 3
;
data _null_;
set sashelp.vcolumn end=lr;
where libname='WORK' and memname='SOMEDATA';
if _n_ = 1 then
call execute('proc datasets lib=work nolist; modify somedata;');
if find(name, '2018') ne 0 then
call execute('rename '|| name || '=' || tranwrd(name, '2018', '2019') ||';');
if lr then call execute('quit;');
run;
proc contents data=somedata;run;
Draycut:
It worked. THANK YOU!!!!
@jennyshop anytime, glad to help 🙂
@jennyshop wrote:
Hi all,
Can someone help me. I want to rename all the variables in a dataset that have a certain value in the name. I have hundreds of variables. For example. address2018, city2018, state2018. Is there anyway I can change them all to address2019, city2019, state2019.
Thanks in advance,
Jenny
Or prevent the entire issue by having a variable such as YEAR with the value of 2018 and all of the variables with names like address, city, state.
Then if you have another "year" data it has the same structure and you can append the data and process or model using the YEAR variable for grouping or reporting.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.