宏变量不会解析 SAS
macro variable won't resolve SAS
当我运行宏"quantplot"我运行进入问题如图:
Picture of Error.
本质上似乎正在发生的事情是 none 这种形式的表达式 recode_&variables、out_&variables 等似乎返回了正确的值。它只是将其读取为 recode_ 并忽略宏变量。
我为示例数据提供了以下代码:
data test (drop=i);
do i=1 to 1000;
a=round(uniform(1)*4,.01);
b=round(uniform(1)*10,.01);
c=round(uniform(1)*7.5,.01);
u = rand("Uniform");
y=floor( (2)*u );
output;
end;
stop;
run;
%macro percentiles(indep_var=,num_groups=);
%let recodes=recode_%sysfunc(tranwrd(&indep_var,%str( ),%str( recode_)));
proc rank data=test out=test groups=7;
var &indep_var;
ranks &recodes;
run;
%mend;
%percentiles(indep_var=a b c);
还有当前不起作用的宏代码。请帮忙!
/*Plots to determine functional form for quantitative variables*/
%macro quantplot(indep_var=,dep_var=);
/* Count the number of words in the input */
%let count=%sysfunc(countw(&indep_var));
/* Loop through the total number of values */
%do i = 1 %to &count;
%let variables=%qscan(&indep_var,&i,%str( ));
%put(&variables);
proc means data=test;
%put(&variables);
class recode_&variables;
var &dep_var;
output out = out_&variables mean = pby_&variables;
run;
data p_&variables;
set out_&variables; if _type_=1;
lnp = log(pby_&variables/(1-pby_&variables));
run;
ods graphics on;
proc gplot data = p_&variables;
symbol value = star i=join;
plot lnp*recode_&variables;
plot pby_&variables*recode_&variables;
run;
ods graphics off;
%end;
%mend;
%quantplot(indep_var=a b c,dep_var=y);
问题是您使用 %qscan 引入了宏引用:
%let variables=%qscan(&indep_var,&i,%str( ));
有时这个引用不会在应该被删除的时候自动删除,它破坏了以下的标记化:
class recode_&variables;
每当 MPRINT 显示看起来正确但出错的代码时,您应该怀疑宏引用问题。您可以自己取消引用该值:
class %unquote(recode_&variables);
或将 %qscan
的用法更改为 %scan
。
您使用 %sysfunc(strip())
的解决方案有效,因为 %sysfunc()
取消引用该值。
当我运行宏"quantplot"我运行进入问题如图:
Picture of Error.
本质上似乎正在发生的事情是 none 这种形式的表达式 recode_&variables、out_&variables 等似乎返回了正确的值。它只是将其读取为 recode_ 并忽略宏变量。
我为示例数据提供了以下代码:
data test (drop=i);
do i=1 to 1000;
a=round(uniform(1)*4,.01);
b=round(uniform(1)*10,.01);
c=round(uniform(1)*7.5,.01);
u = rand("Uniform");
y=floor( (2)*u );
output;
end;
stop;
run;
%macro percentiles(indep_var=,num_groups=);
%let recodes=recode_%sysfunc(tranwrd(&indep_var,%str( ),%str( recode_)));
proc rank data=test out=test groups=7;
var &indep_var;
ranks &recodes;
run;
%mend;
%percentiles(indep_var=a b c);
还有当前不起作用的宏代码。请帮忙!
/*Plots to determine functional form for quantitative variables*/
%macro quantplot(indep_var=,dep_var=);
/* Count the number of words in the input */
%let count=%sysfunc(countw(&indep_var));
/* Loop through the total number of values */
%do i = 1 %to &count;
%let variables=%qscan(&indep_var,&i,%str( ));
%put(&variables);
proc means data=test;
%put(&variables);
class recode_&variables;
var &dep_var;
output out = out_&variables mean = pby_&variables;
run;
data p_&variables;
set out_&variables; if _type_=1;
lnp = log(pby_&variables/(1-pby_&variables));
run;
ods graphics on;
proc gplot data = p_&variables;
symbol value = star i=join;
plot lnp*recode_&variables;
plot pby_&variables*recode_&variables;
run;
ods graphics off;
%end;
%mend;
%quantplot(indep_var=a b c,dep_var=y);
问题是您使用 %qscan 引入了宏引用:
%let variables=%qscan(&indep_var,&i,%str( ));
有时这个引用不会在应该被删除的时候自动删除,它破坏了以下的标记化:
class recode_&variables;
每当 MPRINT 显示看起来正确但出错的代码时,您应该怀疑宏引用问题。您可以自己取消引用该值:
class %unquote(recode_&variables);
或将 %qscan
的用法更改为 %scan
。
您使用 %sysfunc(strip())
的解决方案有效,因为 %sysfunc()
取消引用该值。