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


Please Help Members By Posting Answers For Below Questions

what is the use of proc contents and proc print in sas? : Sas-administrator

816


What is SAS? What are the functions does it performs?

943


Differentiate between format and informat? : sas-grid-administration

791


What will calendar procedure do?

819


How can sas program be validated?

766


What are the special input delimiters used in SAS?

858


what is intially documentation in sas?

4606


Describe a time when you were really stuck on a problem and how you solved it?

2410


How do you use the do loop if you don’t know how many times you should execute the do loop?

991


what is data integration? : Sas-di

834


what is the difference between unique key and primary key? : Sas-di

816


what is sas and what are the functions? : Sas-administrator

732


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

929


What is program data vector (pdv) and what are its functions?

820


Give some examples where proc report’s defaults are different than proc print’s defaults?

799