Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# Re: st: creating quintiles while using by

 From Fernando Rios Avila <[email protected]> To [email protected] Subject Re: st: creating quintiles while using by Date Mon, 30 Jul 2012 17:15:12 -0400

```> Hi Ben,
> what about something like this:
> forvalues i=1(1)50 {
> capture drop q`i'
> xtile q`i'=var1 [fweight=fwt] if type==`i', n(5)
> }
> egen q_t=rowtotal(q1-q50)
> HTH
> Fernando
>
>
> On Mon, Jul 30, 2012 at 4:47 PM, Ben Hoen <[email protected]> wrote:
>>
>> Hi all,
>>
>> I have been racking my brain trying to find an elegant solution for a
>> problem I am having with quintiles.  If anyone has any advice it would be
>> great.
>>
>> I would like to divide data into quintiles (e.g., 0-20%, 20-40%, 40-60%,
>> ...) based on a continuous variable and for each of a categorical
>> variable,
>> so that I can create a table of min, max, and mean for each quintile for
>> each level of the categorical variable. I also have a fweight variable
>> that
>> I need to include.
>>
>> This does pretty close to what I want:
>>
>> sysuse auto, clear
>> keep mpg
>> g fwt=(int(10*runiform()))+1 //to create an fweight variable from 1 to 10
>> g type=(int(3*runiform()))+1 //to create an categorical variable from 1
>> to 3
>>
>> xtile quin=mpg [fweight=fwt] if type==1, n(5)
>> xtile quin2=mpg [fweight=fwt] if type==2, n(5)
>> xtile quin3=mpg [fweight=fwt] if type==3, n(5)
>> replace quin=quin2 if type==2
>> replace quin=quin3 if type==3
>>
>> bys type: tabstat mpg [fweight=fwt], stat(n min mean max) by(quin)
>>
>> By for my dataset I have 50 or so levels of "type", so ideally I would
>> like
>> to automate this.  I suspect it is a loop function but I was not able to
>> get
>> it to work.
>>
>> Any help would be appreciated.  Thanks.
>>
>> Ben
>>
>>
>> Ben Hoen
>> Principal Research Associate
>> Lawrence Berkeley National Laboratory
>> Office: 845-758-1896
>> Cell: 718-812-7589
>> [email protected]
>> http://eetd.lbl.gov/ea/emp/staff/hoen.html
>>
>> http://eetd.lbl.gov/ea/ems/emp-pubs.html
>>
>> http://eetd.lbl.gov/ea/emp/list/emp_pubs_signup.php
>>
>>
>>
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/statalist/faq
>> *   http://www.ats.ucla.edu/stat/stata/
>
>
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```