简体   繁体   English

sas中表与列的内积

[英]Inner product between a table and a column in sas

I would like to calculate an inner product between a table (matrix) and a column (vector).我想计算表(矩阵)和列(向量)之间的内积。 Below are the sample datasets.以下是示例数据集。

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 ;

I want to calculate it by v1*0.1 + v2*0.2 + v3*0.3 by each row.我想通过每一行的v1*0.1 + v2*0.2 + v3*0.3来计算它。 And the final result will be look like:最终结果将如下所示:

1.4
2.8
4.2
5.6
7

as a column.作为专栏。

Which respectively calculated by其中分别由

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

THANKS.谢谢。

I have try to proc transpose the tempdf datasets and merge the coef columns in testcoef dataset then do array over all columns by multiplying with coef column, and eventually sum all the columns up.我尝试proc transpose tempdf数据集并合并testcoef数据集中的coef列,然后通过与coef列相乘对所有列进行数组处理,并最终将所有列相加。

But the process will be very slow if the dataset is large in rows, I wonder if there is any smarter or faster way to do it.但是如果数据集很大,这个过程会很慢,我想知道是否有更聪明或更快的方法来做到这一点。

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 will be like: final_out表将是这样的:


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

See if this works for you看看这是否适合你

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;

Result:结果:

value 
1.4 
2.8 
4.2 
5.6 
7.0 

PROC SCORE.过程分数。

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;

在此处输入图像描述

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

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