how to rename variables with Macro/array

Accepted Solution Solved
Reply
Contributor
Posts: 49
Accepted Solution

how to rename variables with Macro/array

Hello everyone. I'm having trouble wrting in macro or array to rename a number of variables by adding the same prefix to each of them. Below is the code in data step. But I'm hoping that somebody could help me come up with a more efficient way in macro or by array.

 

Thanks for your help!

 

data temp3.try;
   set temp3.freqPT_f;
   rename _0420=weight_0420 _0421=weight_0421 _0422=weight_0422 _0423=weight_0423 _0424=weight_0424 _0429=weight_0429;
run;

Accepted Solutions
Solution
‎06-03-2016 12:51 PM
SAS Super FREQ
Posts: 676

Re: how to rename variables with Macro/array

Hi

 

Looks like your temp3.freqPT_f was created by Proc TRANSPOSE. If this is correct then you might want to use the PREFIX= option of Proc TRANSPOSE

 

Bruno

View solution in original post


All Replies
Solution
‎06-03-2016 12:51 PM
SAS Super FREQ
Posts: 676

Re: how to rename variables with Macro/array

Hi

 

Looks like your temp3.freqPT_f was created by Proc TRANSPOSE. If this is correct then you might want to use the PREFIX= option of Proc TRANSPOSE

 

Bruno

Contributor
Posts: 49

Re: how to rename variables with Macro/array

That is amazing! Thanks for pointing this out! It works perfectly. But I'm also hoping that there is some other way to fix the problem if the variable was not created by proc transpose.
Grand Advisor
Posts: 10,210

Re: how to rename variables with Macro/array

How many of these do you have? If no more than you show then the added complexity of a macro may not be a good idea.

 

Here's one way:

%macro RenameList(vars= , prefix=);
   %let list=;
   %do i = 1 %to %sysfunc(countw(&vars));
      %let var=%scan(&vars,&i);
      %let list= &list %sysfunc(catx(=,&var,&prefix.&var));
   %end;
   &list
%mend;

use as

 

Rename %RenameList(vars=_0420 _0421 _0422 _0423, prefix=weight) ;

NOTE: there is no error checking for incorrect variables or resulting lengths. If the length of one of your variables names plus the prefix exceeds 32 characters it will generate errors.

Contributor
Posts: 49

Re: how to rename variables with Macro/array

Thank you for your valuable inputs! And yes, _0420 _0421 _0422 _0423 are all the variables to be renamed in my data set.

 

I'm new to the Macro language but would like to know whether your code would affect all the variables named like '_0421 ' to any of the dataset containing these variables. What if I just want to rename it in one particular dataset? In addition, what if I want to add suffix instead of prefix? Should I change the code to

 

 %let list= &list %sysfunc(catx(=,&var,&var.&suffix));

Thanks again for sharing your thoughts.

 

Grand Advisor
Posts: 10,210

Re: how to rename variables with Macro/array


Crystal_F wrote:

Thank you for your valuable inputs! And yes, _0420 _0421 _0422 _0423 are all the variables to be renamed in my data set.

 

I'm new to the Macro language but would like to know whether your code would 1) affect all the variables named like '_0421 ' to any of the dataset containing these variables. 2) What if I just want to rename it in one particular dataset?3) In addition, what if I want to add suffix instead of prefix? Should I change the code to

 

 %let list= &list %sysfunc(catx(=,&var,&var.&suffix));

Thanks again for sharing your thoughts.

 


Please see the bolded section to match my response

1) No only the exactly named variables. IF the variable doesn't exist you'll get errors.

2) Since the example use is in a RENAME statement it would only affect the rename statement in the current dataset you are using/creating. BUT any data set derived from the resulting set would have the new names just like any Rename statement.

3) Yes.

 

Contributor
Posts: 49

Re: how to rename variables with Macro/array

Thank you for educating me, which is very helpful.

 

However, the log indicated that there might be some error in the code:

 

278  %macro RenameList(vars= , prefix=);
279     %let list=;
280     %do i = 1 %to %sysfunc(countw(&vars));
281        %let var=%scan(&vars,&i);
282        %let list= &list %sysfunc(catx(=,&var,&prefix.&var));
283     %end;
284     &list
285  %mend;
286
287  %RenameList(vars=_0420 _0421 _0422 _0423, prefix=weight) ;
NOTE: Line generated by the macro variable "LIST".
1      _0420=weight_0420 _0421=weight_0421 _0422=weight_0422 _0423=weight_0423
       -----
       180

ERROR 180-322: Statement is not valid or it is used out of proper order.



Not really sure why it didn't work.

 

Grand Advisor
Posts: 10,210

Re: how to rename variables with Macro/array

The resolved code is datastep code. If it is not within a data step then the resulting line is an error, though you can see what it looks like;

 

The use is as I mentioned (partially) earlier

 

data new;

   set have;

   Rename %RenameList(vars=_0420 _0421 _0422 _0423, prefix=weight) ;

run;

The code generated by the macro then yields

data new;

   set have;

   Rename _0420=weight_0420 _0421=weight_0421 _0422=weight_0422 _0423=weight_0423 ;

run;

 

Contributor
Posts: 49

Re: how to rename variables with Macro/array

Thank you ballardw for all your help. This is great!
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 1463 views
  • 3 likes
  • 3 in conversation