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

in the flow of data step processing, what is the first action in a typical data step? : Sas programming

657


Of all your work, where have you been the most successful?

4215


How to create list output for cross-tabulations in proc freq?

626


what is business intelligence? : Sas-bi

593


which date function advances a date, time or datetime value by a given interval? : Sas programming

606






how to debug and test the sas program? : Sas-administrator

586


What is the difference between using drop = data set option in data statement and set statement?

647


what is the effect of the options statement errors=1? : Sas programming

600


Describe a time when you were really stuck on a problem and how you solved it?

2094


What are the difficulties u faced while doing vital signs table or dataset?

1943


Give some examples where proc report’s defaults are different than proc print’s defaults?

607


how many types prompting framework can be broken down to? : Sas-bi

639


Mention what are the data types does SAS contain?

703


what is data access? : Sas-di

621


For what purpose would you use the RETAIN statement?

1057