【问题标题】:Syntax issue when using macro in SAS在 SAS 中使用宏时的语法问题
【发布时间】:2020-10-11 15:52:24
【问题描述】:

我想写一个宏。它的作用是接收数据集,将数据集分成几组,统计每组的样本大小,最后创建一个变量调用“ind”来表示这个组大小是否大。 (ind=0 表示小,ind=1 表示大)。

我首先在宏之外编写代码,运行良好。我得到了没有错误的理想输出。

data stu; input group score; datalines; 
1 700
1 850
1 820
1 640
1 920
1 480
1 460
1 500
2 570
2 580
run;

proc freq data=stu;
table group/out=count;
run;

data count; set count;
if count>=3 then ind=1; else ind=0;
run;

proc print data=count;run;

但是,当我尝试使用宏来实现这个功能时:

%macro tests(data=, group=);

proc freq data=&data;
table &group/out=freqn;
run;

data count; set freqn;
%if count>=3 %then ind=1; %else ind=0;
run;

proc print data=count;run;

%mend tests;

%tests(data=stu, group=group);

无法生成数据集“count”,我也遇到了错误:

ERROR 22-322: Syntax error, expecting one of the following: !, !!, &, *,
 **, +, -, /, <, <=, <>, =, >, ><, >=, AND, EQ, GE, GT, LE,
 LT, MAX, MIN, NE, NG, NL, OR, ^=, |, ||, ~=.

怎么会这样?

谢谢。

【问题讨论】:

    标签: sas sas-macro


    【解决方案1】:

    我想通了:在数据步中,if-then statements 前面不应该有%。所以宏代码应该是:

    %macro tests(data=, group=);
    
    proc freq data=&data;
    table &group/out=freqn;
    run;
    
    data count; set freqn;
    if count>=3 then ind=1; else ind=0;
    run;
    
    proc print data=count;run;
    
    %mend tests;
    
    %tests(data=stu, group=group)
    

    (希望它可以帮助像我一样会犯愚蠢错误的 SAS 初学者)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多