BookmarkSubscribeRSS Feed
dvtarasov
Obsidian | Level 7

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
IanWakeling
Barite | Level 11

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.

 

Rick_SAS
SAS Super FREQ

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.

Rick_SAS
SAS Super FREQ

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

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 3 replies
  • 1263 views
  • 0 likes
  • 3 in conversation