I need to create a square matrix (let's call it SQUARE_M) where the rows represent a certain variable (let's call it Var_A), the columns, another variable (Var_B), and their intersection, a certain value resulting from a combination of the two variables. Each variable can have m values, so my matrix needs to have m rows and m columns.
Currently, I have a matrix (let's call it CURRENT) with m**2 rows and 3 columns (Var_A, Var_B, and, for each combination of the two, the resulting value in Var_C). I intend to get the matrix into a square shape (as described above) as follows:
1. Sort current matrix by Var_A, then by Var_B;
2. Create a square matrix, populated only with 1's so far:
SQUARE_M=j (m, m+1, 1); *The extra column is for holding the value of VAR_A from the CURRENT matrix;
What I'm not sure of, though, is the exact IML syntax for the two steps below:
3. Loop through VAR_A in CURRENT and write each unique value into column 1 in SQUARE_M, in the order in which they are found.
m = [number of rows and columns in my data];
r_new_row = 1; * row in new matrix;
c_new_column = 1; * column in new matrix;
while c_new_column <= m:
(write the figure from CURRENT to [m_square_row, m_square_column]);
c_square_column +=1;
r_square_row +=1;
quit;
Also, am I right that, when counting rows or columns, IML begins with 1 rather than 0?
Have a look at the FULL() function, as this might do everything that you want. For example:
x={
4.1 1 2,
5.3 1 5,
3.7 3 3};
y=full(x);
print x, y;
Regarding the last question, you are correct that IML matrices have no row or column zero.
Ian's suggestion will work as long as the values of VAR_A and VAR_B are 1 through m. From your pseudo-code, this appears to be the case.
If this issue has been resolved, please mark the question as 'answered.'
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.