Writes a data frame to a Stata .dta file, preserving variable labels,
value labels, and missing value types. Tagged NAs from any source format
(SPSS, Stata, SAS) are written as Stata extended missing values
(.a through .z).
Arguments
- data
A data frame to export. Columns of class
haven_labelledwill have their labels written to the.dtafile.- path
Path to the output file. Must end in
.dta.- version
Stata file version to use. Supported values: 8-15. Default is 14 (Stata 14/15, supports Unicode). Use 13 for compatibility with older Stata versions.
Details
Tagged NA Handling
Stata natively supports 27 extended missing values: .a through .z and
. (system missing). When exporting data with tagged NAs:
From Stata (
read_stata()): Native extended missing values are preserved in a full roundtrip.From SPSS (
read_spss()): Tagged NAs are written as Stata extended missing values (.a,.b, etc.). The original SPSS numeric codes (e.g., -9, -8) are not preserved in the Stata file, but the distinct missing value types and their labels are.From SAS (
read_sas(),read_xpt()): SAS special missing values are mapped to their Stata equivalents.
See also
read_stata() for importing Stata files,
write_spss() for SPSS export,
write_xlsx() for Excel export,
write_xpt() for SAS transport export
Other data-export:
write_spss(),
write_xlsx(),
write_xpt()
Examples
if (FALSE) { # \dontrun{
# Roundtrip: read Stata, process, write back
data <- read_stata("survey.dta")
data_clean <- data[data$age >= 18, ]
write_stata(data_clean, "survey_adults.dta")
# Cross-format: SPSS to Stata
data <- read_spss("survey.sav")
write_stata(data, "survey.dta")
# Stata 13 compatibility
write_stata(data, "survey_v13.dta", version = 13)
} # }
