Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Mute
- Printer Friendly Page

🔒 This topic is **solved** and **locked**.
Need further help from the community? Please
sign in and ask a **new** question.

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Posted 01-18-2017 02:07 PM
(3989 views)

How can we apply IF/THEN/ELSE statement to each element in a n*m matrix so that the result is also a n*m matrix of TRUE/FALSE?

1 ACCEPTED SOLUTION

Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Could you provide an example?

To get a boolean (0 or 1) matrix, just use a logical expression with a matrix:

```
proc iml;
x = {1 2,
1 3,
2 4};
b = (x > 2);
print b;
```

The IF-THEN/ELSE statement works on a scalar quantity. The ANY function and ALL function can be used with the IF-THEN statement to make logical decisions. For example:

```
if any(x) > 3 then print "Some element > 3";
else print "No element > 3";
if all(x) > 0 then print "All elements > 0";
else print "Some element <= 0";
```

For more information, see "IF-THEN logic with matrix expressions."

3 REPLIES 3

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

Could you provide an example?

To get a boolean (0 or 1) matrix, just use a logical expression with a matrix:

```
proc iml;
x = {1 2,
1 3,
2 4};
b = (x > 2);
print b;
```

The IF-THEN/ELSE statement works on a scalar quantity. The ANY function and ALL function can be used with the IF-THEN statement to make logical decisions. For example:

```
if any(x) > 3 then print "Some element > 3";
else print "No element > 3";
if all(x) > 0 then print "All elements > 0";
else print "Some element <= 0";
```

For more information, see "IF-THEN logic with matrix expressions."

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

```
Given a matrix x below, I would like to have a resultant matrix y having 1 for each element of x<0 else 0:
proc iml;
```

x = {1 2,

1 -3,

2 4};

IF x<0 THEN y=1;

ELSE y=0;

print y;

Resultant matrix should be:

y= {0 0,

0 1,

0 0}

The result however is a scalar y=0

- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content

As I explained in my previous response, don't use the IF-THEN statement. Just create the binary matrix:

```
proc iml;
x = {1 2,
1 -3,
2 4};
y = (x<0);
print y;
```

**Don't miss out on SAS Innovate - Register now for the FREE Livestream!**

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

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.