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 stop statement?
What do the SAS log messages "numeric values have been converted to character" mean?
what is data governance? : Sas-di
data data1; input dt account; format dt date9.; cards; 1745 1230 1756 1120 1788 1130 1767 1240 ; data data2; input startdt enddt total; format startdt date9. enddt date9.; cards; 1657 1834 12300 1557 1758 16800 1789 1789 12300 1788 1345 12383 1899 1899 13250 ; proc sql; create table data3 as select * from data1 as x left join data2 as y on x.dt>=y.startdt and x.dt<=y.enddt; quit; Here, we are getting cartision product. But,I want left join report consisting of this program. It should not get duplicate values. you can modify the program also.
describe how to adjust the performance of data integrator? : Sas-di
What is the different between functions and PROCs that calculate the same simple descriptive statistics?
How sas treats the dsd delimiters?
How do you control the number of observations and/or variables read or written?
Explain what is the use of proc gplot?
How necessary is it to be creative in your work?
Give an example where SAS fails to convert character value to numeric value automatically?
How to limit decimal places for the variable using proc means?
what is program data vector? : Sas-administrator
describe about metadata object? : Sas-di
What are the ways in which macro variables can be created in sas programming?