Hi all,
I have a dataset with over 100 variables, none of which have labels. I also have an Excel spreadsheet ("Excel") with the 100+ variable names in one column ("Variable") & the appropriate variable labels (which are multi-word phrases) in another column ("Label"). This is how these data were given to me. I need to label all the variables in my Stata dataset. How can I do this using the Excel spreadsheet?
First I tried:
The tokenize section worked great, but the labels were assigned in the wrong order. The local created by the levelsof command lists the labels alphabetically, so the wrong labels ended up being applied to the variables.
I then tried to create a local with the labels in the correct order:
This worked except for the first label, which didn't have quotation marks around it & so broke my code when I tried to apply the variable labels since Stata didn't recognize it as the full phrase.
I am stumped. I've read a lot of the forum topics that seem like the same problem (like the one I linked) but can't get them to work for me. I don't know how to use Mata so would like to avoid that if possible. Could someone please help me figure this out? Thanks!
I have a dataset with over 100 variables, none of which have labels. I also have an Excel spreadsheet ("Excel") with the 100+ variable names in one column ("Variable") & the appropriate variable labels (which are multi-word phrases) in another column ("Label"). This is how these data were given to me. I need to label all the variables in my Stata dataset. How can I do this using the Excel spreadsheet?
First I tried:
Code:
import excel "Excel", sheet("var_labels") firstrow clear
levelsof Label, loc(labels)
use $dataset, clear
ds _all
tokenize `r(varlist)'
foreach x of local labels {
lab var `1' "`x'"
macro shift
}
I then tried to create a local with the labels in the correct order:
Code:
import excel "Excel", sheet("var_labels") firstrow clear
local labels
local end = r(N)
forval i = 1/`end' {
levelsof Label if _n == `i', loc(lab`i')
local labels `labels' `lab`i''
}
I am stumped. I've read a lot of the forum topics that seem like the same problem (like the one I linked) but can't get them to work for me. I don't know how to use Mata so would like to avoid that if possible. Could someone please help me figure this out? Thanks!

Comment