BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
Angela53
Obsidian | Level 7

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
Rick_SAS
SAS Super FREQ

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

View solution in original post

5 REPLIES 5
SASKiwi
PROC Star

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

Rick_SAS
SAS Super FREQ

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;

 

 

Angela53
Obsidian | Level 7
Perfect homework! Thanks
Angela53
Obsidian | Level 7

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. 

 

 

Rick_SAS
SAS Super FREQ

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 826 views
  • 3 likes
  • 3 in conversation