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 is the difference between verification and validation?

9 Answers   Oracle,


how will you location sas platform applications available from web browser? : Sas-bi

0 Answers  


what is proc Index? and what is proc document?

0 Answers   Mind Tree,


1.we can execute a macro with in a macro,by using call symput and symget can any one give me one example? 2.We can create the macro variables by using %let,%do,macro parameters,INTO clause in proc sql and call symput, can any one give me example to create macro variable with INTO clause and call symput? 3.

1 Answers  


How would you delete observations with duplicate keys?

6 Answers  






What are the difference between sas functions and procedures?

0 Answers  


How many ways to overcome a missing values???

0 Answers   HSBC,


How can you put a "trace" in your program?

2 Answers   Quintiles,


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

0 Answers  


Hello Friends, am new to this forum and am not good at sas progarmming. please can any one of you send me couple of sample large sample SAS Jobs which can you use 200 MB of data and other sas job upto 25GB of data. am doing a performance testing on our legacy systems and new upgraded system. I would really appreciate if you can do me this favor Thank you Priya

0 Answers  


In the flow of DATA step processing, what is the first action in a typical DATA Step?

9 Answers  


Describe the function and untility of the most difficult SAS macro that you have written.

0 Answers  


Categories