BookmarkSubscribeRSS Feed
DaniDenzel
Fluorite | Level 6

I am new to SAS so my knowledge is very basic. I am using SAS version 9.3. I want to create multiple matrices based on a single matrix, V, that I have created. In Stata, the loop is very simple but I do not know how to replicate it in SAS. In Stata, I would write:

 

forvalues i=1/23 {
    mat V`i'=V

}

 

How do I write this in SAS to produce 23 matrices that are equal to the matrix, V? I want to do this so that I can then manipulate the matrices.

 

Thanks for your help!

 

5 REPLIES 5
IanWakeling
Barite | Level 11

There is no built in way to have an array of matrices in IML, so it may be best to consider storing all the matrices in one large matrix. If the matrices are all the same shape (as in your example) then you could stack them side-by-side or on top of each other and then access each one by subsetting columns or rows.

 

There is a way to keep the matrices separate by using the VALSET and VALUE functions, but avoid this if you can as it can make the code difficult to follow.  Rick has a nice blog post on this.

DaniDenzel
Fluorite | Level 6
Thank you for your response; it has been extremely helpful. I am glad to see that I didn't miss a glaringly obvious solution.
Ksharp
Super User
Could you post an example (a dummy matrix) to explain what you are looking for ?
Rick_SAS
SAS Super FREQ

Welcome to SAS. I'm not familiar with that Stata notation, but here are some tips that I hope will help you get started. First, read "Ten tips for learning the SAS/IML language."

 

Second, let us know what you are trying to accomplish. Different languages have different conventions and strengths/weaknesses. The exact steps to accomplish something in Stata might be difficult to accomplish in another language, but there is often an equivalent set of steps that accomplishes the same thing.  Just as idioms in English do not translate verbatim into Spanish, idoms/paradigms in one computer language not translate exactly into another computer language.

 

Here is some sample code that might help you. The following program starts with a matrix V. It loops 23 times. For each iteration it creates a new matrix, B. The new matrix starts out being equal to V, but then two values are randomly chosen and swapped.

The determinant of B is computed and stored in a vector called RESULT:

 

proc iml;
V = {1 2 3, 
     3 2 1,
     2 1 3};
/* Loop to compute the determinant of 23 matrices.
   Each matrix is similar to the matrix V, but 
   has two random elements swapped. */
N = 23;
numElements = 9;
result = j(N, 1);     /* allocate array to store results */
do k = 1 to N; 
   B = V;             /* initialize B to V */
   i = sample(1:numElements, 1); /* random integer 1:9 */
   j = sample(1:numElements, 1); /* random integer 1:9 */
   B[i||j] = V[j||i];            /* swap values */
   result[k] = det(B);           /* store result for this iteration */
end;
print result;

Two lessons here:

1) The matrix B is re-used 23 times. You do not need to create 23 matrices with different names because the result for each iteration is independent.

2) The results of each iteration are stored in a vector.

 

This is a common way to program an iterative algorithm in SAS/IML. You re-use matrices within a loop and store the results in a vector.

DaniDenzel
Fluorite | Level 6
Thank you for such a detailed response that has been so helpful to me.
It is impressive to see such a supportive environment, even for someone that is a beginner like me! So thanks again.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 1076 views
  • 2 likes
  • 4 in conversation