|
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: egen, cut and labels
--- Dimitriy V. Masterov wrote:
> When using egen with the cut option, is it possible to get nice value
> labels for the resulting variable? Let's say I have some fake data:
>
> clear
> set obs 100
> gen x = uniform()
> egen bins = cut(x), at(0(.1)1)
>
> I would like to get value labels like "[0,.1)" and "[.1,.2)" ... and
> "[0.9,1]". The labels option does not really do this. For the fake
> data this is really trivial, but for a longer list this would be
> complicated.
It can be done. Below I show a way which makes heavy use of extended
macro function, see -help macro- for more on that.
*--------------------- begin example --------------------
clear
set obs 100
gen x = uniform()
/* with the -label- option the left boundery */
/* is stored in the label */
egen bins = cut(x), at(0(.1)1) label
/* getting the right most value */
sum bins, meanonly
local right = `r(max)'
/* getting the right limit for that interval */
/* you might need to tweak the round option */
/* to get pretty values in your case */
sum x, meanonly
local max = round(`r(max)', .01)
/* getting a list of all values */
levelsof bins
local levs "`r(levels)'"
foreach lev of local levs {
/* left boundery is stored in its label */
local lableft : label (bins) `lev'
if `lev' != `right' {
/* If not the right most value */
/* the right boundery is stored in the label */
/* of the next value */
local levright = `lev' + 1
local labright : label (bins) `levright'
local labright : subinstr local labright "-" ""
}
else {
/* The right boundery of the right most value */
/* was stored befor in the local `max' */
local labright "`max'"
}
label define bins2 `lev' "[`lableft'`labright')", add
}
label values bins bins2
tab bins
*---------------------- end example ------------------------
(For more on how to use examples I sent to the Statalist, see
http://home.fsw.vu.nl/m.buis/stata/exampleFAQ.html )
Hope this helps,
Maarten
-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands
visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434
+31 20 5986715
http://home.fsw.vu.nl/m.buis/
-----------------------------------------
___________________________________________________________
Want ideas for reducing your carbon footprint? Visit Yahoo! For Good http://uk.promotions.yahoo.com/forgood/environment.html
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/