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

# Re: st: RE: Calculating SD of a variable

 From lreine ycenna To statalist@hsphsun2.harvard.edu Subject Re: st: RE: Calculating SD of a variable Date Wed, 19 Oct 2011 13:58:30 +0100

```Hi, I forgot to respond to this, and now this problem's come back to
me. I udnerstand now that using -egen SD= sd(gdp), by(country year) -
generates missing observations, but how can I then calculate SD
according to both country and year? (i.e. no repeated sd for each year
or each country).

i.e. when I use egen SD= sd(gdp), by(country),

then I have the same SD for each country.

111	1979	USA	2.082018
111	1980	USA	2.082018
111	1981	USA	2.082018
111	1982	USA	2.082018
111	1983	USA	2.082018
111	1984	USA	2.082018
111	1985	USA	2.082018
111	1986	USA	2.082018
111	1987	USA	2.082018
111	1988	USA	2.082018
111	1989	USA	2.082018

when I use egen SD= sd(gdp), by(year),

then I have the same SD for each year.

111	1979	USA	6.972519
111	1980	USA	7.856638
111	1981	USA	8.209687
111	1982	USA	5.787104
111	1983	USA	6.855165
111	1984	USA	5.567018
111	1985	USA	5.427435
111	1986	USA	5.576608
111	1987	USA	5.678556

On 20 May 2011 12:05, Nick Cox <n.j.cox@durham.ac.uk> wrote:
> If you fire up -egen, sd()- using the option -by(countryno year)- then the combinations of country and year correspond to single observations. There can be no variability in a single observation, but what overrides that zero is that the SD calculation uses (n - 1) rather than n in the divisor, so n - 1 = 0 in that case and missing results are thus shown.
>
> Your problem remains vaguely stated, but note that SDs for countries would be
>
> egen SD = sd(gdp), by(countryno)
>
> and -- in general -- that the -by()- option defines groups within which SD is calculated.
>
> Nick
> n.j.cox@durham.ac.uk
>
> lreine ycenna
>
> I have a list of countries and their annual GDP growth rates. I need
> to calculate the standard deviations of the growth rate. But I seem to
> have done it wrong:
>
> Using the command egen sd = sd(gdp) gives me the same number for the
> entire column
>
> Using egen sd= sd (gdp), by(countryno year) gives me missing values.
>
> Am I missing something?
>
>
> My panel data looks like this:
>
> countryno  year         gdp
> 122     1995    1.448922432
> 122     1996    1.802327838
> 122     1997    1.939189289
> 122     1998    3.486108757
> 122     1999    3.057156394
> 122     2000    3.403232592
> 122     2001    0.706544087
> 122     2002    0.572674299
> 122     2003    0.806288363
> 122     2004    2.143516615
> 122     2005    2.0000
> 122     2006    3.021230987
> 124     1970    6.058892112
> 124     1971    3.1155209
> 124     1972    4.699826137
> 124     1973    6.212549621
> 124     1974    4.21133101
> 124     1975    -2.174872486
> 124     1976    5.612220928
> 124     1977    0.585991929
> 124     1978    2.844723097
> etc..
>
> *
> *   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/
```