7

I am getting an error while converting R file into Stata format. I am able to convert the numbers into Stata file but when I include strings I get the following error:

library(foreign)
write.dta(newdata, "X.dta")

Error in write.dta(newdata, "X.dta") : 
  empty string is not valid in Stata's documented format

I have few strings like location, name etc. which have missing values which is probably causing this problem. Is there a way to handle this? .

Nick Cox
  • 35,529
  • 6
  • 31
  • 47
user3570187
  • 1,743
  • 3
  • 17
  • 34

2 Answers2

16

I've had this error many times before, and it's easy to reproduce:

library(foreign)
test <- data.frame(a = "", b = 1, stringsAsFactors = FALSE)
write.dta(test, 'example.dta')

One solution is to use factor variables instead of character variables, e.g.,

for (colname in names(test)) {
  if (is.character(test[[colname]])) {
    test[[colname]] <- as.factor(test[[colname]])
  }
}

Another is to change the empty strings to something else and change them back in Stata.

This is purely a problem with write.dta, because Stata is perfectly fine with empty strings. But since foreign is frozen, there's not much you can do about that.

Update: (2015-12-04) A better solution is to use write_dta in the haven package:

library(haven)
test <- data.frame(a = "", b = 1, stringsAsFactors = FALSE)
write_dta(test, 'example.dta')

This way, Stata reads string variables properly as strings.

Frank
  • 2,386
  • 17
  • 26
1

You could use the great readstata13 package (which kindly imports only the Rcpp package).

readstata13::save.dta13(mtcars, 'mtcars.dta')

The function allows to save already in Stata 15/16 MP file format (experimental), which is the next update after Stata 13 format.

readstata13::save.dta13(mtcars, 'mtcars15.dta', version="15mp")

Note: Of course, this also works with OP's data:

readstata13::save.dta13(data.frame(a="", b=1), 'my_data.dta')
jay.sf
  • 60,139
  • 8
  • 53
  • 110