BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
MarkGIP
Calcite | Level 5

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!!!

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

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

View solution in original post

3 REPLIES 3
Rick_SAS
SAS Super FREQ

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

MarkGIP
Calcite | Level 5

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)?

Rick_SAS
SAS Super FREQ

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: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 3 replies
  • 1113 views
  • 0 likes
  • 2 in conversation