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
Related Posts with Identify value label names and storing in macro
Expressing margins predictions of log outcomesHi all, I wonder if there's a way to express margins results for log transformed outcomes back in t…
Looping with MacrosHI I have a query about looping with macros. I have uploaded a do-file of my code to this post. Es…
Create an index for countriesI have a dataset of 105 countries and want to create an index variable but forgot the command for it…
Fixed-effect level: Is student AND evaluator fixed-effect still correct?Hey, I have the following stylized example. Code: * Example generated by -dataex-. To install: s…
Translating do file to pdfI am using the following command: translate 4th_Assignmnet.do 4th_Assignmnet.pdf and get: transla…
Subscribe to:
Post Comments (Atom)
0 Response to Identify value label names and storing in macro
Post a Comment