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
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.
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;
what do we do if the denominator look like -1E-16 ?
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;
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.
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.
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!
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.
Ready to level-up your skills? Choose your own adventure.