Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: AW: st: RE: Decile sorts


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: AW: st: RE: Decile sorts
Date   Fri, 10 Nov 2006 12:29:13 -0000

The -levelsof- solution is one I would endorse. 

For people interested in tricks in this territory, 

FAQ     . . . . . . . . . . Making foreach go through all values of a variable
        8/05    Is there a way to tell Stata to try all values of a
                particular variable in a foreach statement without
                specifying them?
                http://www.stata.com/support/faqs/data/foreach.html

talks about the -levelsof-/-foreach- manoeuvre and alternatives. 

Nick 
[email protected] 

Philipp Rehm
 
> A potential work-around on a command that's not by-able is to use 
> -levelsof- in combination with a loop.
> 
> Example: Assume you want price deciles by car type (foreign). The 
> following may get you there:
> 
> sysuse auto, clear
> 
> gen deciles_price=.
> levelsof foreign, local(l)
> foreach C in `l' {
>    xtile deciles_price_`C'=price if foreign==`C', n(10)
>    replace deciles_price=deciles_price_`C' if foreign==`C'
>    drop deciles_price_`C'
> }
> 
> You can make this loop arbitrarily complicated to incorporate more 
> variables (although it's not particularly pretty to read): 
> Just add more 
> -levelsof- and -foreach--loops.
> 
> There may well be much better ways to do this, though.
> 
 Thomas Erdmann wrote:
> > A further note on Jeph's suggestion:
> > 
> > It looks very convenient, but I need to adjust for the fact 
> that I do not
> > need the mean of the same item but of a different attribute:
> > 
> > foreach X of varlist c1* {
> > 	xtile deciles_`X'=`X', n(10)
> > 	bysort deciles_`X': egen Rr`X'=mean(c1ds_ri)
> > 	}
> > 
> > But a problem still remains: 
> > the deciles are calculated over all observations - but what 
> I need is
> > calculating the mean of deciles by yrm (my time variable 
> representing
> > YearMonth) and afterwards the mean of all deciles groups 
> (1-10) over all
> > yrm's. I was not able to integrate this into this short 
> solution as -by- is
> > not allowed for -xtile- . 

*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index