When using SAS/IML, I sometimes need to form submatrices from a given matrix. For example, when I am given 16 x 16 square matrix, say matrix A, I would probably want to subdivide the matrix into four 4x4 submatrices and apply the same data process. In this case, it would probably be more convenient if we can make an index for the submatrices, such as A_1, A_2, A_3, A_4, or A_{1,1}, A_{1,2}, A_{2,1}, A_{2,2}.
How can I write a code for making these submatrices?
You can specify a submatrix by using the row and column subscripts.
For example, if you have a 6 x 6 matrix, you can get the four 3 x 3 submatrices as follows
proc iml;
A = toeplitz(6:1);
print A;
/* A[rows, cols] */
A1 = A[1:3, 1:3];
A2 = A[1:3, 4:6];
A3 = A[4:6, 1:3];
A4 = A[4:6, 4:6];
print A1, A2, A3, A4;
How you use this depends on what you are trying to accomplish. It's not clear what you want to do.
You can also loop over the submatrices and extract each one as part of a loop. For example, the following loop that iterates over the four submatrices:
cutPts = {0, 3, 6};
do i = 1 to 2;
rows = (1+cutPts[i]) : cutPts[i+1];
do j = 1 to 2;
cols = (1+cutPts[j]) : cutPts[j+1];
A_ij = A[rows, cols];
det = det(A_ij);
print i j det;
end;
end;
You can specify a submatrix by using the row and column subscripts.
For example, if you have a 6 x 6 matrix, you can get the four 3 x 3 submatrices as follows
proc iml;
A = toeplitz(6:1);
print A;
/* A[rows, cols] */
A1 = A[1:3, 1:3];
A2 = A[1:3, 4:6];
A3 = A[4:6, 1:3];
A4 = A[4:6, 4:6];
print A1, A2, A3, A4;
How you use this depends on what you are trying to accomplish. It's not clear what you want to do.
You can also loop over the submatrices and extract each one as part of a loop. For example, the following loop that iterates over the four submatrices:
cutPts = {0, 3, 6};
do i = 1 to 2;
rows = (1+cutPts[i]) : cutPts[i+1];
do j = 1 to 2;
cols = (1+cutPts[j]) : cutPts[j+1];
A_ij = A[rows, cols];
det = det(A_ij);
print i j det;
end;
end;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.