How do you add a prefix to some or all variables in a
dataset using a SAS macro?
Answer Posted / kumar
If it is Interview Question I would do say something like this.
/* Running the renaming macro */
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=NEWNAME_&&var&i.
%end;
;
quit;
run;
options pageno=1 nodate;
proc contents data=&lib..&dsn;
title "After Renaming All Variables";
run;
%mend rename;
| Is This Answer Correct ? | 4 Yes | 0 No |
Post New Answer View All Answers
how could you generate test data with no input data? : Sas programming
How do dates work in sas?
What is a put statement?
what versions of sas have you used (on which platforms)? : Sas programming
what are the benefits of data integration? : Sas-di
Did you used proc test? when?
how do you want missing values handled? : Sas programming
What is SAS informats?
for what purpose would you use the retain statement? : Sas programming
How would you define the end of a macro? : sas-macro
What are the default statistics for means procedure?
What is interleaving in SAS?
What is connection profile? : sas-grid-administration
explain the function of substr in sas? : Sas-administrator
Describe crosslist option in tables statement?