Add letters to all variable names in a SAS dataset

Add prescript

%let old=sashelp.shoes;
%let new=newdataname; /*it is okay for the old name and the new name to be the same*/
%let prefix=abcd_;

data XXX;set &old;
run;
PROC SQL NOPRINT;
SELECT TRIM(NAME)||"=&prefix"||TRIM(NAME)
INTO :VARLIST SEPARATED BY ' '
FROM DICTIONARY.COLUMNS
WHERE LIBNAME EQ "WORK" AND MEMNAME EQ "XXX"
ORDER BY VARNUM;
QUIT;
PROC DATASETS LIBRARY=WORK NOLIST;
MODIFY XXX;
RENAME &VARLIST;
QUIT;
data &new;
set XXX;
run;

 

 

Add postscript

PROC SQL NOPRINT;
SELECT TRIM(NAME)||"="||TRIM(NAME)||"&post"
INTO :VARLIST SEPARATED BY ' '
FROM DICTIONARY.COLUMNS
WHERE LIBNAME EQ "WORK" AND MEMNAME EQ "XXX"
ORDER BY VARNUM;
QUIT;

 

If you need some variables not renamed in this process, modify the where statement:

WHERE LIBNAME EQ "WORK" AND MEMNAME EQ "XXX"

and upcase(name)  ne 'GENDER'

and upcase(name)  ne 'ID'

and upcase(name)  ne 'YEAR'

Leave a Reply