Hello, I am trying to perform a simple matrix multiplication in SAS without PROC IML. So, the matrix multiplication is in the form of: A * transpose(A) I have found a following code using a simple input-data: * Input-data;
data matrix_A;
input a b c;
datalines;
4 5 6
7 8 9
10 11 12
;
* Matrix multiplication;
proc transpose data=matrix_A out=matrix_A_t(drop=_name_);
run;
%macro prod_mat(in_A =,in_B=,ou_AB=);
/* determine number of rows and columns in the 2nd matrix*/
%let B_id=%sysfunc(open(&in_B));
%let B_rows=%sysfunc(attrn(&B_id,nobs));
%let B_cols=%sysfunc(attrn(&B_id,nvars));
%let rc=%sysfunc(close(&B_id));
/* transpose the 2nd matrix*/
proc transpose data=&in_B out=t&in_B(drop=_:);run;
/* making Cartesian product of the 1st and transposed 2nd matrices*/
data &ou_AB;
do until(eofA);
set &in_A end=eofA;
do i=1 to n;
set t&in_B nobs=n point=i;
output;
end;
end;
run;
/* multiplication*/
data &ou_AB;
/* new columns for products, equal to number of columns in the 2nd matrix*/
array p[&B_cols];
do j=1 to &B_cols;
p[j]=0;
set &ou_AB;
array col _ALL_;
/* multiply corresponding pairs of columns*/
do i=&B_cols+2 to &B_cols+1+&B_rows;
p[j]+col[i]*col[i+&B_rows];
end;
end;
output;
keep p:;
run;
%mend prod_mat;
%prod_mat(in_A =matrix_A,in_B=matrix_A_t,ou_AB=work.matrix_AA_t)
However, this code is quite slow on larger datasets. Is there a way to optimize this code (without using PROC IML)? Or is there another way of doing A*transpose(A) matrix multiplication (without PROC IML)?
... View more