Day 24 of 30·SDTM Implementation
haven & xportr: Producing Compliant Transport Files
Writing submission-ready .xpt with correct types, lengths and labels.
Learning objectives
- 1Apply variable labels, lengths and types with xportr.
- 2Order variables and set dataset label.
- 3Write a v5 transport file with write_xpt.
- 4Validate against a metadata specification.
Key SAS® → R comparison
| Task | SAS® | R |
|---|---|---|
| Var length | length statement | xportr_length() |
| Var label | label statement | xportr_label() |
| Var order | retain order | xportr_order() |
| Write .xpt | xport engine | xportr_write() / write_xpt() |
Code examples
SAS®
libname out xport 'dm.xpt';
proc copy in=work out=out; select dm; run;R
library(xportr)
dm |>
xportr_type(spec) |>
xportr_length(spec) |>
xportr_label(spec) |>
xportr_order(spec) |>
xportr_write("dm.xpt", label = "Demographics")Note: Driven by a metadata spec, xportr enforces the CDISC conformance details a bare write_xpt() does not.
Explanation
Producing a submission file is more than dumping data to .xpt — variable types, lengths, labels, ordering and the dataset label must conform to the define. xportr applies each of these from a metadata specification in a readable pipeline, then writes a version-5 transport file.
Driving the export from a spec means the same metadata that generates define.xml also governs the data, keeping data and documentation in lockstep — a key validation expectation.
Practice exercise
Given a small spec table, apply xportr_type/length/label/order to a DM dataset and write a compliant dm.xpt, then read it back to confirm labels and lengths.