Solved
Contributor
Posts: 33

# "If...then...else" Function for Matrix's Elements

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

Accepted Solutions
Solution
‎11-04-2011 01:02 PM
SAS Super FREQ
Posts: 4,181

## "If...then...else" Function for Matrix's Elements

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

All Replies
Solution
‎11-04-2011 01:02 PM
SAS Super FREQ
Posts: 4,181

## "If...then...else" Function for Matrix's Elements

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

Contributor
Posts: 33

## Re: "If...then...else" Function for Matrix's Elements

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

SAS Super FREQ
Posts: 4,181

## "If...then...else" Function for Matrix's Elements

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

🔒 This topic is solved and locked.