turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- General Programming
- /
- Division by zero error

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

10-11-2016 02:22 AM - last edited on 10-11-2016 03:05 AM by Reeza

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

10-11-2016 04:48 AM

Ideal use for ifn()

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

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Prateek1

10-11-2016 02:43 AM - edited 10-11-2016 03:33 AM

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

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Prateek1

10-11-2016 03:03 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Prateek1

10-11-2016 03:22 AM

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

How to convert datasets to data steps

How to post code

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

Solution

10-11-2016
06:13 AM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

10-11-2016 04:48 AM

Ideal use for ifn()

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

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-25-2017 01:45 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to SpaceMonkey

05-25-2017 04:17 AM

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

Maxims of Maximally Efficient SAS Programmers

How to convert datasets to data steps

How to post code

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to KurtBremser

05-25-2017 04:39 AM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

05-28-2017 09:10 PM

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.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Prateek1

10-11-2016 07:34 AM

average=divide(purchases,purchases_trx);