Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: egen, weights

From   "Nick Cox" <>
To   <>
Subject   Re: st: egen, weights
Date   Thu, 12 Sep 2002 20:08:41 +0100

Daniel Millimet

> Is there a way to use -egen, pctile- with weights? 
> Note, the weights are not integers, so I cannot simply 
> expand the data set and then use the egen command. 
I can't see a way of doing this with -egen, pctile()-,  
which puts a particular percentile in a variable. 

The most obvious reason for wanting to do this 
is that you have groups of a categorical variable 
and you want each group to have its own percentile. 

Here is one way to do it: 

. u auto 

Yes, it's the auto data. 

. gen pctile = . 

Initialise a variable. 

. levels rep78 , local(levels) 

We don't need -levels- (SSC) for this example, 
but it is helpful for more complicated categorical 

. foreach l of local levels { 
.	_pctile mpg [w=gear_ratio] if rep78 == `l' 
. 	qui replace pctile = r(r1) if rep78 == `l' 
. } 

For each group of that categorical variable 
	calculate the percentile desired 
	plug it in where appropriate 

. tabdisp rep78, c(pctile) 

See what we just did. 

*   For searches and help try:

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