Some datasets come with full-lowercase value labels, and I end up with graphs and tables showing results for "egypt", "jordan" and "saudi arabia" instead of the capitalized country names.
I guess that the proper()
string function can do something for me, but I am not finding the right way to write the code for Stata 11 that will capitalize all value labels for a given variable.
I basically need to run the proper()
function on all value labels on the variable, and then assign them to the variable. Is that possible using a foreach
loop and macros in Stata?
Yes. First let's create some sample data with labels for testing:
clear
drawnorm x, n(10)
gen byte v = int(4+x)
drop x
label define types 0 "zero" 1 "one" 2 "two" 3 "three" 4 "four" 5 "five" 6 "six"
label list types
label values v types
Here's a macro to capitalize the values associated with the variable "v":
local varname v
local sLabelName: value label `varname'
di "`sLabelName'"
levelsof `varname', local(xValues)
foreach x of local xValues {
local sLabel: label (`varname') `x', strict
local sLabelNew =proper("`sLabel'")
noi di "`x': `sLabel' ==> `sLabelNew'"
label define `sLabelName' `x' "`sLabelNew'", modify
}
After running it, check the results:
label list types
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With