为什么 proc sql 求和函数 returns 计数而不是总值?
why proc sql sum function returns count instead of total values?
我正在学习 SAS 中的过程 sql。当我使用 sql 求和函数时,我意识到如果添加比较运算符,输出是行数而不是垂直求和。我怎样才能得到垂直求和,求和背后的机制是什么?
data apple;
input target;
cards;
0
1
3
5
;
run;
proc sql;
select sum(target ge 3)
from apple;
quit;
预期结果为 3+5=8;
实际结果是2
proc sql;
select sum(target)
from apple
where target ge 3;
quit;
我相信您的代码所做的是将 (target gt 3) 作为布尔表达式求值,因此由于在 SAS TRUE=1 和 FALSE=0 中,求和函数添加了 0,0,1,1。
Craig 的解决方案实际上更好,但是 case when else end 你可以做你尝试的事情。
proc sql;
select sum(case when target ge 3 then target else 0 end)
from apple;
quit;
我正在学习 SAS 中的过程 sql。当我使用 sql 求和函数时,我意识到如果添加比较运算符,输出是行数而不是垂直求和。我怎样才能得到垂直求和,求和背后的机制是什么?
data apple;
input target;
cards;
0
1
3
5
;
run;
proc sql;
select sum(target ge 3)
from apple;
quit;
预期结果为 3+5=8;
实际结果是2
proc sql;
select sum(target)
from apple
where target ge 3;
quit;
我相信您的代码所做的是将 (target gt 3) 作为布尔表达式求值,因此由于在 SAS TRUE=1 和 FALSE=0 中,求和函数添加了 0,0,1,1。
Craig 的解决方案实际上更好,但是 case when else end 你可以做你尝试的事情。
proc sql;
select sum(case when target ge 3 then target else 0 end)
from apple;
quit;