Hi,
Below is output and data:
I am using "chk=put(mean,8.2);" but still I am getting 5.42 instead of 5.43. is there anything wrong with my SAS?
Thanks,
Hi @chinna0369,
@chinna0369 wrote:I am using "chk=put(mean,8.2);" but still I am getting 5.42 instead of 5.43. is there anything wrong with my SAS?
No, this can easily happen due to numeric representation issues, i.e. small rounding errors that make numeric values different from what they look like in common display formats.
Example:
data test;
mean=mean(-4.19, 15.04);
true_mean=round(mean,1e-9);
chk=put(mean,8.2);
true_chk=put(true_mean,8.2);
put mean hex16.;
put true_mean hex16.;
run;
proc print data=test noobs;
run;
Log:
1185 put mean hex16.; 1186 put true_mean hex16.; 1187 run; 4015B33333333332 4015B33333333333
Output:
true_ mean mean chk true_chk 5.425 5.425 5.42 5.43
This is exactly what you observed, although mathematically the mean of -4.19 and 15.04 is, of course, 5.425, which is displayed as 5.43 with format 8.2, but not if the internal value is slightly less than 5.425 because of rounding errors (see log excerpt above).
In the above example even the BEST18. format reveals the rounding error in variable MEAN, but this is not always the case.
Hi @chinna0369,
@chinna0369 wrote:I am using "chk=put(mean,8.2);" but still I am getting 5.42 instead of 5.43. is there anything wrong with my SAS?
No, this can easily happen due to numeric representation issues, i.e. small rounding errors that make numeric values different from what they look like in common display formats.
Example:
data test;
mean=mean(-4.19, 15.04);
true_mean=round(mean,1e-9);
chk=put(mean,8.2);
true_chk=put(true_mean,8.2);
put mean hex16.;
put true_mean hex16.;
run;
proc print data=test noobs;
run;
Log:
1185 put mean hex16.; 1186 put true_mean hex16.; 1187 run; 4015B33333333332 4015B33333333333
Output:
true_ mean mean chk true_chk 5.425 5.425 5.42 5.43
This is exactly what you observed, although mathematically the mean of -4.19 and 15.04 is, of course, 5.425, which is displayed as 5.43 with format 8.2, but not if the internal value is slightly less than 5.425 because of rounding errors (see log excerpt above).
In the above example even the BEST18. format reveals the rounding error in variable MEAN, but this is not always the case.
Hi I have one more question?
I have a value as "-2.22045E-16" then I want this value as "-2.2E-16". How can I do this?
Thank you in advance!
To display this value differently use a different format:
data test;
x=0.2+1.4-1.6;
put x best12.;
put x e8.;
run;
Result:
-2.22045E-16 -2.2E-16
Note from the above example that this value (-2**(-52)) frequently occurs as a consequence of numeric representation issues. Appropriate rounding can avoid such values in many cases.
data _null_;
x=0.2+1.4-1.6;
r=round(x,1e-9);
put x hex16.;
put r hex16.;
put r=;
run;
Result:
BCB0000000000000
0000000000000000
r=0
What format is currently assigned to the MEAN variable?
If the actual value for the mean is 5.42456 then a display of 3 decimals would "round" to 5.425 for display but the 8.2 would be 5.42 because the 0.00456 portion is less than .005
or if the value is something like 5.2499999999 then almost any format that uses fewer than 10 decimal places will round to 5.425 but the 8.2 decimal rounds down at the second decimal.
data junk; x=5.424999999999; put 'BEST12 format' x=best12.; put 'F8.2 format' x= f8.2; run;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.