BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
alepage
Barite | Level 11

Imagine that I have a percentage equal to that

percentage = (A - B) / B = A/B -1

But the division by zero give the infini.
I belive that the good answer will be zero 
How to solve that issue

How to deal if the denominator is missing 

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

You may want to consider use of the DIVIDE function instead of the / operator.

The function will return special missing of .I (infinity) or .M (negative infinity) or ._ when dividing by zero, missing values or values that might cause an overflow such as very small values without notes to the log.

 

Example from the online help for Divide:

data _null_;
   a=divide(1, 0);
   put +3 a='(infinity)';
   b=divide(2, .I); 
   put +3 b=;        
   c=divide(.I, -1);
   put +3 c='(minus infinity)';
   d=divide(constant('big'), constant('small'));
   put +3 d='(infinity because of overflow)';
run;

with results:

   a=I (infinity)
   b=0
   c=M (minus infinity)
   d=I (infinity because of overflow)

Use a custom format to display desired text for the special missing values for reporting.

View solution in original post

5 REPLIES 5
Kathryn_SAS
SAS Employee

You could use an IF condition like the following:

data test;
a=25;
b=0;
if b ne 0 then percentage=(a-b)/b;
else percentage=0;
run;

proc print;
run;
alepage
Barite | Level 11

what do we do if the denominator look like -1E-16 ?

 

Kathryn_SAS
SAS Employee

That does not give a Division by zero Note. What are you expecting in this case?

data test;
a=25;
b=-1E-16;
if b ne 0 then percentage=(a-b)/b;
else percentage=0;
run;

proc print;
run;
PaigeMiller
Diamond | Level 26

percentage = (A - B) / B = A/B -1

But the division by zero give the infini.
I belive that the good answer will be zero
How to solve that issue

 

The answer really depends on what you are going to do with the percentage numbers after you compute percentage. Please tell us.

 

I don't see how zero is a better answer than the missing you will get when B=0, but maybe there are situations where zero is the best answer. PLEASE EXPLAIN.

--
Paige Miller
ballardw
Super User

You may want to consider use of the DIVIDE function instead of the / operator.

The function will return special missing of .I (infinity) or .M (negative infinity) or ._ when dividing by zero, missing values or values that might cause an overflow such as very small values without notes to the log.

 

Example from the online help for Divide:

data _null_;
   a=divide(1, 0);
   put +3 a='(infinity)';
   b=divide(2, .I); 
   put +3 b=;        
   c=divide(.I, -1);
   put +3 c='(minus infinity)';
   d=divide(constant('big'), constant('small'));
   put +3 d='(infinity because of overflow)';
run;

with results:

   a=I (infinity)
   b=0
   c=M (minus infinity)
   d=I (infinity because of overflow)

Use a custom format to display desired text for the special missing values for reporting.

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

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
  • 5 replies
  • 681 views
  • 2 likes
  • 4 in conversation