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

# st: creating quintiles while using by

 From "Ben Hoen" To Subject st: creating quintiles while using by Date Mon, 30 Jul 2012 16:47:32 -0400

```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

http://eetd.lbl.gov/ea/ems/emp-pubs.html