From 32c997737a76c6ed15c91efb81919facde79cd43 Mon Sep 17 00:00:00 2001 From: muridaehsr Date: Mon, 21 Oct 2024 10:23:38 -0700 Subject: [PATCH 1/2] Updated table and associated script to long-form so it's closer to the comparisons template from other pages. --- SAS/rounding.qmd | 56 +++++++++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/SAS/rounding.qmd b/SAS/rounding.qmd index acfc7b1c..c5a9e436 100644 --- a/SAS/rounding.qmd +++ b/SAS/rounding.qmd @@ -15,32 +15,47 @@ For example (See references for source of the example) ```{r, eval=FALSE} #Example code data XXX; - my_number=2.2; output; - my_number=3.99; output; - my_number=1.2345; output; - my_number=7.876; output; - my_number=13.8739; output; + my_number=2.2; decimal_paces=1; output; + my_number=2.2; decimal_paces=2; output; + my_number=2.2;; decimal_paces=3 output; + my_number=3.99; decimal_paces=1; output; + my_number=3.99; decimal_paces=2; output; + my_number=3.99; decimal_paces=3; output; + my_number=1.2345; decimal_paces=1; output; + my_number=1.2345; decimal_paces=2; output; + my_number=1.2345; decimal_paces=3; output; + my_number=7.876; decimal_paces=1; output; + my_number=7.876; decimal_paces=2; output; + my_number=7.876; decimal_paces=3; output; + my_number=13.8739; decimal_paces=1; output; + my_number=13.8739; decimal_paces=2; output; + my_number=13.8739; decimal_paces=3; output; run; data xxx2; set xxx; - r_1_dec = round(my_number, 0.1); - r_2_dec = round(my_number, 0.01); - r_3_dec = round(my_number, 0.001); - - re_1_dec = rounde(my_number, 0.1); - re_2_dec = rounde(my_number, 0.01); - re_3_dec = rounde(my_number, 0.001); + round = round(my_number, decimal_places); + rounde = rounde(my_number, decimal_places); run; ``` -| my_number | r_1_dec | r_2_de | r_3_dec | re_1_dec | **re_2_dec** | **re_3_dec** | -|------------|---------|--------|-----------|----------|--------------|--------------| -| 2.2 | 2.2 | 2.2 | 2.2 | 2.2 | 2.2 | 2.2 | -| 3.99 | 4 | 3.99 | 3.99 | 4 | 3.99 | 3.99 | -| **1.2345** | 1.2 | 1.23 | **1.235** | 1.2 | 1.23 | **1.234** | -| 7.876 | 7.9 | 7.88 | 7.876 | 7.9 | 7.88 | 7.876 | -| 13.8739 | 13.9 | 13.87 | 13.874 | 13.9 | 13.87 | 13.874 | +| my_number | decimal_places | round | rounde | +|------------|----------------|-----------|-----------| +| 2.2 | 0.1 | 2.2 | 2.2 | +| 2.2 | 0.01 | 2.2 | 2.2 | +| 2.2 | 0.001 | 2.2 | 2.2 | +| 3.99 | 0.1 | 4 | 4 | +| 3.99 | 0.01 | 3.99 | 3.99 | +| 3.99 | 0.001 | 3.99 | 3.99 | +| **1.2345** | 0.1 | 1.2 | 1.2 | +| **1.2345** | 0.01 | 1.23 | 1.23 | +| **1.2345** | 0.001 | **1.235** | **1.234** | +| 7.876 | 0.1 | 7.9 | 7.9 | +| 7.876 | 0.01 | 7.88 | 7.89 | +| 7.876 | 0.001 | 7.876 | 7.876 | +| 13.8739 | 0.1 | 13.9 | 13.9 | +| 13.8739 | 0.01 | 13.87 | 13.87 | +| 13.8739 | 0.001 | 13.874 | 13.874 | In some rare cases, `round()` does not return result as expected. For example below. @@ -54,6 +69,7 @@ run; ``` You can find a little more by the code below. It creates dummy numbers with different numbers of decimal digits, and filter incorrect results. Note, the incorrect results are expected when the input number is near or beyond the precision level, i.e. the last decimal of the input number is near or less than the number multiplied by `constant('maceps')`. + ```{sas, eval=FALSE} data dum1; int1=0; output; @@ -80,6 +96,7 @@ quit; ``` Or more by the code below and comparing with results from another language, e.g. R. + ```{sas, eval=FALSE} data dum1; dec1=0; int1=0; output; @@ -113,7 +130,6 @@ data dat3; run; ``` - As the incorrect rounding all occur on large number, `round()` is still reliable in most of cases. **References** From a3e5a42092b93540e0662ea47c8c9c2800a552fa Mon Sep 17 00:00:00 2001 From: muridaehsr Date: Mon, 21 Oct 2024 10:27:56 -0700 Subject: [PATCH 2/2] Updated reference for the R version from the 'round_half_up()' function in the janitor package, to the 'round5()' function in the cards package. --- Comp/r-sas_rounding.qmd | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Comp/r-sas_rounding.qmd b/Comp/r-sas_rounding.qmd index 7dbd2633..c6b16410 100644 --- a/Comp/r-sas_rounding.qmd +++ b/Comp/r-sas_rounding.qmd @@ -4,10 +4,9 @@ title: "R v SAS rounding" **Rounding; R and SAS** -On comparing the documentation of rounding rules for both languages, it will be noted that the default rounding rule (implemented in the respective language's round() function) are different. Numerical differences arise in the knife-edge case where the number being rounded is equidistant between the two possible results. The round() function in SAS will round the number 'away from zero', meaning that 12.5 rounds to the integer 13. The round() function in Base R will round the number 'to even', meaning that 12.5 rounds to the integer 12. SAS does provide the rounde() function which rounds to even and the janitor package in R contains a function round_half_up() that rounds away from zero. In this use case, SAS produces a correct result from its round() function, based on its documentation, as does R. Both are right based on what they say they do, but they produce different results (Rimler, M.S. et al.). - -As described in [R - Rounding](../R/rounding.html) and [SAS - Rounding](../SAS/rounding.html), `round()` in SAS and `round_half_up()` in R are incorrect in rare different cases. It may need to be considered when comparing results between SAS and R. +On comparing the documentation of rounding rules for both languages, it will be noted that the default rounding rule (implemented in the respective language's round() function) are different. Numerical differences arise in the knife-edge case where the number being rounded is equidistant between the two possible results. The round() function in SAS will round the number 'away from zero', meaning that 12.5 rounds to the integer 13. The round() function in Base R will round the number 'to even', meaning that 12.5 rounds to the integer 12. SAS does provide the rounde() function which rounds to even and the cards package in R contains a function round5() that rounds away from zero. In this use case, SAS produces a correct result from its round() function, based on its documentation, as does R. Both are right based on what they say they do, but they produce different results (Rimler, M.S. et al.). +As described in [R - Rounding](../R/rounding.html) and [SAS - Rounding](../SAS/rounding.html), `round()` in SAS and `cards::round5()` in R are incorrect in rare different cases. It may need to be considered when comparing results between SAS and R. **References**