Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: creating quintiles while using by


From   Fernando Rios Avila <f.rios.a@gmail.com>
To   statalist@hsphsun2.harvard.edu
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 <bhoen@lbl.gov> 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
>> bhoen@lbl.gov
>> http://eetd.lbl.gov/ea/emp/staff/hoen.html
>>
>> Visit our publications at:
>> http://eetd.lbl.gov/ea/ems/emp-pubs.html
>>
>> Sign up for our email list to receive publication notifications:
>> 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/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index