[1] 1 2 3 -3
Rounding numbers
SAS ROUND() and w.d formats, written in R with sas_round() and sas_format().
round() in R gives different answers than ROUND() in SAS, and how to get the SAS results in R.
SAS ROUND(x, unit), w.d formats
Agreement with SAS 99.95% of ROUND() results, 99.40% of 32.d results
R’s round() and SAS’s ROUND() often give different answers. This is not a bug in either language. They follow different rules, and both work with numbers that the computer cannot store exactly. If you move a SAS program to R and use round(), some values in your tables can change in the last decimal place.
segue has two functions that give the SAS results in R:
-
sas_round()works like the SASROUND()function. -
sas_format()prints numbers like a SASw.dformat, such as8.2.
Round to a whole number
data halves;
input x;
whole = round(x);
datalines;
0.5
1.5
2.5
-2.5
;
run;SAS rounds a value that is exactly halfway to the number farther from zero, so 2.5 becomes 3. R’s round() follows the IEC 60559 standard, which rounds halves to the even number, so 2.5 becomes 2:
| x | SAS ROUND(x) | sas_round(x) | round(x) |
|---|---|---|---|
| 0.5 | 1 | 1 | 0 |
| 1.5 | 2 | 2 | 2 |
| 2.5 | 3 | 3 | 2 |
| -2.5 | -3 | -3 | -2 |
Round to two decimal places
data cents;
input x;
cents = round(x, 0.01);
datalines;
1.005
2.675
99.995
;
run;Here the two languages differ for a second reason. The number 2.675 cannot be stored exactly as a binary double. The computer keeps the closest value it can, which is slightly less than 2.675:
sprintf("%.20f", 2.675)[1] "2.67499999999999982236"
R’s round(2.675, 2) rounds that stored value, so the answer is 2.67. SAS treats values that are very close to halfway as halfway. The SAS documentation calls this “fuzzing”. So ROUND(2.675, 0.01) is 2.68, which is the answer you get when you round by hand.
| x | SAS ROUND(x, 0.01) | sas_round(x, 2) | round(x, 2) |
|---|---|---|---|
| 1.005 | 1.01 | 1.01 | 1.00 |
| 2.675 | 2.68 | 2.68 | 2.67 |
| 99.995 | 100.00 | 100.00 | 100.00 |
sas_round() takes either digits, as round() does, or unit, as ROUND() does. Use unit for units that are not powers of ten:
quarter = round(x, 0.25);
sas_round(2.675, unit = 0.25)[1] 2.75
Print numbers like a SAS format
A SAS format such as 8.2 does not use ROUND(). It has its own rules, so segue has a separate function for it.
data _null_;
input x;
put x 8.2;
datalines;
1.005
2.675
99.995
-0.001
;
run;
x <- c(1.005, 2.675, 99.995, -0.001)
sas_format(x, digits = 2)[1] " 1.01" " 2.68" "100.00" " -0.00"
Three rules to keep in mind:
-
Halfway values round up. As with
ROUND(), 2.675 prints as2.68. But formats use a much smaller tolerance thanROUND(), sosas_format()does not callsas_round(). -
Negative values keep their sign. SAS prints
-0.001as-0.00.format(round(-0.001, 2), nsmall = 2)gives"0.00". -
At most 15 significant digits. When a format needs more digits, SAS rounds to 15 significant digits and pads with zeros.
sas_format()does the same.
DECIMALCONV= setting
These rules hold for the default SAS system option DECIMALCONV=COMPATIBLE. With DECIMALCONV=STDIEEE, SAS formats the exact binary value, so 2.675 prints as 2.67. sas_format() reproduces COMPATIBLE only.
| x | 8.2 with COMPATIBLE | 8.2 with STDIEEE | sas_format(x, 2) |
|---|---|---|---|
| 1.005 | 1.01 | 1.00 | 1.01 |
| 2.675 | 2.68 | 2.67 | 2.68 |
| 99.995 | 100.00 | 100.00 | 100.00 |
How close are the results?
The comparison ran SAS’s ROUND() on about 23,000 test values with 59 rounding units, then ran each R option on the same doubles. The values were read into R bit for bit from the SAS data sets, so any difference comes from the rounding and not from the inputs. The test values include every value with one decimal place in [-100, 100], every value with three decimal places in [-3, 3], and values a few units in the last place away from halfway points.
ROUND(x, unit) results that each R option reproduces exactly.
sas_round() gives the same double as SAS for every value with up to three decimal places. The few differences are values of 1e15 or more, or values divided by a tiny unit such as 1e-15. Those numbers have more than 15 significant digits, and SAS is not exact there either: ROUND(1000000000000000.5, 0.5) is 1000000000000001, although that value is already a multiple of 0.5.
For formats, sas_format() matches SAS’s 32.d output for 99.40% of 100,516 results. sprintf() matches 96.98%. The differences are values within a few units in the last place of a halfway point, and values shown with 15 or more digits, where SAS’s own digits are not consistent.
Other SAS rounding functions
segue does not have functions for these yet. The closest R functions are:
| SAS | R | Notes |
|---|---|---|
ROUNDE(x, unit) |
none | Rounds halves to even, like round(), but with SAS’s fuzzing. |
ROUNDZ(x, unit) |
none | No fuzzing. ROUNDZ(2.675, 0.01) is still 2.68 because SAS divides by the unit first. |
INT(x) |
trunc(x) |
SAS treats values within 1e-12 of an integer as that integer. |
FLOOR(x), CEIL(x)
|
floor(x), ceiling(x)
|
Same fuzzing as INT(). FLOOR(1 - 1e-12) is 1. |
INTZ, FLOORZ, CEILZ
|
trunc(), floor(), ceiling()
|
No fuzzing. Identical except for subnormal numbers, which SAS treats as 0. |
R’s parser can turn some decimal text into a slightly different double than SAS does. For example, 1e300 in R is a few units in the last place away from SAS’s 1e300. If you compare R and SAS results in a test, read the SAS values from a .sas7bdat file with haven::read_sas() instead of typing them.