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

Could someone please help me out?

I have a matrix A={1 1,1 2,2 1,2 2,2 3,3 1}. I would like to create a block diagonal matrix (B) such that B={1 1 0 0 0 0, 1 2 0 0 0 0, 0 0 2 1 0 0, 0 0 2 2 0 0, 0 0 2 3 0 0, 0 0 0 0 3 1}. I know of the block function but it does not work here (in its basic form at least). How can I do this in IML? Is it possible without looping because the number of rows in A can get very large (millions or more)?

Lots of thanks in advance for your help.

George

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

You don't need to loop over rows of A; you can loop over the unique blocks defined by A.  Use the UNIQUE function on the first column of A to find out how many blocks there are. Then for each block, use the BLOCK function to iteratively build up the matrix.  For you example, the loop would have three iterations and be equivalent to the following statements:

B = A[1:2,];  /* initialize outside loop */

/* do i = 2 to 3; */

B = block(B, A[3:5,]);

B = block(B, A[6,]);

/* end do */

print B;

View solution in original post

2 REPLIES 2
Rick_SAS
SAS Super FREQ

You don't need to loop over rows of A; you can loop over the unique blocks defined by A.  Use the UNIQUE function on the first column of A to find out how many blocks there are. Then for each block, use the BLOCK function to iteratively build up the matrix.  For you example, the loop would have three iterations and be equivalent to the following statements:

B = A[1:2,];  /* initialize outside loop */

/* do i = 2 to 3; */

B = block(B, A[3:5,]);

B = block(B, A[6,]);

/* end do */

print B;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

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. 

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
  • 2 replies
  • 1085 views
  • 0 likes
  • 2 in conversation