## read SAS dataset into a matrix

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

## Re: read SAS dataset into a matrix

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

> 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

## Re: read SAS dataset into a matrix

Here's the link to the online  IML documentation. Have a look through it then come back with any remaining questions.

## Re: read SAS dataset into a matrix

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

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;
``````

## Re: read SAS dataset into a matrix

Perfect homework! Thanks

## Re: read SAS dataset into a matrix

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.

## Re: read SAS dataset into a matrix

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

> 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, ];

From The DO Loop