要了解发生了什么,我建议您摆脱宏逻辑,并逐步运行宏调用生成的代码,查看结果。
样本数据:
data new;
input d;
cards;
0.22
0.21
0.22
0.21
;
在循环之前你有:
68 data out;
69 set new;
70 put (d)(=);
71 run;
d=0.22
d=0.21
d=0.22
d=0.21
那里没什么特别的。在循环的第一次迭代中,您引入了计数器,它会按预期工作:
3 data out;
74 set out new;
75 if d < .22 then count+1 ;
76 put (d count)(=);
77 run;
d=0.22 count=0
d=0.21 count=1
d=0.22 count=1
d=0.21 count=2
d=0.22 count=2
d=0.21 count=3
d=0.22 count=3
d=0.21 count=4
在循环的第二次迭代中,注意变量 COUNT 已经存在于正在读取的数据中(set 语句中的 work.OUT)。因此,对于前 8 条记录,count 的现有值有条件地加 1。这不是一个新的计数器。如果你想要一个新的计数器,你可以将 SET 语句更改为set out(drop=count) new ;
所以 &i=2 循环的迭代看起来像:
79 data out;
80 set out new;
81 if d < .22 then count+1 ;
82 put (d count)(=);
83 run;
d=0.22 count=0
d=0.21 count=2
d=0.22 count=1
d=0.21 count=3
d=0.22 count=2
d=0.21 count=4
d=0.22 count=3
d=0.21 count=5
d=0.22 count=.
d=0.21 count=1
d=0.22 count=1
d=0.21 count=2
如果您继续重新提交该步骤,则在第 7 次迭代中,您会得到问题中的陈述:
109 data out;
110 set out new;
111 if d < .22 then count+1 ;
112 put (d count)(=);
113 run;
d=0.22 count=0
d=0.21 count=7
d=0.22 count=1
d=0.21 count=8
d=0.22 count=2
d=0.21 count=9
d=0.22 count=3
d=0.21 count=10
d=0.22 count=.
d=0.21 count=6
d=0.22 count=1
d=0.21 count=7
d=0.22 count=.
d=0.21 count=5
d=0.22 count=1
d=0.21 count=6
d=0.22 count=.
d=0.21 count=4
d=0.22 count=1
d=0.21 count=5
d=0.22 count=.
d=0.21 count=3
d=0.22 count=1
d=0.21 count=4
d=0.22 count=.
d=0.21 count=2
d=0.22 count=1
d=0.21 count=3
d=0.22 count=.
d=0.21 count=1
d=0.22 count=1
d=0.21 count=2
我仍然不清楚您要做什么。不断地用自身覆盖 work.out 并附加自身是一种奇怪的结构。但是使用 DATA 步代码应该有助于显示正在发生的事情。