On this page, we show a few SAS macro programs for dynamically renaming variables. The following are code fragments intended for use by more advanced SAS users. Although they may look complex only small changes are needed to adapt these macros for your data. For more information on learning how macro programs work, visit our seminar page on Introduction to SAS Macro Language.
Scenario Number 1
We have a list of variables and a list for the new names of these variables. In the example below, we want to rename variables faminc1 and faminc2 to be a and b for no particular reason.
%macro rename1(oldvarlist, newvarlist); %let k=1; %let old = %scan(&oldvarlist, &k); %let new = %scan(&newvarlist, &k); %do %while(("&old" NE "") & ("&new" NE "")); rename &old = &new; %let k = %eval(&k + 1); %let old = %scan(&oldvarlist, &k); %let new = %scan(&newvarlist, &k); %end; %mend;data faminc; input famid faminc1-faminc12 ; cards; 1 3281 3413 3114 2500 2700 3500 3114 -999 3514 1282 2434 2818 2 4042 3084 3108 3150 -999 3100 1531 2914 3819 4124 4274 4471 3 6015 6123 6113 -999 6100 6200 6186 6132 -999 4231 6039 6215 ; run; data a ; set faminc; %rename1(faminc1 faminc2, a b); run; proc print data = a heading= h noobs; run;famid a b faminc3 faminc4 faminc5 faminc6 faminc7 1 3281 3413 3114 2500 2700 3500 3114 2 4042 3084 3108 3150 -999 3100 1531 3 6015 6123 6113 -999 6100 6200 6186 faminc8 faminc9 faminc10 faminc11 faminc12 -999 3514 1282 2434 2818 2914 3819 4124 4274 4471 6132 -999 4231 6039 6215
Scenario Number 2
We have a list of variables and the name for each variable consists of the original name plus a common suffix. For example, we are going to rename variables faminc1 and faminc2 to be faminc1_new and faminc2_new below.
%macro rename2(oldvarlist, suffix); %let k=1; %let old = %scan(&oldvarlist, &k); %do %while("&old" NE ""); rename &old = &old.&suffix; %let k = %eval(&k + 1); %let old = %scan(&oldvarlist, &k); %end; %mend; data faminc; input famid faminc1-faminc12 ; cards; 1 3281 3413 3114 2500 2700 3500 3114 -999 3514 1282 2434 2818 2 4042 3084 3108 3150 -999 3100 1531 2914 3819 4124 4274 4471 3 6015 6123 6113 -999 6100 6200 6186 6132 -999 4231 6039 6215 ; run; data a ; set faminc; %rename2(faminc1 faminc2, _new); run; proc print data = a heading= h noobs; run;faminc1_ faminc2_ famid new new faminc3 faminc4 faminc5 faminc6 1 3281 3413 3114 2500 2700 3500 2 4042 3084 3108 3150 -999 3100 3 6015 6123 6113 -999 6100 6200 faminc7 faminc8 faminc9 faminc10 faminc11 faminc12 3114 -999 3514 1282 2434 2818 1531 2914 3819 4124 4274 4471 6186 6132 -999 4231 6039 6215
Scenario Number 3
We have a list of variables defined by a prefix followed by an index number. We are going to change the prefix and still use the same index order. For example, we are going to rename variables faminc1–faminc12 to be oldfaminc1–oldfaminc12.
%macro rename3(oldprefix, newprefix, num); %let k=1; %do %while(&k <= &num); rename &oldprefix.&k = &newprefix.&k; %let k = %eval(&k + 1); %end; %mend; data a ; set faminc; %rename3(faminc, oldfaminc, 12); run; proc print data = a heading= h noobs; run;famid oldfaminc1 oldfaminc2 oldfaminc3 oldfaminc4 oldfaminc5 oldfaminc6 1 3281 3413 3114 2500 2700 3500 2 4042 3084 3108 3150 -999 3100 3 6015 6123 6113 -999 6100 6200 oldfaminc7 oldfaminc8 oldfaminc9 oldfaminc10 oldfaminc11 oldfaminc12 3114 -999 3514 1282 2434 2818 1531 2914 3819 4124 4274 4471 6186 6132 -999 4231 6039 6215
There are also many other SAS programs that you can find that renames variables dynamically. For example, here is a page by SAS: Sample 1582: Dynamically rename multiple variables in a SAS data set .