Turn on suggestions

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

Showing results for

Options

- 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**.
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 06-22-2023 07:42 PM
(564 views)

Hi! I'm new to proc iml or other SAS matrix capabilities. I couldn't find an iml manual online. First of all, I need to find out how to read a 10x10 matrix from a SAS dataset, then elevate it to several increasing powers, then save the resulting matrices as SAS datasets.

Next, I need to multiply each of these matrices' rows by a 10x1 column vector and store the scalars.

Any help is appreciated!

1 ACCEPTED SOLUTION

Accepted Solutions

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

*> how to get proc iml to READ a matrix from a dataset*

I think we answered this question: https://blogs.sas.com/content/iml/2012/01/16/reading-all-variables-into-a-matrix.html If that doesn't answer your question, please provide an example.

*> the 10 by 10 matrices are basic Markov transition probability matrices, *

I assume you are using the ideas in

https://blogs.sas.com/content/iml/2023/03/20/estimate-transition-matrix.html

(or something similar) to estimate the transition matrices. If you have the transition matrices and initial distribution of states, you can use matrix multiplication to estimate the probability that the system is in any state after k transitions: https://blogs.sas.com/content/iml/2016/07/07/markov-transition-matrices-sasiml.html

The distribution of states enables you to answer many questions related to the probable state of the system after k transitions.

If you have specific questions, please post your IML code and/or example data.

*> picking the rows with a macro will be straightfoward to get*

The IML language is a full-featured programming language, so you almost never need to use SAS macro in an IML program. If you want the m_th row of a matrix, just extract it: row = A[m, ];

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

This sounds like homework, but here are a few hints:

1. SAS Kiwi gave you the link to the documentation, but here is another introduction to SAS IML that is shorter: https://support.sas.com/resources/papers/proceedings13/144-2013.pdf

See also https://blogs.sas.com/content/iml/2014/08/11/ten-tips-for-learning-sasiml.html

2. Read from a data set into a matrix: https://blogs.sas.com/content/iml/2012/01/16/reading-all-variables-into-a-matrix.html

3. Write to a data set from a matrix: https://blogs.sas.com/content/iml/2011/04/18/writing-data-from-a-matrix-to-a-sas-data-set.html

4. The matrix power operator enables you to multiply a matrix by itself k times. Or, if you want all powers, you can use a DO loop:

```
proc iml;
A = {1 -1,
-1 2};
B = A;
do i = 2 to 5;
B = B * A;
print i, B; /* = A**i */
end;
```

5. Multiplication by a vector will give you a VECTOR, not a scalar. Presumably, you want to multiply a column vector by a matrix. Here is a 2x2 example that uses the matrix A above:

```
v = {3, 4};
y = A*v;
print y;
```

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

Perfect homework! Thanks

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

Thanks for all the hints, everyone. For now, I had to put the matrix into MATLAB to do the matrix operations; MATLAB is quite straight foward to code and I'm on an extremely tight deadline to provide some of the many answers in this project.

However, I still need to streamline it all into SAS, since all base matrices will be estimated from a dataset containing ~1.7 million rows and growing. The project will require calculating anywhere between 40-50 different matrices from the SAS dataset, then doing all the operations.

I haven't had the time to figure out how to get proc iml to READ a matrix from a dataset. If anyone has the code for this and doesn't mind sharing it, I'd really appreciate the time saving tip, I'm on a tight schedule here.

**In case someone in the community is interested in Markov chains**: the 10 by 10 matrices are basic Markov transition probability matrices, all estimated from the SAS dataset with a bunch of different criteria. Item 5 in the answer is a misunderstanding: the final goal **really are scalars**, each one the expected future values of a certain index. Each scalar is the nth row of the matrix (1 by 10 vector of probabilities) multiplied by the 10 by 1 column vector of the 10 possible index values. I think picking the rows with a macro will be straightfoward to get once the matrix is read as matrix: use a vector with zeroes everywhere and a 1 in the position of the row I want to pick.

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

*> how to get proc iml to READ a matrix from a dataset*

I think we answered this question: https://blogs.sas.com/content/iml/2012/01/16/reading-all-variables-into-a-matrix.html If that doesn't answer your question, please provide an example.

*> the 10 by 10 matrices are basic Markov transition probability matrices, *

I assume you are using the ideas in

https://blogs.sas.com/content/iml/2023/03/20/estimate-transition-matrix.html

(or something similar) to estimate the transition matrices. If you have the transition matrices and initial distribution of states, you can use matrix multiplication to estimate the probability that the system is in any state after k transitions: https://blogs.sas.com/content/iml/2016/07/07/markov-transition-matrices-sasiml.html

The distribution of states enables you to answer many questions related to the probable state of the system after k transitions.

If you have specific questions, please post your IML code and/or example data.

*> picking the rows with a macro will be straightfoward to get*

The IML language is a full-featured programming language, so you almost never need to use SAS macro in an IML program. If you want the m_th row of a matrix, just extract it: row = A[m, ];

Secure your spot at the must-attend AI and analytics event of 2024: SAS Innovate 2024! Get ready for a jam-packed agenda featuring workshops, super demos, breakout sessions, roundtables, inspiring keynotes and incredible networking events.

Register by March 1 to snag the Early Bird rate of just $695! Don't miss out on this exclusive offer.

** **

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.