1

Is it right that in SAS IML, it is not possible to multiply to get product of two matrices if they contain missing value ??

There is no solution for this problem in SAS ???

Joe
  • 62,789
  • 6
  • 49
  • 67
Math
  • 1,274
  • 3
  • 14
  • 32
  • When you say 'missing value' you mean 'a single value in the matrix is missing'? Can you show an example where this doesn't work with example matrix data? – Joe Feb 25 '15 at 17:15
  • No, I have more than one mssing value – Math Feb 25 '15 at 17:15
  • Of course there is a solution, but you need to define the result of multiplying and adding with missing. For example, PROC SCORE can evaluate the matrix product X*B, but has different rules for missing values in the left matrix (the data) versus missing values in the right matrix (the model coefficients). What is the application? Do you want to propogate missing values or ignore them (treat as zero)? – Rick Feb 26 '15 at 18:34

2 Answers2

2

According to the IML Documentation on missing values, you are correct: it is not possible to multiply two matrices that contain a missing value (or more) using matrix multiplication (elementwise is fine). This is stated as for efficiency reasons; I'm not sure what that entails, but presumably there are some linear algebra shortcuts that IML uses which wouldn't work as well if missing values were possible.

Remember that 'missing' isn't actually a value in the backend; it's a particular negative value (lowest possible negative number, in fact) which SAS knows to handle in a certain way when it does math with it.

If you want to use missings, you could re-code them to a very low negative number much larger than any possible value in your data, like -999999, and then after the multiplication is done, re-code negative values (or large negative values) back.

Here's an example:

proc iml;
 xvar = {1 1,2 2};
 yvar = {-99999 2,1 1};
 zvar = xvar*yvar;
 do z=1 to nrow(zvar)    ;
   if zvar[z,1] < 0 then do;
        zvar[z,1]=.;
    end;
 end;
 print zvar;
quit;

This only works if your values are always positive, and -999999 must be big enough that it outweighs any possible addition done with it (so -1 wouldn't work, for example, as then you have -1*1+1+1=0). If you have missings in both matrices, you would have to also filter the large positive values out (and again, this would only work if your data allowed for it; many circumstances would cause this to have undesirable results.)

Joe
  • 62,789
  • 6
  • 49
  • 67
  • Thank you, Also this only works with quantiative data... not with qualitaitive one – Math Feb 25 '15 at 17:36
  • How would you multiply a matrix of qualitative data (by which I assume you mean strings?) Or do you mean something else. This should work with any numerically-represented data that doesn't happen to contain a lot of large negative numbers (of course, `<0` might need to be `<-9999` or something similar). – Joe Feb 25 '15 at 17:37
  • But in your scheme two "missing values" multiplied together would result in a large positive value, which would not get flagged as a missing value. Also, as you point out, this scheme breaksdown when the data are not positive. – Rick Feb 26 '15 at 12:20
  • @Rick It's not a particularly good option, but if one is trying to work around the issue and is satisfied with the particulars of this, it could work. (If you had missings in both datasets, you'd need to remove < -9999 and > 9999 or similar; and of course there are lots of ways this wouldn't work). – Joe Feb 26 '15 at 15:06
  • Wow. I had no idea that missing was actually a 'reserved' number. – Robert Penridge Feb 26 '15 at 19:14
  • I'm not sure if reserved number is appropriate or not, but it is stored in numerics as such, and if you use it in an inequality it is 'less' than any other numbers (and the different special missings have different values such that they have an ordered and defined relationship with each other). – Joe Feb 26 '15 at 19:26
  • Computer scientists would call is a NaN, which stands for "not a number". Software has special rules for how to interpret NaNs in computations. There are various SAS conventions, such as "smaller than any representable number," but those conventions are not absolute. In SAS/IML software, the special missing value .I is sometimes used for "positive infinity." – Rick Feb 26 '15 at 21:39
  • Interestingly, it looks like it's stored as a very large positive number, but treated by SAS specially to be less - ie, when I use `peeklong` to look up values, it's not negative but positive, varoius values (for different special missings) around `1e19`. I'd assumed they would use the negative version of that - but I guess it doesn't really matter what the actual value is. – Joe Feb 26 '15 at 22:46
0

I wrote a blog post that discusses various ways to interpret multiplication with missing values. The article "Matrix multiplication with missing values in SAS" also includes SAS/IML code that enables you to multiply matrices that contain missing values.

Rick
  • 1,210
  • 6
  • 11