0
votes

I'm currently trying to export data from Stata to excel (and make a pivot and a vlookup in excel) and then to reimport it into stata.

Everything works as expected except for Variables with a label are exported as follow "[1] very good" or without label "1" and imported as a string or in case 2 without a label.

Is there a way to reimport the variables in such a way that stata recognizes the label?

1

1 Answers

0
votes

Stata cannot import labels from an Excel file with the single -import excel- command.

However, you could import the labels as local macros from the Excel file if you did this right after your -import excel- command. Here is some example code I used for a project. If you give me more details on your specific instance (e.g. Excel file format), I can rewrite this code for your instance.

*Store variable names, types, labels, and value labels as locals
local opts clear allstring sheet("Variables") firstrow case(lower)
import excel "../docs/Variable names and values.xlsx", `opts'
forvalues i = 1/`=_N' {
    local varname = variablename[`i']       //variable name
    local `varname'lbl = variablelabel[`i'] //variable label
}

import delimited "../raw/NFP Baseline Survey.csv", case(lower) clear delim(",") varnames(1) stringcols(_all)

*Label variables
foreach v of varlist * {
    label variable `v' "``v'lbl'"
}