Solved
Contributor
Posts: 50

# 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)Column).
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
Super User
Posts: 9,840

## Re: Division by zero error

Ideal use for ifn()

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

All Replies
Super User
Posts: 10,571

## 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)Column).
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
How to convert datasets to data steps
How to post code
Super User
Posts: 24,004

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

Super User
Posts: 10,571

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

``````if Purchases_trx = 0
then Average_Purchase = 0;
else Average_Purchase = Purchases / Purchases_trx;``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎10-11-2016 06:13 AM
Super User
Posts: 9,840

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

Super User
Posts: 10,571

## 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
How to convert datasets to data steps
How to post code
Super User
Posts: 9,840

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

Super User
Posts: 24,004

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

Super User
Posts: 10,850

## Re: Division by zero error

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