Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 12-10-2011 08:58 PM
(6873 views)

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

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

5 REPLIES 5

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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?

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

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;

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. **

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.