Obsidian | Level 7

## Nesting and the IML equivalent of FOR and WHILE loops

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.

1. For each unique value in VAR_A, write the corresponding values in VAR_C into the relevant row in SQUARE_M, roughly like this:

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?

3 REPLIES 3
Barite | Level 11

## Re: Nesting and the IML equivalent of FOR and WHILE loops

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.

SAS Super FREQ

## Re: Nesting and the IML equivalent of FOR and WHILE loops

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.

SAS Super FREQ

## Re: Nesting and the IML equivalent of FOR and WHILE loops

If this issue has been resolved, please mark the question as 'answered.'

From The DO Loop