Dear All,

I have a dataset imported into STATA. While importing, variable names get changed to lowercase but their value label names stay as is.

For example, I have a variable v012a but its value labels (lblname) are defined as V012A. I want to define v012a's own labels by copying labels values from V012A. There are thousands of such mismatches so it is extremely time consuming to do the following manually for each variable:

1. describe v012a
2. Identify vallab (V012A in this case)
3. label copy V012A v012a
4. label value v012a v012a

Is there any solution to achieve this for each variable in one go? I tried writing the following program but it is taking too much time to execute:

-----------------------------
program labelfix
version 12
syntax [varlist]


local vars `"`varlist'"'

// create list of variables for modification
local finalvars
foreach var of varlist `vars' {
cap la li `var'
if _rc {
local finalvars "`finalvars' `var'"
di in green "no value labels defined for `var'"
}
else {
di in green "value labels for `var' already defined. no change"
}
}

// label variables
cap la dir
local labels `"`r(names)'"'
foreach varlab of local labels {
preserve
uselabel, clear var
local newvarlist `"`r(`varlab')'"'
restore
if "`newvarlist'" != "" {
foreach var of varlist `newvarlist' {
if ("`varlab'" != "`var'" & strpos(`"`finalvars'"', "`var'") != 0) {
la copy `varlab' `var', replace
}
}
qui save "$S_FN", replace
}
}

cap la dir
local labels `"`r(names)'"'
foreach var of local labels {
la val `var' `var'
}
qui save "$S_FN", replace

end
--------------------------

Thanks for help.

Regards