Solved
SAS Employee
Posts: 3

# IF/THEN element wise logic in IML

Say I have the matrices:

A = { 1 -2 3,

-1 1 5,

3 -2 -4};

B = { 0 0 0,

0 0 0,

0 0 0};

And I would like to apply the following logic: If a value of A is less than zero (A<B), then keep the value. If not then double the value.

My goal is the matrix

C = { 2  -2 6,

-1 2 10,

6 -2 -4};

Can this be done in IML?  I am running into issues where the comparison is being treated as an ALL function.

Or, if I use a statement such as C=A>B then I am returned a matrix of binary values based on the comparison.

Thanks!

Accepted Solutions
Solution
‎01-21-2015 10:13 AM
SAS Super FREQ
Posts: 4,174

## Re: IF/THEN element wise logic in IML

Posted in reply to kellyhobson

You don't need the matrix B.  Use the CHOOSE function as follows:

C = choose(A<0, A, 2*A);

For more information about the CHOOSE function, see Complex assignment statements: CHOOSE wisely - The DO Loop

All Replies
Solution
‎01-21-2015 10:13 AM
SAS Super FREQ
Posts: 4,174

## Re: IF/THEN element wise logic in IML

Posted in reply to kellyhobson

You don't need the matrix B.  Use the CHOOSE function as follows:

C = choose(A<0, A, 2*A);

For more information about the CHOOSE function, see Complex assignment statements: CHOOSE wisely - The DO Loop

SAS Super FREQ
Posts: 4,174

## Re: IF/THEN element wise logic in IML

Posted in reply to kellyhobson

If you do need to use IF-THEN/ELSE logic on matrices, read this article: IF-THEN logic with matrix expressions - The DO Loop

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 2 replies
• 313 views
• 0 likes
• 2 in conversation