Division by zero error

Accepted Solution Solved
Reply
Contributor
Posts: 50
Accepted Solution

Division by zero error

[ Edited ]

hi,

i am getting this kind of error:

R: Limit set by ERRORS= option reached. Further errors of this type will not be printed.

Division by zero detected at line 88 column 27

 

Mathematical operations could not be performed at the following places. The results of the
operations have been set to missing values.
Each place is given by: (Number of times) at (Line)Smiley SadColumn).
2044 at 80:28
NOTE:

my code is :

data Y.Ce;
set Y.Cc;
Average_Purchase=(Purchases/Purchases_trx);
run;

 


Accepted Solutions
Solution
‎10-11-2016 06:13 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Division by zero error

Ideal use for ifn()

average_purchase=ifn(purchases_trx=0,0,purchases/purchases_trx);

View solution in original post


All Replies
Esteemed Advisor
Posts: 6,646

Re: sas

[ Edited ]

Prateek1 wrote:

hi,

i am getting this kind of error:

R: Limit set by ERRORS= option reached. Further errors of this type will not be printed.

Division by zero detected at line 88 column 27

 

Mathematical operations could not be performed at the following places. The results of the
operations have been set to missing values.
Each place is given by: (Number of times) at (Line)Smiley SadColumn).
2044 at 80:28
NOTE:

my code is :

data Y.Ce;
set Y.Cc;
Average_Purchase=(Purchases/Purchases_trx);
run;

 


The NOTE about the Limits is irrelevant. What's really important is/are the error message(s) BEFORE that.

Please post the whole log of the data step (redundant repeated messages can be omitted).

 

Cancel that. "Division by zero" message not detected because of insufficient caffeine level. EBKAC.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Grand Advisor
Posts: 17,338

Re: sas

The Error is:

Division by zero detected at line 88 column 27

 

Is this happening in your calculation? If so, fix it so it doesn't. 

Esteemed Advisor
Posts: 6,646

Re: Division by zero error

The culprit is this:

Average_Purchase=(Purchases/Purchases_trx);

If Purchases_trx happens to be zero, this is an invalid calculation. A division by zero yields (per mathematical definition) infinity, which cannot be handled by SAS (and most other programming environments).

So you need to either

- remove observations where Purchases_trx is zero

- or test if Purchases_trx is zero and than set a "default" value

In your case I'd do

if Purchases_trx = 0
then Average_Purchase = 0;
else Average_Purchase = Purchases / Purchases_trx;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Solution
‎10-11-2016 06:13 AM
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Division by zero error

Ideal use for ifn()

average_purchase=ifn(purchases_trx=0,0,purchases/purchases_trx);
New Contributor
Posts: 4

Re: Division by zero error

IFN does not seem to prevent divide by zero errors when I use it. For example:

 

 

data test;
  x = 0;
  y = ifn(x = 0, 0, 1/x);
run;

 

 

This still generates an error in the log:

 

NOTE: Division by zero detected at line 35 column 22.
x=0 y=0 _ERROR_=1 _N_=1

 

 

Why? If X=0 then IFN should return 0 (second argument). The 1/X condition should never be evaluated.

 

Can someone please explain? Thanks very much.

Esteemed Advisor
Posts: 6,646

Re: Division by zero error

It is a peculiarity of the ifn and ifc functions that they seem to always evaluate all expressions; this comes in handy when you need to conditionally use the result of a lag() function.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Esteemed Advisor
Esteemed Advisor
Posts: 7,203

Re: Division by zero error

Its because its a function.  Normally the if statement is processed and stops when it needs to, i.e:

if x=1 then do abc=3;

   ^  ^

The compiler evaluates this part, and only moves onto the then if that is true.  The compiled function however is treated as a block ie:

abc=ifn(x=1,3,0)

       ^             ^

The compiler treats the whole lot as condition/result due to being a function.

 

@SpaceMonkey: Please dont re-open 8 month old topics for a new discussion, start a new topic.

Grand Advisor
Posts: 17,338

Re: Division by zero error


SpaceMonkey wrote:

 

If a thread is open, I'll comment on it. If you don't like that, tough for you.


@SpaceMonkey Threads are never closed on here. 

Grand Advisor
Posts: 9,576

Re: Division by zero error

average=divide(purchases,purchases_trx);
☑ This topic is SOLVED.

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

Discussion stats
  • 9 replies
  • 786 views
  • 8 likes
  • 6 in conversation