BookmarkSubscribeRSS Feed
deleted_user
Not applicable
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.
4 REPLIES 4
RickM
Fluorite | Level 6
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);
deleted_user
Not applicable
Many thanks for your help. I have tried to replace the &var_median with &var._median or &&var_median, but sas doesn't work.
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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.
statsplank
Calcite | Level 5
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;

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

Health and Life Sciences Learning

 

Need courses to help you with SAS Life Sciences Analytics Framework, SAS Health Cohort Builder, or other topics? Check out the Health and Life Sciences learning path for all of the offerings.

LEARN MORE

Discussion stats
  • 4 replies
  • 1421 views
  • 0 likes
  • 4 in conversation