Learn R Programming

foreign (version 0.4-8)

write.dta: Write files in Stata binary format

Description

Writes the data frame to file in the Stata version 6.0 or 7.0 binary format. Does not write matrix variables.

Usage

write.dta(dataframe, file, version = 6,convert.dates=TRUE, tz="GMT",
 convert.factors=c("labels","string","numeric","codes"))

Arguments

dataframe
a data frame.
file
character string giving filename.
version
Stata version: 6 and 7 are supported.
convert.dates
Convert POSIXt objects to Stata dates?
tz
timezone for date conversion
convert.factors
how to handle factors

Value

  • NULL

Details

The columns in the data frame become variables in the Stata data set. Missing values are correctly handled. Nothing is done with factor levels, which should end up as variable labels. The major difference between versions is that 7.0 allows 32-character variable names. The abbreviate function is used to trim long variables to the permitted length. A warning is given if this is needed and it is an error for the abbreviated names not to be unique.

Optionally, R date/time objects (POSIXt classes) are converted into the Stata format. This loses information -- Stata dates are in days since 1960-1-1. POSIXct objects can be written without conversion but will not be understood as dates by Stata; POSIXlt objects cannot be written without conversion.

There are four options for handling factors. The default is to use Stata value labels for the factor levels. With convert.factors="string", the factor levels are written as strings. With convert.factors="numeric" the numeric values of the levels are written, or NA if they cannot be coerced to numeric. Finally, convert.factors="codes" writes the underlying integer codes of the factors. This last used to be the only available method and is provided largely for backwards compatibility.

References

Stata 6.0 Users Manual, Stata 7.0 Programming manual describe the file formats.

See Also

read.dta, attributes, DateTimeClasses, abbreviate

Examples

Run this code
data(swiss)
write.dta(swiss,swissfile<-tempfile())
read.dta(swissfile)

Run the code above in your browser using DataLab