1

I am trying to translate some SAS code into R. I am using SAS Viya on a remote server and the latest version of R on a Windows 10 laptop. Apparently the round function does not work the same in SAS and R. If I divide two vectors and use the round function on the result, SAS and R frequently differ by one. I have been able to get SAS to return the same results as R by using the SAS function rounde instead of round. But I have not been able to figure out how to get R to return the same results as SAS. I guess rounde in SAS 'rounds to even':

https://communities.sas.com/t5/SAS-Data-Management/An-oddity-Round-function-Can-somebody-who-has-used-both-R-and/td-p/532678

https://go.documentation.sas.com/doc/en/pgmsascdc/9.4_3.2/lefunctionsref/n02h2rvq30k869n164ccmcwvhqyk.htm

Is there a relatively simple way to get base R to return the same results as the round function in SAS? Or would doing so require a complex function? Or is the problem at a deeper level perhaps as described here:

https://stats.stackexchange.com/questions/160711/how-to-solve-a-problem-with-different-results-in-sas-and-r-due-to-a-number-repre

Perhaps the best thing to do is simply define the rounded results in R to be the same as those returned by the round function in SAS until I have the SAS code fully translated?

Here are the vectors, formatted for R, that I have been using:

hr <- c(0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4)
cy <- c(990, 1011, 1036, 1085, 1117, 1157, 1203, 997, 1019, 1045, 1096, 1130, 1171, 1220, 1004, 1027, 1055, 1106, 1143, 1187, 1237)

Here are the lines of code in SAS and R.

In R:

Rpop <- round(cy/hr)

In SAS this is different from R:

SASpop=round(cy/hr);

In SAS this is the same as R:

SASpope=rounde(cy/hr);

Here are the results, formatted in R, from the three lines of code above:

Rpop.results   <- c(2475, 2528, 2590, 2712, 2792, 2892, 3008, 2492, 2548, 2612, 2740, 2825, 2928, 3050, 2510, 2568, 2638, 2765, 2858, 2968, 3092)
SASpop.results <- c(2475, 2528, 2590, 2713, 2793, 2893, 3008, 2493, 2548, 2613, 2740, 2825, 2928, 3050, 2510, 2568, 2638, 2765, 2858, 2968, 3093)
SASpope.results <- c(2475, 2528, 2590, 2712, 2792, 2892, 3008, 2492, 2548, 2612, 2740, 2825, 2928, 3050, 2510, 2568, 2638, 2765, 2858, 2968, 3092)

SASpop.results - Rpop
#[1] 0 0 0 1 1 1 0 1 0 1 0 0 0 0 0 0 0 0 0 0 1

SASpope.results - Rpop
#[1] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Here is functional SAS code:

data a;
input cy hr ;
cards;
 990  0.4
 1011 0.4
 1036 0.4
 1085 0.4
 1117 0.4
 1157 0.4
 1203 0.4
 997  0.4
 1019 0.4
 1045 0.4
 1096 0.4
 1130 0.4
 1171 0.4
 1220 0.4
 1004 0.4
 1027 0.4
 1055 0.4
 1106 0.4
 1143 0.4
 1187 0.4
 1237 0.4
;

data b;
set a;

SASpop=round(cy/hr);
SASpope=rounde(cy/hr);
SASdiff = SASpop - SASpope ;

proc print;
run;

# Obs    cy        hr      SASpop   SASpope  SASdiff
# 1      990       0.4      2475      2475      0
# 2      1011      0.4      2528      2528      0
# 3      1036      0.4      2590      2590      0
# 4      1085      0.4      2713      2712      1
# 5      1117      0.4      2793      2792      1
# 6      1157      0.4      2893      2892      1
# 7      1203      0.4      3008      3008      0
# 8      997       0.4      2493      2492      1
# 9      1019      0.4      2548      2548      0
# 10     1045      0.4      2613      2612      1
# 11     1096      0.4      2740      2740      0
# 12     1130      0.4      2825      2825      0
# 13     1171      0.4      2928      2928      0
# 14     1220      0.4      3050      3050      0
# 15     1004      0.4      2510      2510      0
# 16     1027      0.4      2568      2568      0
# 17     1055      0.4      2638      2638      0
# 18     1106      0.4      2765      2765      0
# 19     1143      0.4      2858      2858      0
# 20     1187      0.4      2968      2968      0
# 21     1237      0.4      3093      3092      1
Mark Miller
  • 12,483
  • 23
  • 78
  • 132
  • 2
    `floor(cy/hr+0.5)` in R should match. – Jon Spring Aug 19 '23 at 18:05
  • 1
    On the sample data, `floor(cy/hr+0.5) - SASpop.results` gives me zeroes. Does it not for you, or is your real data different in some aspect from the sample? – Jon Spring Aug 19 '23 at 18:22
  • @JonSpring My apologies. It does return zeroes. I got confused and compared the wrong two vectors. If you post your comment as an answer I will upvote it and probably accept it tomorrow. – Mark Miller Aug 19 '23 at 18:26

0 Answers0