BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MarkGIP
Calcite | Level 5

Hi everyone!

I am looking for a way in IML to add additional rows and columns to a matrix. To give you a an idea I'd better use an example:

Let's assume I have 10 x 10 matrix. My goal is to add an extra column with 0's between the original second and third column and between the sixth and seventh column. The same goes for the rows. The result should be a 12 x 12 matrix.

Do you have any ideas?

THANK YOU!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

You could use the INSERT function four times, but I think it will be more efficient to allocate a 12x12 matrix of zeros and then insert the original matrix into the appropriate rows/cols as a submatrix. Both methods are shown below:

proc iml;

x = shape(1:100, 10);

/* 1. Insert row or col of zeros */
z = j(1, 10, 0); /* row of zeros */

tmp = insert(x, z, 3);
tmp = insert(tmp, z, 8);

tmp = insert(x, z`, 0, 3);
y = insert(tmp, z`, 0, 8);
print y;

/* 2. Allocate 12x12 matrix of zeros and copy x into it */

y = j(12, 12, 0); /* all zeros */
idx = setdif(1:12, {3 8}); /* gives {1 2 4 5 6 7 9 10 11 12} */
y[idx, idx] = x;
print y;

View solution in original post

5 REPLIES 5
MarkGIP
Calcite | Level 5

Perfect.... Thank you so much.

Just one more thing:

Do you know a way to use the insert function for more than one vector/matrix at once. The problem is that the function won't allow me to insert the vector at two different positions at the same time (between second and third column and sixth and seventh column). The only way I can think of is using a loop.

Do you happen to know a more elegant/efficient way?

art297
Opal | Level 21

My knowledge of IML is EXTREMELY limited but, from what I can tell, you are simply trying to insert a 1 column matrix into an existing matrix twice.  Couldn't you just do that by using two statements that use the input function with each specifying the desired column.  Of course, the desired column number for the second insert would have to be one greater since a new column had already been added.

MarkGIP
Calcite | Level 5

Thank you for your quick reply...

Repeating the statements would be also an option. However when the matrices become bigger and when there are more columns to be inserted it doesn't seem to be so efficient.

Rick_SAS
SAS Super FREQ

You could use the INSERT function four times, but I think it will be more efficient to allocate a 12x12 matrix of zeros and then insert the original matrix into the appropriate rows/cols as a submatrix. Both methods are shown below:

proc iml;

x = shape(1:100, 10);

/* 1. Insert row or col of zeros */
z = j(1, 10, 0); /* row of zeros */

tmp = insert(x, z, 3);
tmp = insert(tmp, z, 8);

tmp = insert(x, z`, 0, 3);
y = insert(tmp, z`, 0, 8);
print y;

/* 2. Allocate 12x12 matrix of zeros and copy x into it */

y = j(12, 12, 0); /* all zeros */
idx = setdif(1:12, {3 8}); /* gives {1 2 4 5 6 7 9 10 11 12} */
y[idx, idx] = x;
print y;

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
  • 5 replies
  • 7172 views
  • 0 likes
  • 3 in conversation