Learn R Programming

mariposa (version 0.7.3)

write_stata: Export Data to Stata Format

Description

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).

Usage

write_stata(data, path, version = 14)

Value

Invisibly returns the file path.

Arguments

data

A data frame to export. Columns of class haven_labelled will have their labels written to the .dta file.

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.

Limitations

Stata supports at most 27 distinct missing types per variable. SPSS data with more than 27 tagged NA types in a single variable may lose some distinctions (though this is extremely rare in practice).

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

Run this code
# \donttest{
if (requireNamespace("haven", quietly = TRUE)) {
  # Roundtrip: write to a temporary .dta, read back
  tmp <- tempfile(fileext = ".dta")
  write_stata(survey_data, tmp)
  data <- read_stata(tmp)

  # Stata 13 compatibility
  tmp13 <- tempfile(fileext = ".dta")
  write_stata(survey_data, tmp13, version = 13)

  unlink(c(tmp, tmp13))
}
# }

Run the code above in your browser using DataLab