2018年2月28日星期三

A SAS macro to rename all variable names in a data set

Data one;
 U_2=1;
 V_3=2;
 X_2=3;
 Y_2=4;
 Z_3=5;
Run;

options macrogen mprint mlogic;
%macro rename(lib,dsn);
options pageno=1 nodate;
proc contents data=&lib..&dsn;
title "Before Renaming All Variables";
run;
proc sql noprint;
 select nvar into :num_vars
 from dictionary.tables
 where libname="&LIB" and
 memname="&DSN";
 select distinct(name) into :var1-
:var%TRIM(%LEFT(&num_vars))
 from dictionary.columns
 where libname="&LIB" and
 memname="&DSN";
quit;
run;
proc datasets library=&LIB;
 modify &DSN;
 rename
 %do i=1 %to &num_vars;
 &&var&i=%substr(&&var&i., 1, %length(&&var&i.)-2)
%end;
;
quit;
run;
options pageno=1 nodate;
proc contents data=&lib..&dsn;
title "After Renaming All Variables";
run;
%mend rename;
%rename(WORK,ONE);/***Note the library and database name should be all in CAPITAL LETTERS)****/