Calcite | Level 5

## IF-THEN statement is SAS IML when there are zeros in matrix

I'm trying to perform logical operation through if-then statement but since my matrix has some value sas zero it fails to perform the operation correctly

``````Start M_1(y);
p=13;
outarray_M=j(nrow(y),ncol(y),.);
outarray_01=j(nrow(y),ncol(y),.);
do i=14 to (nrow(y)-1);
idx= max(2,i-p+1):i;
outarray_M[i,]=unit;
if all(outarray_M[i,] ^= 0) then outarray_01[i+1,] = abs((y[i+1,]-median(y[idx,])))/mad(y[idx,],"nmad");
else outarray_01[i+1,]= choose(y[i+1,] =0, 0, 9999);
end;
return(outarray_01);
finish;``````
4 REPLIES 4
Diamond | Level 26

## Re: IF-THEN statement is SAS IML when there are zeros in matrix

@pari2189 wrote:

I'm trying to perform logical operation through if-then statement but since my matrix has some value sas zero it fails to perform the operation correctly

Can you show us the SASLOG that contains the error?

--
Paige Miller
Calcite | Level 5

## Re: IF-THEN statement is SAS IML when there are zeros in matrix

There is no error but the results are incorrect.

So what I want is if it is a non-zero value perform the calculation as per the formula and if it is zero go to the choose condition. But since  if-then statement returns false because some values in matrix are zero then it never performs the calculation and goes to choose condition directly.

Super User

## Re: IF-THEN statement is SAS IML when there are zeros in matrix

If

then you have a division by 0 which is double-plus-ungood.

SAS Super FREQ

## Re: IF-THEN statement is SAS IML when there are zeros in matrix

I think ballardw mailed it: The MAD function will return 0 when more than half of the values are the same value.

In addition, I'd like to point out that the outarray_M matrix is not necessary in your code. Hopefully, that code is left over from a more complicated example (thanks for supplying a short module for us to view!). If not, you can use

if unit ^= 0 then...

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
• 4 replies
• 921 views
• 3 likes
• 4 in conversation