3

I imported a .dta (Stata file format) into R, but it looks like the variable labels did not get imported along with the variable names.

  • Using foreign::read.dta, I tried labels(df), but that only gives me the variable names; and str(df$var) is also is not telling me label.
  • Using a function from the haven package, attributes(df$var) gives me levels and class, but not variable label.

Am I missing something here?

smci
  • 32,567
  • 20
  • 113
  • 146
laura bmw
  • 41
  • 5
  • 2
    Per `haven` [docs](https://haven.tidyverse.org/reference/read_dta.html) (assuming this is your package), *Variable labels are stored in the "label" attribute of each variable*. Check: `attributes(df$col1)`. – Parfait Jun 28 '19 at 17:56
  • 1
    If Parfait's pointer doesn't resolve it, you may need to add more information (like the tool you used for import, maybe the Stata version). You could also try `str(DF)` to look around for the labels more. – Frank Jun 28 '19 at 17:58
  • Does that help https://stackoverflow.com/a/25850262/5784831? – Christoph Jun 28 '19 at 18:03
  • @parfait - I just tried the `haven` package and `attributes(df$var)`, which gives me levels and class, but not variable label. – laura bmw Jun 28 '19 at 18:15
  • @Frank - I also tried `str(df$var)` and that also is not telling me label. I'm guess the labels must not have transferred over, so I should probably try a difference import function? I used `read.dta()` to import. – laura bmw Jun 28 '19 at 18:16
  • 1
    First, it's useful to state that .dat is a Stata file format; 99% of users won't know - I added that in body and tags. Second, I believe you're talking about making sure **categorical labels** get imported right, right? Can you narrow the issue down to whether they're getting correctly **exported from Stata to the .dta file** (can you show us a snippet of .dta file?), or **imported from .dta file to R**? – smci Jun 28 '19 at 18:29
  • Yeah, it might be that a different function is required, or you could try changing the arguments to the R function (from Parfait's link, maybe you think the file is from one version, but it's really from another?). Both foreign and haven are kept up to date, so I'd expect them to somehow work. As smci noted, it might also be a problem with the export from Stata (testable by reopening it there, I guess). – Frank Jun 28 '19 at 18:31

1 Answers1

1

To see variable labels in R, it depends on how the Stata file is imported. Just using the foreign package (command read.dta) does not import variable labels.

Use the haven package to import the Stata file (read_dta command). Using the attributes command via haven package (@parfait) will give you format, class, and levels, in addition to variable label. However, if you only want to see the variable labels, then use the var_lab command from the expss package.

    library(haven)
    df <- read_dta(file="df.dta")
    library(expss)
    lapply(df, var_lab)
    # OR
    var_lab(df$var)
laura bmw
  • 41
  • 5