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
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 |
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 |
How would you identify a macro variable?
What is the difference between INPUT and INFILE ?
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
What is the sas data set? : sas-grid-administration
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?
What is a pdv and what are its functions?
Are you familiar with special input delimiters? How are they used?
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?
How many ways to overcome a missing values???
What is the difference between an informat and a format? Name three informats or formats.
Name validation tools used in SAS