Statistical programming, matrix languages, and more

Working with matrices

Reply
New Contributor
Posts: 3

Working with matrices

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!

 

Frequent Contributor
Posts: 134

Re: Working with matrices

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.

New Contributor
Posts: 3

Re: Working with matrices

Thank you for your response; it has been extremely helpful. I am glad to see that I didn't miss a glaringly obvious solution.
Grand Advisor
Posts: 9,593

Re: Working with matrices

Could you post an example (a dummy matrix) to explain what you are looking for ?
SAS Super FREQ
Posts: 3,416

Re: Working with matrices

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.

New Contributor
Posts: 3

Re: Working with matrices

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.
Ask a Question
Discussion stats
  • 5 replies
  • 283 views
  • 2 likes
  • 4 in conversation