Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalizing value labels in Stata

Tags:

stata

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?

like image 452
Fr. Avatar asked Mar 13 '11 14:03

Fr.


1 Answers

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
like image 192
whuber Avatar answered Sep 30 '22 04:09

whuber