DATA Step, Macro, Functions and more

when is 0.245 not 0.245?

Reply
New Contributor
Posts: 3

when is 0.245 not 0.245?

Hi everybody! 

 

I'm not getting this:

I created a data set via a do-loop, data x, below. 

When I use a w.d format on the value that I 'see' in the output data(value), the value is rounded up(fvalue).

When I create a hard value for 0.245 and use the same w.d format, the value is rounded down(fvalue2).

Clearly, SAS does not see the raw value of 0.245 coming from the data as 0.245. 

Question is why?

 

Thanks for considering and I hope this is clear.

 

Jon

 

 

data x;

do i= .01 to .99 by .0001;

value = i;

output;

end;

run;

 

data y;

      set x;

      fvalue=value;

      fvalue2=0.2450;

      rawvalue=fvalue2;

      format fvalue fvalue2 8.2;

      drop i;

run;

 

                                    when is 0.245 not 0.245?                       

 

                    Obs    value      fvalue    rawvalue  fvalue2 

 

                      1      0.245        0.24       0.245          0.25

PROC Star
Posts: 2,215

Re: when is 0.245 not 0.245?

It's actually worse than you think.

Run this:

data x;
  do X= .244 to .245 by .0001;                     
    Y=round(X,0.0001);
    output;                         
  end;         
  format X Y 20.16;
run;

to obtain (on a PC):

 

X Y
0.2440000000000000 0.2440000000000000
0.2441000000000000 0.2441000000000000
0.2442000000000000 0.2442000000000000
0.2443000000000000 0.2443000000000000
0.2444000000000000 0.2444000000000000
0.2444999999999900 0.2445000000000000
0.2445999999999900 0.2446000000000000
0.2446999999999900 0.2447000000000000
0.2447999999999900 0.2448000000000000
0.2448999999999900 0.2449000000000000
0.2449999999999900 0.2450000000000000

Read about numeric precision to know more.

For example here or here.

 

 

 

Ask a Question
Discussion stats
  • 1 reply
  • 62 views
  • 1 like
  • 2 in conversation