BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Prateek1
Obsidian | Level 7

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Ideal use for ifn()

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

View solution in original post

9 REPLIES 9
Kurt_Bremser
Super User

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

Reeza
Super User

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. 

Kurt_Bremser
Super User

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;
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Ideal use for ifn()

average_purchase=ifn(purchases_trx=0,0,purchases/purchases_trx);
SpaceMonkey
Fluorite | Level 6

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.

Kurt_Bremser
Super User

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.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

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.

Reeza
Super User

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

Ksharp
Super User
average=divide(purchases,purchases_trx);

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 13325 views
  • 11 likes
  • 6 in conversation