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
Loop with spaces and quotesHello, I have a list of terms that have spaces and quotes that I want to loop over. I have been abl…
Dummy variables for table values for percentileHey guys, I want to create one dummy variable which assigns a value of 1 for values above 90 percen…
Logit with CI chartIs there a Code: lfitci or Code: qfitci type command to chart a logistic regression with confi…
forvalues with skipped valuesI have about 300 items for which I want to run the forvalues loop. However, I need to skip about 6 v…
create a variable for the second lowest valueI have a list of values (bids). I already have generated the minimum value via: Code: egen minbid…
Subscribe to:
Post Comments (Atom)
0 Response to Identify value label names and storing in macro
Post a Comment