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

Explain substr function?

770


what are all the reports you generated in your recent project?

1927


Do you know the features of sas?

796


Can you suggest us materials for sdtm mapping?

4232


How do you control the number of observations and/or variables read or written?

992


what is the limit of the number of the rows and columns available in the worksheet? : Sas-bi

843


Describe the ways in which you can create macro variables?

828


what do you mean by data staging area? : Sas-di

817


What is a method for assigning first.VAR and last.VAR to the BY group variable on unsorted data?

2275


what is the difference between infile and input? : Sas-administrator

833


how many display types available in sas bi dashboard? : Sas-bi

835


What is the difference between nodupkey and nodup options?

779


How will you use the WHO Drug Dictionary for Reporting Clinical Trials?

2086


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

969


what is the difference between calculating the 'mean' using the mean function and proc means? : Sas programming

869