简体   繁体   English

SAS中的Excel sumproduct

[英]Excel sumproduct in SAS

Table1 表格1

Age     B C D E F G H
0-10    5 6 8 9 0 1 1
10-20   4 5 9 7 1 4 5
20-30   5 6 5 4 2 5 8
0-10    0 4 0 5 8 0 7

Table2 表2

Age      A1  B1  C1  D1
0-10     0.1 0.9 0.2 
10-20    0.5 0.6 0.7

I want to extract 我要提取

SUMPRODUCT(Table1(G:H),Table2(B1:C1))

with condition that IF Table1.Age = Table2.Age . IF Table1.Age = Table2.Age

OUTPUT Table should look like 输出表应该看起来像

AGE   SUMPRODUCT
0-10  0.8
10-20 0.70

You need to merge the two datasets together, and then calculate the sum of the products. 您需要将两个数据集合并在一起,然后计算乘积之和。

data want;
merge table1(in=a) table2(in=b);
by age;
if a and b;
newvar = sum(g*b1, h*c1);
run;

You could do the same thing in a SQL join or a hash table lookup. 您可以在SQL连接或哈希表查找中执行相同的操作。

Arrays: 数组:

data want;
merge table1(in=a) table2(in=b);
by age;
if a and b;
array avars[20] <list of a vars>;
array bvars[20] <list of b vars>;
do _t = 1 to dim(avars);
   newvar = sum(newvar,avars[_t]*bvars[_t]);
end;
run;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM