Jared:
You can convert the variable labels to variable names from within Stata before exporting it to a R or text file.
As Ian mentions, variable labels usually do not make good variable names, but if you convert spaces and other characters to underscores and if your variable labels aren't too long, you can re-label your vars with the varlabels quite easily.
Below is an example using the inbuilt Stata dataset "cancer.dta" to replace all variable names with var labels--importantly, this code will not try to rename variable with no variable labels. Note that I also picked a dataset where there are lots of characters that aren't useful in naming a variable (e.g.: =, 1, ', ., (), etc)...you can add any characters that might be lurking in your variable labels to the list in the 5th line: "local chars "..." " and it will make the changes for you:
****************! BEGIN EXAMPLE
//copy and paste this code into a Stata do-file and click "do"//
sysuse cancer, clear
desc
**
local chars "" " "(" ")" "." "1" "=" `"'"' "___" "__" "
ds, not(varlab "") // <-- This will only select those vars with varlabs //
foreach v in `r(varlist)' {
local `v'l "`:var lab `v''"
**variables names cannot have spaces or other symbols, so::
foreach s in `chars' {
local `v'l: subinstr local `v'l "`s'" "_", all
}
rename `v' ``v'l'
**make the variable names all lower case**
cap rename ``v'l' `=lower("``v'l'")'
}
desc
****************! END EXAMPLE
You might also consider taking a look at Stat Transfer and it's capabilities in converting Stata to R datafiles.
~ Eric
[email protected] |
[email protected] |
http://www.eric-a-booth.com