[英]How to use sum function in SAS
我认为还有其他方法可以对所有单元格求和。 我只想学习如何使用 sum function。 请给我一些关于 SAS 中程序的“sum(y)”的建议。
data dt00;
input X Y @@;
cards;
1 3 2 5
;
run;
data dt01;
set dt00;
z+x+sum(y);
run;
沿行求和- sum 和 arrays。
使用总和 function:
data want;
set dt00;
sum=sum(x, y);
run;
你也可以使用z=sum(of _numeric_);
- 这将汇总所有数字列,以防太多而无法列出。
使用 arrays:
data want;
set dt00;
array s {*} _numeric_;
sum=sum(of s[*]);
run;
沿列求和- 这里我们只对列的总和感兴趣,所以我们 select 最后一行。 您可以使用 set 语句中的end=
选项和 if 语句的组合来执行此操作。
data want;
set dt00 end=eof;
sum_x + x;
sum_y + y;
if eof then output;
keep sum:;
run;
如果您有很多列,我建议您使用其他 SAS 程序(例如,使用 mean 程序)计算列总数。
proc means data=dt00 noprint;
var _numeric_;
output out=want(drop=_type_ _freq_) sum= / autoname;
run;
对所有单元格求和——这里我们使用与沿列求和相同的技巧,因为我们只对所有单元格的总和感兴趣。
data want;
set dt00 end=eof;
sum + x + y;
if eof then output;
keep sum;
run;
sum 语句sum + x + y
意味着 sum 变量被自动保留,并且所有缺失值都被视为 0。
简而言之,这相当于
data want;
set dt00 end=eof;
retain sum;
sum = sum(sum,x,y);
if eof then output;
keep sum;
run;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.