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

st: RE: Re: egen .&.. std()


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: Re: egen .&.. std()
Date   Mon, 20 Jan 2003 09:24:25 -0000

Steven Fraser

> > I am interested in obtaining the standard deviation for a 
> particular
> > variable with groups determined by another variable.  
> Essentially I would
> > like to combine the 'egen std()' function with the 'by' 
> construct which is
> > not allowed.  Is there an alternative to using collapse 
> (I would prefer not
> > to change the dataset) or having to introducing a matrix?

Scott Merryman

> Or, if you want the standard deviation as a variable you could:
> 
> levels class, local(levels)
> gen std = .
> foreach l of local levels {
>     qui sum mpg if class == `l'
>     qui replace std = r(sd) if class ==  `l'
>       }
> 
> You will need Nick Cox's -levels- (ssc install levels)

Scott's solution is fine (thanks for the plug), 
but it also seems from Steven's wording that 
-egen, sd()- will do what he wants (reference to -std()- 
is a red herring here). 

Nick 
[email protected] 

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