Thursday, January 3, 2008

Renaming all variables in a SAS dataset with a suffix

/* This macro adds the suffix "_base" to all the variables in the sas dateset lib.dsn.
Mainly using ideas from http://www2.sas.com/proceedings/sugi28/118-28.pdf and used it successfully*/

%macro rename(lib,dsn,suff);
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=&&var&i.._&suff
%end;
;
quit;
run;
options pageno=1 nodate;
proc contents data=&lib..&dsn;
title "After Renaming All Variables";
run;
%mend rename;
%rename(WORK,MEPS2000,base);

No comments: