30-Day Course
Clinical SAS → R
Day 19 of 30·Clinical Data Handling & CDISC Basics

Data Quality Checks & Validation in R

Programmatic checks, duplicate detection and reconciliation.

Learning objectives

  • 1Write data checks that return offending records.
  • 2Detect duplicate keys and out-of-range values.
  • 3Use assertions to gate a pipeline.
  • 4Produce a simple discrepancy report.

Key SAS® → R comparison

TaskSAS®R
Find dup keysproc sort nodupkey + logcount(key) |> filter(n>1)
Range checkif x<lo or x>hi then ...filter(x<lo | x>hi)
Assertmanual log scanassertr / stopifnot

Code examples

Duplicate key detection
SAS®
proc sort data=lb nodupkey dupout=dups;
  by usubjid lbtestcd visitnum;
run;
R
dups <- lb |>
  count(usubjid, lbtestcd, visitnum) |>
  filter(n > 1)

Note: Returns exactly which key combinations repeat — a ready-made discrepancy listing.

Explanation

R makes data-quality checks first-class: instead of scanning a log, you write expressions that return the offending rows as a dataset you can review, count, or export. Duplicate-key detection, range checks and cross-dataset reconciliation all become small, testable pipelines.

Assertion packages such as assertr let you embed checks directly in a pipeline so it halts when an expectation is violated, giving you the audit trail validation reviewers expect.

Practice exercise

Write checks that return duplicate LB keys, AVAL values outside a plausible range, and subjects present in ADAE but missing from ADSL; export each as a discrepancy listing.