macro rename variable problem

Reply
N/A
Posts: 0

macro rename variable problem

dear all,

When I use the following code to rename a variable name, it works well.

proc means median maxdec=2 data=tmp;
var x1;
class group;
ods output summary=tmp1;
data tmp1;set tmp1(rename=x_median=xmedian);
run;

When I use a following macro to rename the variable name, it doesn't work any way.

%macro rename(data=,var=,group=);
proc means median maxdec=2 data=&data;
var &var;
class &group;
ods output summary=tmp1;
data tmp1;set tmp1(rename=&&var_median=xmedian);
run;
%mend;

How can I to revise the macro? Thanks in advance for your time and patience.
Regular Contributor
Posts: 165

Re: macro rename variable problem

It looks like the problem is most likely because SAS thinks &var_median is a macro variable (as in %let var_median=Variable; ). You need a period at the end of your macro variable so that SAS will look up the correct macro variable name. It also looks like the && is redundant in your code.

So try using:

set tmp1(rename=&var._median=xmedian);
N/A
Posts: 0

Re: macro rename variable problem

Many thanks for your help. I have tried to replace the &var_median with &var._median or &&var_median, but sas doesn't work.
Super Contributor
Super Contributor
Posts: 3,174

Re: macro rename variable problem

Suggest the OP post a reply with the SAS log revealing the exact error so we can better understand what this means: "sas doesn't work"

Scott Barry
SBBWorks, Inc.
Frequent Contributor
Posts: 77

Re: macro rename variable problem

Hi wmqy,

Does the variable in tmp1 must be xmedian? Can it be medianx?

If medianx is ok, try the following code:

%macro rename(data=,var=,group=);
proc means maxdec=2 data=&data noprint;
var &var;
class &group;
output out=tmp1 median(&var)=median&var;
run;
data tmp1;
set tmp1(where=(_Type_ NE 0));
drop _Type_ _Freq_;
run;
Ask a Question
Discussion stats
  • 4 replies
  • 498 views
  • 0 likes
  • 4 in conversation