I recently read a blog post in which a SAS user had to rename a bunch of variables named A1, A2,..., A10, such as are contained in the following data set:

 /* generate data with variables A1-A10 */ data A; array A[10] A1-A10 (1); do i = 1 to 10; A{i} = i; end; output; run;

He needed to rename the variables to B1, B2, ..., B10 in order to merge some data. He did it like this:

 /* rename Ai = Bi for i=1 to 10 */ data B; set A (rename=(A1=B1 A2=B2 A3=B3 A4=B4 A5=B5 A6=B6 A7=B7 A8=B8 A9=B9 A10=B10)); run;

Many SAS users know that you can use numbered range lists to specify variables on a VAR statement or on a MODEL statement in a SAS procedure, but did you know that you can also specify a numbered range list in the RENAME= option?

 /* rename A1-A10 to B1-B10 */ data C; set A (rename=(A1-A10=B1-B10)); run;

Remember this trick when you need to rename dozens of variables that have numerical suffixes and a common prefix.

Share

Distinguished Researcher in Computational Statistics

Rick Wicklin, PhD, is a distinguished researcher in computational statistics at SAS and is a principal developer of SAS/IML software. His areas of expertise include computational statistics, simulation, statistical graphics, and modern methods in statistical data analysis. Rick is author of the books Statistical Programming with SAS/IML Software and Simulating Data with SAS.

1. Very nice trick to know about. Could save lots of time and typing, and eliminate opportunities to make stupid errors.

2. Ross Bettinger on

The logical extension to the numbered range rename is

rename=( a--e = f--j )

but the SAS compiler rejects this syntax.... :-(

• Chris Hemedinger on

This relies on the name range list, which figures out the ranges by their sequence in the data set (order in which variables were defined, as reported by PROC CONTENTS). It isn't influenced by "collate" order of the variable names. So, while something like this might be convenient, it isn't really the design of the name range feature.