turn on suggestions

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

Showing results for

Find a Community

Topic Options

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

11-04-2011 12:17 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MarkGIP

11-04-2011 01:02 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MarkGIP

11-04-2011 01:02 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Rick_SAS

11-04-2011 01:35 PM

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to MarkGIP

11-18-2011 08:54 AM

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