It can be done in a few different ways. I think this one would be the easiest to understand. It uses CALL Execute to write code the uses the PROC DATASETS procedure to do the rename.
data work.have;
x = 1; y = 'Y'; output;
run;
data renames;
oldname = "X"; NewName = "X_NEW"; OUTPUT;
oldname = "Y"; NewName = "Y_NEW"; OUTPUT;
run;
data _null_;
set renames end= finished;
if _n_ = 1 then do;
call execute("proc datasets library=work nolist;");
call execute(" modify have;");
end;
call execute(" rename " || oldname || " = " || NewName || ";");
if finished then do;
call execute("quit;");
end;
run;
Like this?
data NAMES;
input LIBNAME $ MEMNAME $ OLDNAME $ NEWNAME $;
cards;
WORK A a1 c1
WORK A a2 c2
WORK A a3 c3
run;
data _null_;
set NAMES;
call execute('proc datasets lib='||LIBNAME||' noprint;');
call execute(' modify '||MEMNAME||';');
call execute(' rename '||OLDNAME||'='||NEWNAME||';');
call execute('run;');
run;
@ChrisNZ's code works. And to get the dataset NAMES used in his code, you can do this:
proc transpose data=A (obs=0) out=A_t (keep=_NAME_ rename=(_NAME_=oldname));
var :;
run;
data NAMES;
set A_t;
set B (keep=b2 rename=(b2=newname));
libname='WORK';
memname='A';
run;
It can be done in a few different ways. I think this one would be the easiest to understand. It uses CALL Execute to write code the uses the PROC DATASETS procedure to do the rename.
data work.have;
x = 1; y = 'Y'; output;
run;
data renames;
oldname = "X"; NewName = "X_NEW"; OUTPUT;
oldname = "Y"; NewName = "Y_NEW"; OUTPUT;
run;
data _null_;
set renames end= finished;
if _n_ = 1 then do;
call execute("proc datasets library=work nolist;");
call execute(" modify have;");
end;
call execute(" rename " || oldname || " = " || NewName || ";");
if finished then do;
call execute("quit;");
end;
run;
Try using this code
data test;
input a1 a2 a3;
datalines;
1 2 3
4 5 6
7 8 9
;
run;
data var_names;
input var1 $2. var2 $3.;
datalines;
b1 c1
b2 c2
b3 c3
;
run;
data contents;
ds=open("test",'i');
num=attrn(ds,'nvars');
do v=1 to num;
vn=varname(ds,v);
output;
end;
run;
proc sql;
select count(distinct var1) into : count from var_names;
%let count=&count;
select distinct var2 into : newvar_1-: newvar_&count from var_names;
quit;
proc sql;
select count(distinct v) into : count1 from contents;
%let count1=&count1;
select distinct vn into : oldvar_1-:oldvar_&count1 from contents;
quit;
%put &oldvar_1 &oldvar_2 &oldvar_3;
%put &newvar_1 &newvar_2 &newvar_3;
data want1;
set test;
run;
%macro rename;
%do i=1 %to &count1.;
data want1;
set want1;
rename &&oldvar_&i..=&&newvar_&i..;
run;
%end;
%mend;
%rename;
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.