table 和 sas 中的列之间的内积

Inner product between a table and a column in sas

我想计算 table(矩阵)和列(向量)之间的内积。以下是示例数据集。

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 . ;
    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 数据集并合并 testcoef 数据集中的 coef 列,然后通过与 [=19= 相乘对所有列进行排列] 列,并最终将所有列相加。

但是如果数据集的行数很大,这个过程会很慢,我想知道是否有更聪明或更快的方法来做到这一点。

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 table 会像:


  | Cust |  COL1
-----------------
1 | COL1 |  1.4 
2 | COL2 |  2.8 
3 | COL3 |  4.2 
4 | COL4 |  5.6 
5 | COL5 |  7   

看看这是否适合你

proc sql noprint;
   select count(*) into :d separated by ' '
   from testcoef;
quit;

data want(keep = value);

   if _N_ = 1 then do i = 1 by 1 until (z);
      set testcoef end = z;
      array c {&d.} _temporary_;
      c{i} = coef;
   end;
   
   set tempdf;
   array v v1 - v&d.;

   do over v;
      value = sum(value, v*c{_i_});
   end;

run;

结果:

value 
1.4 
2.8 
4.2 
5.6 
7.0 

过程得分。

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 ;
   retain _TYPE_ 'SCORE';
   length Variable ;
   input Variable v1-v3;
   rename variable=_NAME_;
   datalines ;
New 0.1 0.2 0.3
;
RUN ;
proc print;
   run;
proc score data=tempdf score=testcoef out=t3;
   var V:;
   run;
proc print;
   run;