turn on suggestions

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

Showing results for

Find a Community

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

08-11-2016 05:50 AM

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!

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to DaniDenzel

08-11-2016 06:54 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to IanWakeling

08-15-2016 04:10 AM

Thank you for your response; it has been extremely helpful. I am glad to see that I didn't miss a glaringly obvious solution.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to DaniDenzel

08-11-2016 07:53 AM

Could you post an example (a dummy matrix) to explain what you are looking for ?

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to DaniDenzel

08-11-2016 08:51 AM

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.

- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

08-15-2016 04:14 AM

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.

It is impressive to see such a supportive environment, even for someone that is a beginner like me! So thanks again.