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

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


From   "Scott Merryman" <[email protected]>
To   <[email protected]>
Subject   st: Re: egen .&.. std()
Date   Sun, 19 Jan 2003 15:29:21 -0600

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

----- Original Message ----- 
From: "Fraser, Steven" <[email protected]>
To: "Statalist (E-mail)" <[email protected]>
Sent: Sunday, January 19, 2003 2:28 PM
Subject: st: egen .&.. std() 


> Statalisters -
> 
> 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?
> 
> Assume:
> 
> Manuf Model Class Mpg
> Chevy Tahoe SUV 14
> Toyota Highlander SUV 19
> Toyota Camry Auto 23
> Ford F-150 P/U 17
> etc.
> 
> I would like to capture the standard deviation of the mpg within a
> particular class.
> 
> i.e.
> 
> sort class
> by class: gen (or egen) mpgstd = std(mpg)
> 
> The error is:
> . by obj: egen mpgstd = std(mpg)
> egen ... std() may not be combined with by
> 
> This however, is not allowed.  Any thoughts or comments are greatly
> appreciated.
> 
> Steve Fraser
> 


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