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
Which command is used to save logs in the external file?
what are sas/access and sas/connect? : Sas programming
What would be the result of the following SAS function (given that 31 Dec, 2000 is Sunday)?
I have a dataset concat having variable a b & c. How to rename a b to e & f?
State the difference between INFORMAT and FORMAT ?
explain about sas business intelligence? : Sas-bi
What is the difference between proportion and average?
How do dates work in SAS data?
how can you put a "trace" in your program? : Sas programming
How will you react when, while consulting a SAS documentation manual to get an answer to a problem, someone says hey, I thought you were supposed to know all that stuff already, and not have to look it up in a book?
how does sas handle missing values in a merge? : Sas programming
What is the difference between nodupkey and nodup options?
what are the benefits of data integration? : Sas-di
what are the component of range? : Sas-bi
To what type of programms have you used scratch macros?