Hi Experts,
Would like to consult regarding the round function.
Given the value 123.456 for a, how come this turns out to be the value when I use the round function.
ROUND parameter a = 123.456 | Result |
---|---|
b = round(a,0.01) | 123.46 (ok) |
b = round(a,0.02) | 123.46 (ok) |
b = round(a,0.03) | 123.45 (?) |
b = round(a,0.04) | 123.44 (?) |
b = round(a,0.05) | 123.45 (?) |
b = round(a,0.06) | 123.48 (?) |
b = round(a,0.07) | 123.48 (?) |
b = round(a,0.08) | 123.44 (?) |
b = round(a,0.09) | 123.48 (?) |
Is there a bug on the round function or is there something I am not aware on how it derives into these results. Thanks!
The second number in the SAS round function specifies the units that should be rounded to, so for example round(123.456, .03) says round the number to the nearest multiple of .03 . There are 4115.2 multiples of .03 in the number 123.456; rounding this to the nearest integer gives 4115 multiples. Multiplying back correctly gives the resultant 123.45, and so for the other values of rounding unit.
Initial value | Rounding Unit | Number of Multiples | Multiples Rounded | Multiplied back |
123.456 | 0.01 | 12345.6 | 12346 | 123.46 |
123.456 | 0.02 | 6172.8 | 6173 | 123.46 |
123.456 | 0.03 | 4115.2 | 4115 | 123.45 |
123.456 | 0.04 | 3086.4 | 3086 | 123.44 |
123.456 | 0.05 | 2469.12 | 2469 | 123.45 |
123.456 | 0.06 | 2057.6 | 2058 | 123.48 |
123.456 | 0.07 | 1763.657143 | 1764 | 123.48 |
123.456 | 0.08 | 1543.2 | 1543 | 123.44 |
123.456 | 0.09 | 1371.733333 | 1372 | 123.48 |
The second number in the SAS round function specifies the units that should be rounded to, so for example round(123.456, .03) says round the number to the nearest multiple of .03 . There are 4115.2 multiples of .03 in the number 123.456; rounding this to the nearest integer gives 4115 multiples. Multiplying back correctly gives the resultant 123.45, and so for the other values of rounding unit.
Initial value | Rounding Unit | Number of Multiples | Multiples Rounded | Multiplied back |
123.456 | 0.01 | 12345.6 | 12346 | 123.46 |
123.456 | 0.02 | 6172.8 | 6173 | 123.46 |
123.456 | 0.03 | 4115.2 | 4115 | 123.45 |
123.456 | 0.04 | 3086.4 | 3086 | 123.44 |
123.456 | 0.05 | 2469.12 | 2469 | 123.45 |
123.456 | 0.06 | 2057.6 | 2058 | 123.48 |
123.456 | 0.07 | 1763.657143 | 1764 | 123.48 |
123.456 | 0.08 | 1543.2 | 1543 | 123.44 |
123.456 | 0.09 | 1371.733333 | 1372 | 123.48 |
Wow, This is awesome! I did not know that. Learn some. Thanks a lot!
Haikuo
This is totally great explanation regarding the round function. I believe most are not also aware of this. Thank you very much! You have just expanded my knowledge!
I can envisage there might be a situation where round(x, .02) or round(x, .05) might be required but I cannot imagine why anyone would want round(x, .03) or round(x, .07). However, it is there if needed. You can also round to other fractions like quarters, round(x, .25) or eighths, round(x, .125) etc.
Rounding a whole number by say .07 can yield unexpected results: round(123, .07) = 122.99 ( ! ).
milts, thanks for your comment. Can you mark this answered?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.