Doing Matrix Multiplication in a Data Step is possible. Usually it is not for beginners. But you must be courageous to read and understand the code. You have two Data sets with 3 rows and 3 columns, 3 rows and 2 columns. The product matrix is 3 by 2. Firstly, the Data set (Table1 say T1) is loaded into the Array M[3,3]. Similarly T2 is loaded into Array N[3,2]. Both Loadings are done when _N_ =1. The Product Matrix P[3,2] is computed in the usual way as we do in matrix multiplication in the Data Step. Here is the code. Be patient to understand the code.
data work.t1;
infile datalines dlm='#';
input Col1 Col2 Col3;
datalines;
58 # 976 # 1
78 # 571 # 2
49 # 221 # 2
;
run;
data work.t2;
infile datalines dlm='#';
input Col1 Col2;
datalines;
12# 15
748# 48
494# 112
;
run;
data t3;
array m[3,3] _temporary_;
array n[3,2] _temporary_;
array p[3,2] _temporary_;
call missing(of p[*]);
if _n_ = 1 then do;
/* Load the Data SET T1 into Array sized to m[3,3] */
do i = 1 by 1 until(z1);
set t1 end = z1;
array c[3] col1 - col3;
do j = 1 to 3;
m[i, j] = c[j];
end;
end;
/* LOad the Data Set T2 into Array sized to n[3,2] */
do i = 1 by 1 until(z2);
set t2 end = z2;
set t2 end = z2;
array v[2] col1 - col2;
do j = 1 to 2;
n[i,j] = v[j];
end;
end;
end;
/* Do the Matrix Product (3*3 X 3*2 = 3*2) */
/* p[i,j] = SUM of m[i,k] * n[k,j] */
do i = 1 to 3;
do j = 1 to 2;
do k = 1 to 3;
p[i,j] + (m[i,k] * n[k,j]);
end;
end;
end;
/** Output Matrix **/
do i = 1 to 3;
do j = 1 to 2;
put p[i,j] =;
end;
end;
stop;
run;
This can be done easily using PROC FCMP which also comes with Base SAS. Again you consult the SAS Documentation for Proc FCMP. There are SPECIAL Matrix Functions. I am using CALL MULT(M, N, P) to get the Product Matrix. Read_array and Call Dynamic_array are special FCMP functions which you have to read. Read_array reads a Data Set into a 2-dimensional Array. The Call Dynamic_array creates an array of required dimensions (in this case 3 by 2) at run_time. The second function is available in Proc FCMP and is not supported in Data Step.
Here is the magic program:
proc fcmp;
array m[1] / nosym;
array n[1] / nosym;
array p[1] / nosym;
file log;
rc = read_array('t1', m);
rc = read_array('t2', n);
call dynamic_array(p,3,2);
call mult(m,n,p);
put p =;
quit;
Here is the Product Matrix(P)
p[1, 1]=731238 p[1, 2]=47830 p[2, 1]=429032 p[2, 2]=28802 p[3, 1]=166884 p[3, 2]=11567
AFTER 6 Hours of my Reply:
There were two unwanted statements in the first solution. They are deleted and the revised code is given below:
data _null_;
array m[3,3] _temporary_;
array n[3,2] _temporary_;
array p[3,2] _temporary_;
if _n_ = 1 then do;
/* Load the Data SET T1 into Array sized to m[3,3] */
do i = 1 by 1 until(z1);
set t1 end = z1;
array c[3] col1 - col3;
do j = 1 to 3;
m[i, j] = c[j];
end;
end;
/* LOad the Data Set T2 into Array sized to n[3,2] */
do i = 1 by 1 until(z2);
set t2 end = z2;
array v[2] col1 - col2;
do j = 1 to 2;
n[i,j] = v[j];
end;
end;
end;
/* Do the Matrix Product (3*3 X 3*2 = 3*2) */
/* p[i,j] = SUM of m[i,k] * n[k,j] */
do i = 1 to 3;
do j = 1 to 2;
do k = 1 to 3;
p[i,j] + (m[i,k] * n[k,j]);
end;
end;
end;
/** Output Matrix **/
do i = 1 to 3;
do j = 1 to 2;
put p[i,j] =;
end;
end;
stop;
run;
To the Community Manager:
Why the replies are not time-sequenced? Earlier replies come at the bottom and the readability of the thread becomes fragmented. At least the Original Poster could have NOTICED and reacted to the reply. To me replying to Posts become dull and monotonous as there were no reactions shown to the reply. A mere 'LIKE' is the only solace.
... View more