[1] 0 2 2 -2
sas_round(x)[1] 1 2 3 -3
round(1.005, digits = 2)[1] 1
sas_round(1.005, digits = 2)[1] 1.01
sas_round(1234.5, digits = -2)[1] 1200
sas_round(pi, unit = 1 / 7) == 22 / 7[1] TRUE
sas_round() replicates the SAS ROUND() function.
sas_round(x, digits = 0, unit = 10^-digits)| Arguments | Description |
|---|---|
| x | A numeric vector. |
| digits | A single whole number between -15 and 15 for the number of decimal places, as in round(). Negative values round to a power of ten (e.g., digits = -2 rounds to the nearest hundred). |
| unit | A single positive number for the rounding unit (the rounding-unit argument of the SAS function). The default, 10^-digits, is determined by digits. Use unit for units that are not powers of ten, such as 0.25 or 1/7. Supply only one of digits and unit. As in SAS, units at or below .Machine$double.xmin (about 2.2e-308) are not allowed. |
R’s round() and SAS’s ROUND() can give different answers for two reasons:
• When a value is halfway between two multiples of unit, round() returns the even multiple (e.g., round(2.5) is 2), while SAS returns the multiple farther from zero (3).
• Many decimal values do not have an exact binary representation. For example, 1.005 is stored as slightly less than 1.005, so round(1.005, 2) is 1. SAS treats values that are within a small relative tolerance of the halfway point as being halfway (called “fuzzing”), so ROUND(1.005, 0.01) is 1.01.
sas_round() follows the algorithm described in the SAS documentation:
• x / unit is rounded half away from zero, after adding a tolerance of min(1e-6, abs(x / unit) * 1e-12) to its absolute value.
• When unit is the reciprocal of an integer (e.g., 0.01 or 1/7), the rounded value of x / unit is divided by that integer so that the result matches the decimal value (sas_round(0.33, 0.1) is identical to 0.3). As in SAS, 1 / unit must be within 16 units in the last place of the integer.
• When unit is not an integer but has at most four decimal places, the result is computed with integer arithmetic so that it also matches the decimal value.
• Otherwise, the result is the rounded integer times unit. When that product is within a few units in the last place of a decimal with at most four decimal places, the decimal value is returned instead (sas_round(0.0015, unit = 0.00015) is identical to 0.0015).
• When abs(x / unit) is at least 2^52, x is returned unchanged.
Missing values are returned as missing.
These results were compared with about 70,000 results of the SAS ROUND() function (SAS Viya, V.04.00M0P060826), including every value with up to three decimal places between -3 and 3, and every case in the package’s tests. They are identical except when abs(x) or abs(x / unit) is 1e15 or more. There, values have more than 15 significant digits and SAS is not exact either: for example, ROUND(1000000000000000.5, 0.5) is 1000000000000001, although the value is already a multiple of 0.5.
A double vector the same length as x.
SAS Institute Inc. “ROUND Function.” SAS Functions and CALL Routines: Reference.