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
what is the use of proc contents and proc print in sas? : Sas-administrator
What is SAS? What are the functions does it performs?
Differentiate between format and informat? : sas-grid-administration
What will calendar procedure do?
How can sas program be validated?
What are the special input delimiters used in SAS?
what is intially documentation in sas?
Describe a time when you were really stuck on a problem and how you solved it?
How do you use the do loop if you don’t know how many times you should execute the do loop?
what is data integration? : Sas-di
what is the difference between unique key and primary key? : Sas-di
what is sas and what are the functions? : Sas-administrator
Can you execute macro within another macro? If so, how would sas know where the current macro ended and the new one began? : sas-macro
What is program data vector (pdv) and what are its functions?
Give some examples where proc report’s defaults are different than proc print’s defaults?