1

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

这怎么可能发生?

谢谢。

4

1 回答 1

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初学者)

于 2020-10-11T18:42:29.757 回答