Statistical programming, matrix languages, and more

Replace missing values with 0

Accepted Solution Solved
Reply
Contributor
Posts: 59
Accepted Solution

Replace missing values with 0

I have a matrix that contains 2 missing values:

 

G
0.09090910.210.66666670.2857143
0.14285710.28571430.2.0.25
-20.1538462-2-0.1818180.6666667
-0.666667-10.1538462-10.5
0.22222220.252-10.5
0.22222222-0.2857140.4-2
1-0.3333330.33333330.42
0.50.4.-11

 

The CALL CHANGE function only works on text fields. And I don't understand the explanations for the REPLACE function.

 

What is the logic in SAS/IML to replace those two missing values with 0?


Accepted Solutions
Solution
‎10-20-2015 10:55 AM
SAS Super FREQ
Posts: 3,624

Re: Replace missing values with 0

use the LOC function to find the entries that are missing. Then use assignment to replace those values:

 

idx = loc( G = . );
G[ idx ] = 0;

View solution in original post


All Replies
Solution
‎10-20-2015 10:55 AM
SAS Super FREQ
Posts: 3,624

Re: Replace missing values with 0

use the LOC function to find the entries that are missing. Then use assignment to replace those values:

 

idx = loc( G = . );
G[ idx ] = 0;
Contributor
Posts: 59

Re: Replace missing values with 0

Thank you. Using LOC() and idx() worked. Using the same matrix G, I'm getting a new error in applying it to the norm() module. Here's the line of code:

 

    GFrob = norm(G, "Frobenius");

 

And the error is "ERROR: Invocation of unresolved module NORM."

 

Based on a post in The Do Loop, this code should work!  

 

http://blogs.sas.com/content/iml/2014/04/07/vector-and-matrix-norms-in-sas.html

 

SAS Super FREQ
Posts: 3,624

Re: Replace missing values with 0

The first sentence of the blog says that you need SAS/IML 12.1, which was shipped in August 2012 as part of SAS 9.3m2.

 

What do you get when you run the following statement?

%put SYSVLONG = &SYSVLONG;

Contributor
Posts: 59

Re: Replace missing values with 0

SYSVLONG = 9.03.01M1P

SAS Super FREQ
Posts: 3,624

Re: Replace missing values with 0

That answers the question: You are running SAS 9.3.

 

Now what to do about it? There have been five releases of SAS since SAS 9.3, so you might consider upgrading.

 

In the meantime, most norms are trivial to program. Here's a SAS/IML function that computes the Frobenius, L1, L2, and "L infinity" norms for matrices:

start matnorm(x, method);
   s = substr(upcase(method), 1, 4);
   isValid = (s="FROB" | s="L1" | s="LINF" | s="L2");
   if ^isValid then return(.);
   
   if (s="FROB") then do;
      return( sqrt(x[##]) );
   end;
   else if (s="L1") then do;
      return( max(x[+,]) );
   end;
   else if (s="LINF") then do;
      return( max(x[,+]) );
   end;
   else if (s="L2") then do;
      v = eigval(x`*x);
      return( sqrt(v[1]) );
   end;
finish;


/* Test the module on the doc example */

x={1 2, 3 4};
mn1 = matnorm(x, "L1");
mnF = matnorm(x, "Frobenius");
mnInf = matnorm(x, "LInf");
print mn1 mnF mnInf;
Contributor
Posts: 59

Re: Replace missing values with 0

That worked. Thank you again.

🔒 This topic is solved and locked.

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

Discussion stats
  • 6 replies
  • 864 views
  • 2 likes
  • 2 in conversation