BookmarkSubscribeRSS Feed
pari2189
Calcite | Level 5

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;
unit= mad(y[idx,],"nmad");
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
PaigeMiller
Diamond | Level 26

@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
pari2189
Calcite | Level 5

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.

ballardw
Super User

If

 mad(y[idx,],"nmad") returns 0

 

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

Rick_SAS
SAS Super FREQ

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 4 replies
  • 1036 views
  • 3 likes
  • 4 in conversation