我想计算表(矩阵)和列(向量)之间的内积。以下是示例数据集。
DATA tempdf ;
input v1 v2 v3 ;
datalines ;
1 2 3
2 4 6
3 6 9
4 8 12
5 10 15
;
RUN ;
DATA testcoef ;
length Variable $3. ;
input Variable $ coef ;
datalines ;
v1 0.1
v2 0.2
v3 0.3
;
RUN ;
我想v1*0.1 + v2*0.2 + v3*0.3
按每一行计算。最终结果将如下所示:
1.4
2.8
4.2
5.6
7
作为一列。
其中分别计算
1*0.1 + 2*0.2 + 3*0.3 = 1.4
2*0.1 + 4*0.2 + 6*0.3 = 2.8
3*0.1 + 6*0.2 + 9*0.3 = 4.2
4*0.1 + 8*0.2 + 12*0.3 = 5.6
5*0.1 + 10*0.2 + 15*0.3 = 7
谢谢。
我尝试proc transpose
了tempdf
数据集并合并数据集中的coef
列,testcoef
然后通过与列相乘对所有列进行数组处理coef
,并最终将所有列相加。
但是如果数据集很大,这个过程会很慢,我想知道是否有更聪明或更快的方法来做到这一点。
PROC TRANSPOSE data = tempdf out = temptrans name = Variable;
var _all_ ;
RUN ;
PROC SQL ;
create table trans_coef as
select a.*, b.Coef
from temptrans as a
left join testcoef as b
on a.Variable = b.Variable ;
QUIT ;
DATA out1 ;
set trans_coef ;
array colarr COL: ;
do over colarr ;
colarr = colarr * coef ;
end ;
RUN ;
PROC MEANS data = out1 sum;
var col: ;
output out = out1_score(drop = _TYPE_ _FREQ_) sum = ;
RUN;
PROC TRANSPOSE data = out1_score out = final_out name = Cust;
var COL: ;
RUN ;
final_out
表将像:
| Cust | COL1
-----------------
1 | COL1 | 1.4
2 | COL2 | 2.8
3 | COL3 | 4.2
4 | COL4 | 5.6
5 | COL5 | 7