【问题标题】:macro variable won't resolve SAS宏变量不会解析 SAS
【发布时间】:2017-06-08 22:22:29
【问题描述】:

当我运行宏“quantplot”时,我遇到了图片中显示的问题:

Picture of Error.

基本上看起来正在发生的事情是这种形式的表达式 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);

【问题讨论】:

  • 与您的问题无关,但我建议使用 sgplot 而不是 gplot。在我看来,您可以获得更好的图形、更多的控制并且更易于使用。
  • 我的宏版本,用于自动化 proc rank。
  • @Nic - 请不要将答案编辑成问题。欢迎您自己添加答案。
  • @Joe 抱歉!我是堆栈交换的新手,不确定哪种回答方式合适。感谢您的澄清。无论如何,我认为其他人最终给出了更好的答案。

标签: macros sas resolve


【解决方案1】:

问题是您使用 %qscan 引入了宏引用:

%let variables=%qscan(&indep_var,&i,%str( ));

有时这种引用在应该删除时不会自动删除,它会破坏以下标记化:

class recode_&variables;

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

class %unquote(recode_&variables);

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

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

【讨论】:

  • 这是一个很好的答案。我不知道很多这些事情。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-08
相关资源
最近更新 更多