BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jennyshop
Calcite | Level 5

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

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

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;

 

View solution in original post

4 REPLIES 4
PeterClemmensen
Tourmaline | Level 20

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;

 

jennyshop
Calcite | Level 5

Draycut:

     It worked.  THANK YOU!!!!

ballardw
Super User

@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.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 1100 views
  • 1 like
  • 3 in conversation