How do you add a prefix to some or all variables in a
dataset using a SAS macro?

Answers were Sorted based on User's Feedback



How do you add a prefix to some or all variables in a dataset using a SAS macro?..

Answer / 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

How do you add a prefix to some or all variables in a dataset using a SAS macro?..

Answer / sastechies

Often we need to add a prefix to some or all variables in a
dataset before we might have to merge datasets that have
similar column attributes...This macro would allow you to
do that....

Try it for yourself....

/**
SAS Macro to add a prefix to some or all variables in a
data set...
to be used like this...
%prefixvars(inpdsn,prefix,outdsn,excludevars=);
inpdsn - input dataset name libname.dsnname
prefix - prefix that you want to assign
outdsn - output dataset name libname.dsnname
excludevars - vars that you do not want to rename with the
prefix
**/

%macro prefixvars(inpdsn,prefix,outdsn,excludevars=);

/* split the excludevars into individual macro var names
for later use*/
%let num=1;
%let excludevar=%scan(%upcase(&excludevars),&num,' ');
%let excludevar&num=&excludevar;

%do %while(&excludevar ne );
%let num=%eval(&num + 1);
%let excludevar=%scan(&excludevars,&num,' ');
%let excludevar&num=&excludevar;
%end;
%let numkeyvars=%eval(&num - 1); /* this is number of
variables given in the exclude vars */


%let dsid=%sysfunc(open(&inpdsn)); /* open the dataset
and get the handle
*/

%let numvars=%sysfunc(attrn(&dsid,nvars)); /* get the
number of variables
*/

data
&outdsn;


set &inpdsn(rename=(
/*rename all the variables that are not in the
excludevars=
*/

%do i = 1 %to &numvars;
%let flag=N;
%let var&i=%sysfunc(varname(&dsid,&i));
%do j=1 %to &numkeyvars;
%if %upcase(&&var&i) eq &&excludevar&j %then %
let flag=Y;
%end;
%if &flag eq N %then %do;
&&var&i=&prefix&&var&i %end;
%
end;));



%let rc=%sysfunc(close
(&dsid));


run;


%mend
prefixvars;





/*Call the macro
now*/


%prefixvars
(sashelp.buy,fr_,work.out,excludevars=date)




Is This Answer Correct ?    3 Yes 1 No

Post New Answer

More SAS Interview Questions

What happens in the following code, if u type 8 instead of *? proc sql noprint; create table abc as select 8 from lib.abc; quit;

3 Answers  


how to assign a macro value to a variable?

8 Answers   CitiGroup,


What is the difference between %local and %global? : sas-macro

0 Answers  


What is the use of stop statement?

0 Answers  


If you?re not wanting any SAS output from a data step, how would you code the data statement to prevent SAS from producing a set?

1 Answers  






Explain what is the use of proc gplot?

0 Answers  


What are the prime responsibilities of data integration administrator? : Sas-di

0 Answers  


Given an unsorted data set, how to read the last observation to a new data set?

0 Answers  


How do you add a number to a macro variable?

2 Answers  


How many ways to overcome a missing values???

0 Answers   HSBC,


what is change analysis in sas di ? : Sas-di

0 Answers  


: and & modifiers.

0 Answers   Oracle,


Categories