In R,the {stats} or the {coin} package can be used to calculate McNemar. The {coin} package has the same defaults as SAS. But, using either of these packages, the first step is to calculate a frequency table, using the table function.
The FREQ procedure can be used in SAS with the AGREE option to run the McNemar test, with OR, and RISKDIFF options stated for production of odds ratios and risk difference. These options were added as epibasix::mcNemar outputs the odds ratio and risk difference with confidence limits as default. In contrast to R, SAS outputs the Kappa coefficients with confident limits as default.
proc freq data=colds;
tables age12*age14 / agree or riskdiff;
run;
Summary and Recommendation
When calculating the odds ratio and risk difference confidence limits, SAS is not treating the data as matched-pairs. There is advice on the SAS blog and SAS support page to amend this, which requires a lot of additional coding.
{stats} is using Edward’s continuity correction by default, but this can be removed. In contrast, there is no option to include Edward’s continuity correction in SAS, but this can be manually coded to agree with R. However, its use is controversial due to being seen as overly conservative.
There is another R package that is sometimes used to calculate McNemar’s, called epibasix. This package is no longer being maintained, and there was no documentation available for certain methods used. Therefore, the use of the epibasix package is advised against and other packages may be more suitable.
Session Info
─ Session info ───────────────────────────────────────────────────────────────
setting value
version R version 4.4.3 (2025-02-28)
os Ubuntu 24.04.2 LTS
system x86_64, linux-gnu
ui X11
language (EN)
collate C.UTF-8
ctype C.UTF-8
tz Europe/London
date 2025-03-13
pandoc NA (via rmarkdown)
─ Packages ───────────────────────────────────────────────────────────────────
! package * version date (UTC) lib source
P coin * 1.4-3 2023-09-27 [?] RSPM (R 4.4.0)
[1] /home/michael/source/CAMIS/renv/library/linux-ubuntu-noble/R-4.4/x86_64-pc-linux-gnu
[2] /opt/R/4.4.3/lib/R/library
P ── Loaded and on-disk path mismatch.
─ External software ──────────────────────────────────────────────────────────
setting value
SAS 9.04.01M7P080520
──────────────────────────────────────────────────────────────────────────────