Skip to contents

Sorts x by the key variables defined in ds_spec$keys for dataset, equivalent to SAS PROC SORT BY on those variables. Sets the herald.sort_keys attribute on the result so that write_xpt() and write_json() can verify sort order without re-sorting. No-op (with no error) when no keys are defined.

Usage

sort_keys(x, spec, dataset)

Arguments

x

A data frame.

spec

A herald_spec object or a path to a spec file.

dataset

Character. The dataset name.

Value

The data frame sorted by key variables (invisibly), with herald.sort_keys attribute set.

Examples

spec <- herald_spec(
  ds_spec = data.frame(
    dataset = "DM",
    label   = "Demographics",
    keys    = "STUDYID, USUBJID",
    stringsAsFactors = FALSE
  ),
  var_spec = data.frame(
    dataset  = "DM",
    variable = c("STUDYID", "USUBJID"),
    stringsAsFactors = FALSE
  )
)
dm <- data.frame(
  STUDYID = c("S1", "S1"),
  USUBJID = c("001-002", "001-001"),
  stringsAsFactors = FALSE
)
result <- sort_keys(dm, spec, "DM")
result$USUBJID  # "001-001" "001-002"
#> [1] "001-001" "001-002"
attr(result, "herald.sort_keys")  # "STUDYID" "USUBJID"
#> [1] "STUDYID" "USUBJID"