user3570187
user3570187

Reputation: 1773

Converting R file to Stata with missing string values

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

Upvotes: 7

Views: 15601

Answers (2)

jay.sf
jay.sf

Reputation: 72758

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

Upvotes: 1

Frank
Frank

Reputation: 2416

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.

Upvotes: 16

Related Questions