在 SAS 中使用宏时的语法问题

Syntax issue when using macro in SAS

我想写一个宏。它的作用是接收数据集,将数据集分成若干组,统计每组的样本量,最后创建一个变量调用“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, ^=, |, ||, ~=.

怎么会这样?

谢谢。

我想通了:在一个数据步内,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新手)