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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

what are informats in sas? : Sas-administrator

588


Do we follow ADAM in analysis dataset development?How? Usually which version? Why is it necessary?

1916


name the scheduler for scheduling job and explain the scheduler? : Sas-di

596


How does the internal authentication work in sas? : sas-grid-administration

585


what is function of retain statment

1525






What is the purpose of _character_ and _numeric_?

589


what are _numeric_ and _character_ and what do they do? : Sas programming

678


What are symget and symput? : sas-macro

735


Hi,by usining ptf how we have to combine (likr merge)10 datasets at a time in the oracle database(and write a macro code also)?Like this i have a douts a lot if you dont mind may please send one text mail for me(madhusudhanap16@gmail.com)?

1214


what are some problems you might encounter in processing missing values? In data steps? Arithmetic? Comparisons? Functions? Classifying data? : Sas programming

730


do you prefer proc report or proc tabulate? Why? : Sas programming

576


Explain by-group processing?

591


Hi Jeevan/Akshara I have tried to contact SASI Vats for latest Advanced SAS Programming Certification Dumps (A00-212) but have not got any response. I would highly and gratefully appreciate your assistance in this regard. If you guys have access to this, would you be kind enough to forward it to me ? I would abide by all the terms you have for this. Please, note my email id: upretigopi@yahoo.com Preti Sharma

3066


Which are the statements whose placement in the data step is critical?

764


What is PDV?

728