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 ;
    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 transposetempdf数据集并合并数据集中的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   
4

2 回答 2

2

看看这是否适合你

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 
于 2022-02-22T07:26:57.843 回答
1

过程分数。

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 $32;
   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;

在此处输入图像描述

于 2022-02-22T14:38:28.590 回答