%macro mat_mult(left=, right=, result=); /***************************************************************************/ /* This macro multiplies two matrices and returns the result in a matrix */ /* Macro mat_mult operates in a SAS data step. Matrices RESULT, LEFT, */ /* and RIGHT must already be declared in the data step where this macro is */ /* invoked. Furthermore, it is up to the user to ensure that the matrices */ /* are conformable: that the number of columns of LEFT is the same as the */ /* number of rows of RIGHT and that the RESULT matrix has the same number */ /* of rows as LEFT and the same number of columns as RIGHT. */ /* */ /* Macro variables: */ /* Left - the left-side matrix that is multiplied by Right to produce */ /* the Result matrix. Left and Right must conform. That is, */ /* the number of columns of matrix Left must match the number */ /* of rows of matrix Right. */ /* Right - the right-side matrix. (See notes about macro variable Left)*/ /* Result - the matrix that is the product of Left x Right. Dimension */ /* of the Result matrix is determined as the number of rows in */ /* Left and the number of columns in Right. */ /* */ /* Author: Dale McLerran */ /* Fred Hutchinson Cancer Res Ctr */ /* */ /***************************************************************************/ _n_nrowL = dim(&left,1); _n_ncolL = dim(&left,2); _n_nrowR = dim(&right,1); _n_ncolR = dim(&right,2); do _row=1 to _n_nrowL; do _col=1 to _n_ncolR; __sum=0; do _i_j=1 to _n_ncolL; __sum + &left[_row, _i_j] * &right[_i_j, _col]; end; &result[_row, _col] = __sum; end; end; %mend mat_mult;