BookmarkSubscribeRSS Feed
newboy1218
Quartz | Level 8

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_amtb_debt_amta_debt_pcent
2402092.307692308
95595

 

What I am getting (I don't want that, it is wrong):

a_debt_amtb_debt_amta_debt_pcent
10701.540100
84256.960100
2402092.307692308
23876.830100
95595
3 REPLIES 3
FreelanceReinh
Jade | Level 19

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);
newboy1218
Quartz | Level 8

Sorry I don't think this work.. It gives me '50' in all output now =/

FreelanceReinh
Jade | Level 19
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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 455 views
  • 0 likes
  • 2 in conversation