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-2024.png

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.

 

Register now!

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
  • 927 views
  • 0 likes
  • 2 in conversation