A little announcement. A lot to explore.

Happy learning!

Explore lessons, including SAS & R programs and practice data.

Search lessons

Type at least 3 characters to see matching lessons.

Open SAS ↗Open R ↗
← SASnRCompare datasets

Introduction

  • In clinical programming, we routinely compare two versions of the same dataset — for example the production DM and the independently programmed QC DM — to prove they match.
  • A good comparison should answer three questions:
    • Do both datasets have the same variables (names, types, lengths, labels)?
    • For subjects common to both datasets, do the variable values match?
    • Which subjects are in only one dataset and not the other?
  • SAS uses PROC COMPARE with BASE= and COMPARE=. The log and listing together answer all three questions, and the return code (&sysinfo) can be interrogated downstream in automation.
  • R has no single built-in equivalent, so we reach for a helper package. diffdf and arsenal::comparedf() are the two popular choices in clinical work — both print a structured report very similar in spirit to PROC COMPARE output.
  • The examples below build two small DM-like datasets, then run the same comparison in SAS, tidyverse R (using diffdf) and base R (using arsenal).
  • Before running, note the differences we deliberately seeded:
    • Subject 1005 has sex = F in dm but sex = M in qc_dm.
    • Subjects 1006 and 1007 are in dm but missing from qc_dm.
    • Subject 1008 is in qc_dm but not in dm.
  • After running, confirm the report flags exactly these three discrepancies. If the report shows more or fewer, revisit the code.

Compare datasets