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.'
Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.
If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website.
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.