I have noticed a discrepancy when applying a format to a mean where the last digit is 5. In the example below two means are calculated, both giving a result of exactly 90.55. However when the results are formatted to 6.1, one mean shows as 90.5 and the other as 90.6.
data problem;
input gp x;
datalines;
1 92.8
1 93.4
1 94.6
1 81.4
2 90.55
2 90.55
2 90.55
2 90.55
;
run;
proc means data=problem mean;
by gp;
var x;
output out=means mean=x;
run;
proc print data=means;
format x 6.1;
run;
The output from this code is as follows:
--------------------------------------------- gp=1 --------------------------------------------- The MEANS Procedure Analysis Variable : x Mean 90.5500000 --------------------------------------------- gp=2 --------------------------------------------- Analysis Variable : x Mean 90.5500000 Obs gp _TYPE_ _FREQ_ x 1 1 0 4 90.5 2 2 0 4 90.6
Can anyone explain this please?
This is due to rounding error, which happens when computing means and when changing the number of decimal places. This is usually unavoidable when working with fractions/decimals that are not a negative power of 2, or multiples thereof.
If you change the format of X to best32., you will see
gp x 1 90.5499999999999 2 90.55
and so to round to one decimal place, x for gp=1 is 90.5, while for gp=2 rounded to one decimal place x is 90.6.
If you really don't want this to happen, and you need x to one decimal place without this rounding error, you can first run this and then to one decimal place you get the same answer.
data means;
set means;
x=round(x,0.01);
run;
This is due to rounding error, which happens when computing means and when changing the number of decimal places. This is usually unavoidable when working with fractions/decimals that are not a negative power of 2, or multiples thereof.
If you change the format of X to best32., you will see
gp x 1 90.5499999999999 2 90.55
and so to round to one decimal place, x for gp=1 is 90.5, while for gp=2 rounded to one decimal place x is 90.6.
If you really don't want this to happen, and you need x to one decimal place without this rounding error, you can first run this and then to one decimal place you get the same answer.
data means;
set means;
x=round(x,0.01);
run;
Thank you @PaigeMiller
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.