Nic*_*hik 0 macros sas resolve
当我运行宏"quantplot"时,我遇到了图片中显示的问题:
图片错误.
基本上似乎正在发生的事情是,这种形式的表达式recode_&variable,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);
Run Code Online (Sandbox Code Playgroud)
还有目前无效的宏代码.请帮忙!
/*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);
Run Code Online (Sandbox Code Playgroud)
问题是您使用%qscan引入了宏引用:
%let variables=%qscan(&indep_var,&i,%str( ));
Run Code Online (Sandbox Code Playgroud)
有时这个引用不应该自动删除,它会打破以下标记:
class recode_&variables;
Run Code Online (Sandbox Code Playgroud)
每当MPRINT显示看起来正确但错误的代码时,您应该怀疑宏引用问题.您可以自己取消引用该值:
class %unquote(recode_&variables);
Run Code Online (Sandbox Code Playgroud)
或改变你的使用%qscan到%scan.
您的使用解决方案%sysfunc(strip())因为%sysfunc()取消引用该值而起作用.