宏变量不会解决SAS

时间:2017-06-08 22:22:29

标签: macros sas resolve

当我运行宏" quantplot"我遇到了图片中显示的问题:

Picture of Error

基本上似乎正在发生的事情是,这种形式的recode_&变量,out_&变量等中的任何表达式似乎都没有返回正确的值。它只是将其读作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);

1 个答案:

答案 0 :(得分:5)

问题是您使用%qscan:

引入了宏引用
%let variables=%qscan(&indep_var,&i,%str( ));

有时这个引用不应该自动删除,它会打破以下标记:

class recode_&variables;

每当MPRINT显示看起来正确但代码错误的代码时,您应该怀疑宏引用问题。您可以自己取消引用该值:

class %unquote(recode_&variables);

或将%qscan的使用更改为%scan

使用%sysfunc(strip())的解决方案有效,因为%sysfunc()取消引用该值。