在我定义数组的数据步骤中,我可以使用求和函数,但计数函数不起作用。如何计算数组中不为零的值的数量?SAS - 阵列内的计数
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"
在我定义数组的数据步骤中,我可以使用求和函数,但计数函数不起作用。如何计算数组中不为零的值的数量?SAS - 阵列内的计数
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"
而不是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;
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;