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
How does SAS handle missing values in: assignment statements, functions, a merge, an update, sort order, formats, PROCs?
What is proc sort?
WHAT IS SAS WEB SERVICE and what are the steps to create an xml service ?
AE datasets names? how many types?
name the scheduler for scheduling job and explain the scheduler? : Sas-di
what is the use of proc contents and proc print in sas? : Sas-administrator
What are the statements in proc sql?
Can you execute macro within another macro? : sas-macro
What are the best sas programming practices for handling very large datasets? : sas-grid-administration
How to create a permanent sas data set?
hi here is a problem can anybody solve this? i want to report the data through third party file. by using data _null_ or proc report or macro automation process. but i want to insert the 'titles and footnotes' in between the data and also starting of 2nd and ending of 2nd and starting of 3rd and ending of the 3rd page. tell me how and write the code?
If a variable contains letters or special characters, can it be numeric data type?
Why double trailing @@ is used in input statement?
What is the difference between SAS functions and procedures?
Mention what is the difference between nodupkey and nodup options?