BookmarkSubscribeRSS Feed
Justin9
Obsidian | Level 7

Hi,

 

I have got the following variables: A_BC_1, A_BC_2, A_BC_3 and it continues to A_BC_120 (i.e. A_BC_1 - A_BC_120). Can someone help me rename all of these 120 variables in a more efficient way, so that they are called "eq1, eq2, eq3 and all the way to eq120 (i.e. eq1 - eq120)? Thanks!

 

Manually writing the code would be:

data final;

   set initial (rename=(A_BC_1=eq1

                                  A_BC_2=eq2

                                  A_BC_3=eq3

                                                                              (continues all the way to eq120)

                                  A_BC_120=eq120 ) );

run;

4 REPLIES 4
PaigeMiller
Diamond | Level 26
data want;
    set have;
    rename a_bc_1-a_bc_120=eq1-eq120;
run;

This is somewhat inefficient for HUGE data sets, but it works.

--
Paige Miller
ErikT
Obsidian | Level 7

Probably the easiest way is to use a variable list notation like this:

data final;
set initial(rename=(A_BC_1 - A_BC_120 = eq1 - eq120));
run;

But you could also make it a little more complicated to impress your co-workers and add an extra data step that creates a macro variable, which can be used in the set statement. Like this:

data _null_;
length dummy_var $5000;
do n=1 to 120;
   dummy_var = catx(' ',dummy_var,'A_BC_'||left(put(n,3.)),'=eq'||left(put(n,3.)));
end;
call symput('renamelist',dummy_var);
stop;
run;
data final;
set initial(rename=(&renamelist));
run;

It first defines a character variable of sufficient length. then it adds in a do loop all necessary rename pairs. Upon completion it uses the call symput routine to put that string into the macro variable renamelist, which is later referenced in the set statement.

 

Good luck!

PaigeMiller
Diamond | Level 26

If you go through the trouble of creating a macro variable to do this, then the much more efficient way of renaming variables is using PROC DATASETS rather than doing all the input and output of reading the (possibly very large) data set again just to perform a rename.

--
Paige Miller
ErikT
Obsidian | Level 7

It all depends on what has to be done with the variables. If it is just renaming and nothing else, I agree that using  PROC DATASETS for the rename is much more efficient. But if you have to have a data step anyway for some re-coding or calculations, then the rename in the SET statement makes sense.

 

Note: there is a difference between the separate RENAME statement and the rename option in the SET statement. Both variants were proposed in previous posts. The RENAME statement works on the output data set, so for actions within the data step you have to use the old names. The rename option in the SET statement works on the input. So you can use the new names in the data step.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 657 views
  • 1 like
  • 3 in conversation