在我定义数组的数据步骤中,我可以使用sum函数,但count函数不起作用。如何计算数组中不为零的值的数量?
SUM_ARRAY = sum(of A1-A20); - Works
COUNT_ARRAY = count(of A1-A20); Yields the following error: "The COUNT function call has too many arguments"
答案 0 :(得分:1)
正确的函数而不是COUNT是N,DIM或HBOUND。 不幸的是,没有人会计算特定值,只排除缺失值。
循环结果是计算非0的一种方法。
Array _a(*) a1-a20;
Count0 = 0;
Do I = 1 to dim(_a);
If _a (I) ne 0 then count0 = count0 + 1;
End;
答案 1 :(得分:1)
COUNT
执行此操作。我不确定它比循环操作在时间上或结构上更好,但它至少是一个有趣的解决方案。
基本上我们按;
分隔数据,包括用分隔符开始和结束数据,然后计算;0;
的数量,并从总数中减去。
data _null_;
call streaminit(7);
array a[20];
do _i = 1 to 20;
a[_i] = max(0,rand('Uniform')-0.3);
end;
put a[*]=;
nonzero = dim(a) - count(';'||catx(';',of a[*])||';',';0;');
put nonzero=;
run;