ROUND() and SAS formats round an exact half away from zero: 6.25 becomes 6.3. R's round() rounds an exact half to the even digit: 6.25 becomes 6.2. When production is in SAS and QC is in R (or the other way round), the AE table percentages don't match. The fix is one small function in R, applied to every displayed number.This is one of the first mismatches teams hit when they start double programming TLFs in R. The counts match. The denominators match. The percentages are off by 0.1 in a few cells, and only in a few. Programmers spend time checking the population flags and the ADAE merge before anyone looks at the rounding.
The table that doesn't match
A standard AE summary: subjects with each preferred term, n (%), percentages to one decimal place. The placebo arm has N = 80 in the safety population.
| Preferred term | n | Exact % |
|---|---|---|
| Headache | 5 | 6.25 |
| Nausea | 9 | 11.25 |
| Dizziness | 3 | 3.75 |
| Fatigue | 1 | 1.25 |
| Diarrhoea | 6 | 7.5 |
With N = 80, every percentage is a multiple of 0.25, so it ends in .00, .25, .50 or .75. Every odd count lands exactly halfway between two one-decimal values, and those ending in .25 (n = 1, 5, 9, 13 and so on) are where SAS and R disagree.
Will this hit your study? A tie at one decimal place can only happen when N is a multiple of 16 (48, 80, 96, 160, 240...). A tie at whole percent can only happen when N is a multiple of 8 (40, 80, 120, 200...). Medians and means follow a different pattern: a median of an even number of values often ends in exactly .5, whatever the N.
Show the code that creates these counts (SAS and R)
data aecnt;
length aedecod $20;
n_tot = 80;
aedecod = "Headache"; n = 5; output;
aedecod = "Nausea"; n = 9; output;
aedecod = "Dizziness"; n = 3; output;
aedecod = "Fatigue"; n = 1; output;
aedecod = "Diarrhoea"; n = 6; output;
run;
library(dplyr)
aecnt <- tibble(
AEDECOD = c("Headache", "Nausea", "Dizziness", "Fatigue", "Diarrhoea"),
n = c(5, 9, 3, 1, 6),
N_TOT = 80
)
Rounding in SAS and R, side by side
data aepct;
set aecnt;
pct = 100 * n / n_tot;
pct_r = round(pct, 0.1);
pct_txt = put(pct, 5.1);
run;
aepct <- aecnt %>%
mutate(
pct = 100 * n / N_TOT,
pct_r = round(pct, 1),
pct_txt = sprintf("%.1f", pct)
)
Expected output:
| Preferred term | Exact % | SAS | R round() | Match? |
|---|---|---|---|---|
| Headache | 6.25 | 6.3 | 6.2 | No |
| Nausea | 11.25 | 11.3 | 11.2 | No |
| Dizziness | 3.75 | 3.8 | 3.8 | Yes |
| Fatigue | 1.25 | 1.3 | 1.2 | No |
| Diarrhoea | 7.5 | 7.5 | 7.5 | Yes |
That pattern is what makes the problem confusing in QC: some halves match and some don't. Dizziness matches because 3.75 rounds up to 3.8 under both rules. Headache doesn't, because R rounds 6.25 down to the even digit 2.
sprintf("%.1f", 6.25) also gives "6.2" on current R builds. It formats the stored binary value through the C library, which breaks an exact tie to even. The result can depend on the platform, so don't use it to "fix" the difference.
Why they differ
Type any number, choose the decimal places, and watch where each language sends it. Or tap an example.
Rounding 6.25 to 1 decimal: between 6.2 and 6.3
Stored by the computer as 6.25
Different result: this cell fails QC.
With the round_half_up() function from this post, R gives 6.3, the same as SAS.
SAS: half away from zero
ROUND(x, 0.1) returns the nearest multiple of 0.1. When x is exactly halfway, SAS goes away from zero: 6.25 becomes 6.3 and -6.25 becomes -6.3. The w.d format in PUT() and PROC REPORT gives the same result for exact halves, which is why most SAS TLF code never shows a difference between rounding first and formatting directly.
R: two cases, not one
"R rounds to even" is only half of the story. What R does depends on how the number is stored in binary.
Case 1: the half is stored exactly. Values like 0.5, 2.5, 6.25, 12.5 and 0.125 are exact in binary. For these, the R documentation for round() says the IEC 60559 rule is expected: go to the even digit. So round(0.5) is 0, round(1.5) is 2, round(2.5) is 2 and round(6.25, 1) is 6.2. This is sometimes called banker's rounding. Over many values it avoids a small upward bias, which is why the standard prefers it. It is not what SAS outputs or what most SAPs expect.
Case 2: the half is not stored exactly. Most decimals that look like a half, such as 0.15, 0.35 or 2.675, can't be stored exactly. The computer keeps the nearest binary value, which is a tiny bit below or above the half. R rounds that stored value to the nearest result, and the even rule doesn't come into it. The result can end in an odd digit.
| R code | Stored as | R gives | SAS ROUND | Why R gives that |
|---|---|---|---|---|
round(6.25, 1) | 6.25 exactly | 6.2 | 6.3 | Exact half, goes to even |
round(0.125, 2) | 0.125 exactly | 0.12 | 0.13 | Exact half, goes to even |
round(0.15, 1) | 0.149999999... | 0.1 | 0.2 | Just below the half, goes down |
round(0.35, 1) | 0.349999999... | 0.3 | 0.4 | Just below the half, goes down (3 is odd) |
round(2.675, 2) | 2.674999999... | 2.67 | 2.68 | Just below the half, goes down |
round(0.45, 1) | 0.450000000...011 | 0.5 | 0.5 | Just above the half, goes up (matches SAS) |
SAS ROUND allows a small fuzz, so it rounds the decimal number as you typed it, and every half goes away from zero. That is why the R function below adds a tolerance. You can check any value in the calculator above: it shows the stored value and which case applies.
What this means for AE tables: when you compute 100 * n / N and N is below 2000, a tie at one decimal always ends in .25 or .75, such as 5/80 = 6.25%. Those values are exact in binary, so they are Case 1 and the "even digit" pattern holds. Ties that end in .05, .15, .35 and so on (Case 2) need N to be a multiple of 2000, for example 3/2000 = 0.15%. A tie at whole percent always ends in .5, which is also exact.
SAS can do it R's way too
SAS has a function for half to even: ROUNDE(). rounde(6.25, 0.1) returns 6.2. Teams rarely use it, but it is useful when you need to reproduce an R result in SAS.
The fix: round half away from zero in R
Write the rounding rule once, put it in the study's R utilities, and use it for every number that ends up in a table, listing or figure label:
/* SAS already rounds half away from zero.
Round once, at display time, from the full-precision value. */
data aepct;
set aecnt;
pct = 100 * n / n_tot;
pct_txt = put(round(pct, 0.1), 5.1);
run;
round_half_up <- function(x, digits = 0) {
z <- abs(x) * 10^digits
z <- trunc(z + 0.5 + sqrt(.Machine$double.eps))
sign(x) * z / 10^digits
}
aepct <- aecnt %>%
mutate(
pct = 100 * n / N_TOT,
pct_txt = sprintf("%.1f", round_half_up(pct, 1))
)
Expected output (both languages):
| Preferred term | n (%) |
|---|---|
| Headache | 5 (6.3) |
| Nausea | 9 (11.3) |
| Dizziness | 3 (3.8) |
| Fatigue | 1 (1.3) |
| Diarrhoea | 6 (7.5) |
The name and the code follow janitor::round_half_up(). Despite "up" in the name, it is symmetric: -2.5 becomes -3, exactly like SAS ROUND. Two details in it matter:
abs()andsign()make it symmetric, so a change from baseline of -2.5 becomes -3, the same as SAS. A plainfloor(x + 0.5)would give -2.sqrt(.Machine$double.eps)is a tiny tolerance (about 1.5e-8). Many decimals can't be stored exactly in binary, so a value you think is 2.675 is stored as slightly less. The tolerance lets it round as the decimal number you see.
If the study already uses the janitor package, janitor::round_half_up(x, digits) applies the same rule, so you don't need your own copy.
Where else it shows up
- Means and medians in demographics and lab summaries. A mean of 64.25 kg shown to one decimal is 64.2 in R and 64.3 in SAS. The same happens to a median of 72.5 years if the shell shows age summaries with no decimals.
- Change from baseline. Negative halves are where a home-made
floor(x + 0.5)breaks. - Confidence limits and p-values printed to 2 or 3 decimals.
- Derived analysis values that the SAP says to round before analysis, for example a BMI or a dose. A different rounding here changes the analysis, not just the display.
Common mistakes
- Rounding twice. Rounding 6.249 to two decimals gives 6.25, then rounding to one decimal gives 6.3. The correct one-decimal value is 6.2. Keep full precision in the dataset and round once, at display.
- Fixing the QC program to match production cell by cell. Agree the rule (usually half away from zero, stated in the SAP or TLF shells), then implement it in both programs.
- Using
floor(x + 0.5)in R. It is wrong for negative values and for halves that are stored slightly below the decimal value. - Comparing rounded text instead of numbers. When QC fails on "6.2" vs "6.3", compare the unrounded values first, for example with
PROC COMPARE ... METHOD=ABSOLUTE CRITERION=1E-10on the PCT variable. If they agree, it is a rounding rule difference, not a derivation bug. - Assuming all R table packages round the same way. Run 6.25 and -2.5 through the package you use for TLFs before you trust its output. Some offer an option for SAS-style rounding, so check the documentation for your installed version.
Try it yourself: N = 40 and no decimals
The active arm has N = 40. Percentages are shown with no decimals. For n = 1, 3 and 5, give the exact percentage, the SAS result, R's round() result and the round_half_up() result.
Answer:
| n | Exact % | SAS | R round() | Fixed R |
|---|---|---|---|---|
| 1 | 2.5 | 3 | 2 | 3 |
| 3 | 7.5 | 8 | 8 | 8 |
| 5 | 12.5 | 13 | 12 | 13 |
R's round() differs whenever the digit before the 5 is even (2.5 and 12.5). 7.5 matches because 8 is both "up" and "even".
Quick check
1. What does round(2.5) return in R?
R rounds an exact half to the even digit, so 2.5 goes to 2. SAS round(2.5) gives 3.
2. Which SAS function rounds an exact half to the even digit, like R?
ROUNDE rounds half to even. ROUND rounds half away from zero, and INT doesn't round at all: it drops the decimals.
3. A value of 6.249 is rounded to 2 decimals, then the result is rounded to 1 decimal in SAS. What is shown?
6.249 becomes 6.25, and 6.25 then becomes 6.3. Rounded once from 6.249, the correct answer is 6.2.
Frequently asked questions
Why does R round 0.5 to 0?
R follows the IEC 60559 rule for exact halves: round to the even digit. 0 is even, so 0.5 becomes 0, and 1.5 becomes 2. For a value that only looks like a half, such as 0.35, R rounds the stored binary value instead, so round(0.35, 1) gives 0.3.
How do I make R round like SAS?
Use a round half away from zero function, such as janitor::round_half_up() or the round_half_up() function in this post, for every value you display.
Which rounding rule should a clinical TLF use?
The one stated in the SAP or the TLF shells. Typically that is half away from zero (the SAS default). If nothing is stated, ask, and record the decision.
- SAS
ROUND()and SAS formats: exact halves go away from zero. Rround(): exact halves go to the even digit. - Arm sizes that are multiples of 16 (one decimal) or 8 (no decimals), such as 80 and 160, produce exact halves often, so the difference shows up in real AE tables.
- Put one
round_half_up()in the study's R utilities and use it for every displayed number. - Keep full precision in datasets and round once, at display.
- When QC differs by one in the last digit, compare the unrounded values before you debug the derivation.
Related: SUM function vs + in SAS: missing values · SAS vs R for clinical trials · LOCF in ADaM with SAS and R