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
Mention sas system options to debug sas macros.
What is the use of divide function?
What are the data types does SAS contain?
How can I remove header from output data set?
Difference between nodup and nodupkey options?
what is the use of sas management console? : Sas-di
What are the functions used for character handling?
Explain bmdp procedure?
How would you code a merge that will write the matches of both to one data set, the non-matches from the left-most data?
What is the basic syntax of a sas program?
what is the use of proc sql? : Sas programming
How is character variable converted into numeric variable and vice versa?
What is the different between functions and PROCs that calculate the same simple descriptive statistics?
How do you delete duplicate observations in sas?
What are SAS/ACCESS and SAS/CONNECT?