DATA Step, Macro, Functions and more

I am getting a weird number after doing a sum and not sure how to stop it from happening

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 16
Accepted Solution

I am getting a weird number after doing a sum and not sure how to stop it from happening

I have the following code that I am working with:

data waiver2(keep=pidm_key tw_amount tbraccd_detail_code tbraccd_term_code);
retain tw_amount;
set waiver;
by pidm_key tbraccd_detail_code tbraccd_term_code;
if first.tbraccd_term_code then tw_amount=0;
tw_amount + tbraccd_amt;
if last.tbraccd_term_code;
if tw_amount>= 0.01;
run;

 

The data that I have when I go into the tw_amount + tbraccd_amt is:

tw_amount = 464
pidm_key = 233793
tbraccd_amt = -464
TBRACCD_DETAIL_CODE = WFOS
TBBDETC_DESC = Waiver CR: MD Foster Care
TBRACCD_TERM_CODE = 201540
FIRST.pidm_key = 0
LAST.pidm_key = 1
FIRST.TBRACCD_DETAIL_CODE = 0
LAST.TBRACCD_DETAIL_CODE = 1
FIRST.TBRACCD_TERM_CODE = 0
LAST.TBRACCD_TERM_CODE = 1

 

After the addition my data looks like this:

tw_amount = -5.6843418860808E-14
pidm_key = 233793
tbraccd_amt = -464
TBRACCD_DETAIL_CODE = WFOS
TBBDETC_DESC = Waiver CR: MD Foster Care
TBRACCD_TERM_CODE = 201540
FIRST.pidm_key = 0
LAST.pidm_key = 1
FIRST.TBRACCD_DETAIL_CODE = 0
LAST.TBRACCD_DETAIL_CODE = 1
FIRST.TBRACCD_TERM_CODE = 0
LAST.TBRACCD_TERM_CODE = 1

 

What is going on & how can I avoid this as I want to change the last if statement to not = 0?

 

Thank you,


Accepted Solutions
Solution
‎11-02-2016 02:02 PM
Super User
Posts: 19,768

Re: I am getting a weird number after doing a sum and not sure how to stop it from happening

Posted in reply to HarfordKaren

Then round to the nearest cent...

 

round(variable, 0.01)

View solution in original post


All Replies
Super User
Posts: 19,768

Re: I am getting a weird number after doing a sum and not sure how to stop it from happening

Posted in reply to HarfordKaren

You're running into numerical precision issues. 

Round the value before comparison.

 

if round(tw_amount) = 0 then ...

Occasional Contributor
Posts: 16

Re: I am getting a weird number after doing a sum and not sure how to stop it from happening

The number is a dollar amount so if I round it I will lose the cents. How do I deal with that?
Solution
‎11-02-2016 02:02 PM
Super User
Posts: 19,768

Re: I am getting a weird number after doing a sum and not sure how to stop it from happening

Posted in reply to HarfordKaren

Then round to the nearest cent...

 

round(variable, 0.01)

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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