1

Good day,

I'm trying to export a labeled dataset to SPSS and Stata. When I open the data in SPSS, the labels export correctly, however, the underlying values are recoded. For example, 0 is recoded to 1. I've tried exporting the data with haven::write_dta() and foreign::write.dta()

The result that I expect is

0 = "Never [0]"

1 = "Monthly or less [1]",

2 = "2 to 4 times a month [2]",

3 = "2 to 3 times a week [3]",

4 = "4 or more times a week [4]"

However, these are the values and labels I'm getting in SPSS for SPSS and Stata datasets.

1 = "Never [0]"

2 = "Monthly or less [1]",

3 = "2 to 4 times a month [2]",

4 = "2 to 3 times a week [3]",

5 = "4 or more times a week [4]"

Here is the R code I've used:

library(Hmisc)
audit$a_audit_1_1 =  factor(audit$a_audit_1_1, levels = c(0, 1, 2, 3, 4))
levels(audit$a_audit_1_1) = c(
"Never [0]",
"Monthly or less [1]",
"2 to 4 times a month `[2]",
"2 to 3 times a week [3]",
"4 or more times a week [4]")
label(audit$a_audit_1_1)="How often do you..."

library(haven)
write_dta(audit, "audit.dta")

According to this introduction to variable labels and expss:

The usual way to connect numeric data to labels in R is in factor variables. However, factors miss important features which the value labels provide. Factors only allow for integers to be mapped to a text label, these integers have to be a count starting at 1 and every value need to be labelled. Also, we can’t calculate means or other numeric statistics on factors.

Is there a way to keep the original codes?

Thank you very much for your help.

André

Please note, I've also posted this question on the following forums:

  1. GitHub

  2. RStudio Community

Community
  • 1
  • 1
  • Try `foreign::write.dta()` or `readstata13::save.dta13()` alternatively. – jay.sf Feb 13 '19 at 18:49
  • Thank you very much, @jay.sf! `foreign::write.dta()` gave me the same result as `haven::write_dta()`. `readstata13::save.dta13(audit, "readstata13_audit.dta")` resulted in "Error in iconv(x, to = encoding, sub = "byte") : 'x' must be a character vector or a list of NULL or raw vectors". – André van Zyl Feb 13 '19 at 19:11
  • @AndrévanZyl You lost your original values when you call `factor`. Try to avoid conversion to factor and use value labels instead. – Gregory Demin Feb 14 '19 at 14:57
  • Thank you very much @GregoryDemin, which package do you use to add value labels? – André van Zyl Feb 14 '19 at 19:01
  • @AndrévanZyl I am author of the expss package so I use `apply_labels` from expss. But I didn't test it with Stata files. – Gregory Demin Feb 14 '19 at 21:09
  • 1
    @GregoryDemin thank you. Using `apply_labels` and then exporting the data to SPSS with [write_labels](https://www.rdocumentation.org/packages/expss/versions/0.8.10/topics/write_labels) seems to work. It is easy enough to export the labelled data from SPSS to Stata. – André van Zyl Feb 28 '19 at 02:41

0 Answers0