2

I added variable (and value, for some) labels in R, using the apply_labels function from 'expss'. When I want to save the data using 'write.dta' and open it in Stata (or reopening the newly saved data in R), the labels do not appear.

I am suspecting that it has something to do with this line in the write.dta documentation:

If the "var.labels" attribute contains a character vector with a string label for each variable then this is written as the variable labels. Otherwise the variable names are repeated as variable labels.

Because this is exactly what is happening (the variable names are repeated as variable labels). When checking with attr(df$variable, "label") before trying writing the data using write.dta, the labels appear.

I get the warning message:

"In write.dta [...] abbreviating variable names".

Not sure if this has to do with the problem.

A reproducible example of the code used to add the varibale, labels, and write the data:

library(expss)
library(dplyr)
library(foreign)
df <- data.frame(country = rep(c("NL", "DE", "FR", "AT"), 2),
 year = rep(c(2012,2014), 4),
 LS_medianpovgap60_disp_wa = c(0.448257605781815, 0.468249874784546, 0.473270740126805, 0.483814288478694, 0.486781335455043, 0.49246341926957, 0.51121872756711, 0.556027028656306))
df <- apply_labels(df,
 country = "Country",
 year = "Year",
 LS_medianpovgap60_disp_wa = "Median shortfall from the poverty thresholds using 60% of the median income, disposable income only households with working age (LIS and SILC average)")
write.dta(df, "df_labelled.dta")
xilliam
2,2972 gold badges18 silver badges29 bronze badges
asked Nov 15, 2021 at 8:30
1
  • Could you provide us with a reproducible example of the code used to add the variable, labels, and write the data? Commented Nov 15, 2021 at 9:11

1 Answer 1

2

For Stata version > 7, write.dta attempts to abbreviate variable label's if the label attributes is longer than 31 characters.

You may get a better result by using the haven package for the writing and reading steps of your code.

haven::write_dta(df, "df_labelled.dta")
temp <- haven::read_dta("df_labelled.dta")
temp

Edit The comments below point out that Stata imposes a limit on a variable label's length (80 characters). So R-based work-arounds will all be subject to this constraint.

answered Nov 15, 2021 at 10:07
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Using haven for writing the data works (better). Writing the data with foreign abbreviates the variable name automatically, but does not save the labels. Writing the data with haven gives an error for too long variable names, but after correcting these manually it does save the data, while cutting the label names at 80 characters.
FYI 80 characters is a limit imposed by Stata. See help label: "label variable attaches a label (up to 80 characters) to a variable. If no label is specified, any existing variable label is removed."

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.