BookmarkSubscribeRSS Feed
ShufeGuoding
Obsidian | Level 7

I want to replace missing value in a matrix A with zero and first need to find their locations in the matrix. Since function loc() treats the missing values as zero, how can I use the function to find their locations in A?

7 REPLIES 7
Rick_SAS
SAS Super FREQ

proc iml;

A = {. 2 3, 4 . 6};

idx = loc(A=.);

A[idx] = 0;

ShufeGuoding
Obsidian | Level 7

Because missing values are treated as zero, Your code seems not work properly. The problem is the same as  that I met.

Reeza
Super User

Works fine for me, idx identifies the missing as 1, 5. Next line replaces them to be 0's.

proc iml;

A = {. 2 3, 4 . 6};

idx = loc(A=.);

print idx;

A[idx] = 0;

print a;

OUTPUT:

The SAS System


15
023
406
ShufeGuoding
Obsidian | Level 7

I get it. Thanks a lot!

  Steve

Rick_SAS
SAS Super FREQ

As far as I know, missing values are not "treated as zero" anywhere in IML. You might be thinking of the behavior of an IF/THEN-ELSE statement, where a missing value in the IF conditional results in the condition evaluating to FALSE.

ShufeGuoding
Obsidian | Level 7

I was misleaded by the statements for Loc function in Usg/IML12.1 that read:

The  loc funciton finds nonzero elements of a matix. .... . Missing values treated as zeros.:smileyconfused:

Rick_SAS
SAS Super FREQ

I see.  I interpret that phrase to mean that

y = loc(x);

will produce a matrix y such that y[i,j]=0 whenever x[i,j] is zero or missing, but I see how that phrase might be confusing. To make sure you always get the correct answer, I recommend using a logical expression as the argument to the LOC function.  For example

y = loc(x^=0);

or

y = loc(x>0);

etc

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
  • 7 replies
  • 1598 views
  • 0 likes
  • 3 in conversation