Hi, I am trying to calculate the % of A debt amount within the sum of A + B debt amount, and then I want to filter out % of A debt amount is between 90 - 100% (excluding 90% and 100%). However, in my result, it return answer where a_debt_pcent = 100.. Do you know why even though I excluded 100%?
Thanks.
data aa;
input a_debt_amt b_debt_amt;
datalines;
10701.54 0
84256.96 0
21462.54 0
240 20
23876.83 0
95 5
396 184
982 3874
0 9128
;
run;
data bb;
set aa;
a_debt_pcent = 100*a_debt_amt/(a_debt_amt+b_debt_amt);
if 90 < a_debt_pcent < 100;
run;
WANT:
a_debt_amt | b_debt_amt | a_debt_pcent |
240 | 20 | 92.307692308 |
95 | 5 | 95 |
What I am getting (I don't want that, it is wrong):
a_debt_amt | b_debt_amt | a_debt_pcent |
10701.54 | 0 | 100 |
84256.96 | 0 | 100 |
240 | 20 | 92.307692308 |
23876.83 | 0 | 100 |
95 | 5 | 95 |
Hello @newboy1218,
Computers tend to produce tiny rounding errors in calculations involving non-integer numbers (see Numerical Accuracy in SAS Software for the details or many examples on this forum). In your examples it's the division where this happens, but in general even the multiplication by 100 can be affected.
Examples (log from Windows SAS 9.4M5):
165 data _null_; 166 if 100*0.07 > 7 then put 'Surprise,'; 167 if 55/0.55 < 100 then put 'surprise!'; 168 run; Surprise, surprise! NOTE: DATA statement used (Total process time):
In most cases you can avoid such issues by using the ROUND function with an appropriate small rounding unit:
a_debt_pcent = round(100*a_debt_amt/(a_debt_amt+b_debt_amt),1e-9);
Sorry I don't think this work.. It gives me '50' in all output now =/
data bb;
set aa;
a_debt_pcent = round(100*a_debt_amt/(a_debt_amt+b_debt_amt),1e-9);
if 90 < a_debt_pcent < 100;
run;
proc print data=bb;
run;
Output:
a_debt_ b_debt_ a_debt_ Obs amt amt pcent 1 240 20 92.3077 2 95 5 95.0000
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.