Hi!
I am fairly new to SAS and the concept of IML. Maybe you could help me with my problem:
Is it possible to use the "if... then... else" function on the elements of the Matrix? My intention is to check whether the specific element of Matrix X(mxn) is greater than 2. If that is the case I'd like to change the value of the respective element in matrix X with 1. Else I want the value to be replaced by 0. This method should be applied to the entire Matrix. As a result I'd like to get a (mxn) matrix with just 0's and 2's
I thought about using the "if" function:
if x>2 then x=1;
else x=0;
However, this function won't allow me to check every element separately and keep the dimension of the original matrix.
Looking forward to your support! THANK YOU!!!
Welcome! You might want to subscribe to my blog: every Monday is a "Getting Started" post for people that are new to SAS/IML.
To see previous posts, go to http://blogs.sas.com/content/iml and click on the Getting Started tag in the right hand sidebar.
In order to write efficient programs, you want to avoid loops over elements of matrices. It takes time to learn how to deal with matrices as a "whole entity," so feel free to ask questions at this forum whenever you find yourself looping over all elements of a matrix.
One way to answer your question is to ese the LOC function to find the elements that satisfy the condition. Examples similar to yours are at
http://blogs.sas.com/content/iml/2011/05/16/finding-data-that-satisfy-a-criterion/
A second way is to use the CHOOSE function. See http://blogs.sas.com/content/iml/2011/08/15/complex-assignment-statements-choose-wisely/
The LOC method is more general. I think the CHOOSE function is what you need for this particular example: x = choose(x>2, 1, 0);
Rick
Welcome! You might want to subscribe to my blog: every Monday is a "Getting Started" post for people that are new to SAS/IML.
To see previous posts, go to http://blogs.sas.com/content/iml and click on the Getting Started tag in the right hand sidebar.
In order to write efficient programs, you want to avoid loops over elements of matrices. It takes time to learn how to deal with matrices as a "whole entity," so feel free to ask questions at this forum whenever you find yourself looping over all elements of a matrix.
One way to answer your question is to ese the LOC function to find the elements that satisfy the condition. Examples similar to yours are at
http://blogs.sas.com/content/iml/2011/05/16/finding-data-that-satisfy-a-criterion/
A second way is to use the CHOOSE function. See http://blogs.sas.com/content/iml/2011/08/15/complex-assignment-statements-choose-wisely/
The LOC method is more general. I think the CHOOSE function is what you need for this particular example: x = choose(x>2, 1, 0);
Rick
Thank you Rick for your help. I will sure have a look at your blog;)
Just one more thing... Is it also possible to replace the elements with elements of a different matrix (which has the same dimension like X)?
Sure. Let IDX be the vector that contains the indices into a matrix. Then to copy the elements in Y into the elements in X, say X[IDX] = Y[IDX];
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.