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

How would you identify a macro variable?

0 Answers  


What is the difference between INPUT and INFILE ?

0 Answers  


Hi , which book should i refer to for preaparing SAS statistical Exam. Searched a lot on books but still did n't find relevant books

2 Answers  


What is the sas data set? : sas-grid-administration

0 Answers  


if we dont want to print output what we have to do..???give syntax..???

8 Answers   Accenture, GSK GlaxoSmithKline,






how can you sort the dataset having millions of OBS(instead of sort procedure?

4 Answers   EXL,


What is a pdv and what are its functions?

0 Answers  


Are you familiar with special input delimiters? How are they used?

4 Answers   Accenture,


If you have a dataset that contains 100 variables, but you need only five of those, what is the code to force SAS to use only those variables?

0 Answers  


How many ways to overcome a missing values???

0 Answers   HSBC,


What is the difference between an informat and a format? Name three informats or formats.

13 Answers   Accenture, IBM,


Name validation tools used in SAS

0 Answers  


Categories