# st: Re: collapse problem

 From John Hennen To statalist@hsphsun2.harvard.edu Subject st: Re: collapse problem Date Tue, 10 Sep 2002 20:44:42 -0400

```"Riano, Alejandro" wrote:

> I'm collapsing the standard deviation for a set of variables (monthly
> frequency):
>
> collapse (sd) er rer, by(country_name year)
>
> but if for example one country doesn't have data for all the year, i'm
> getting a 0 instead of a missing value after doing the collapse, do you know
> how can i solve this ?

Are you certain that there isn't at least one observation in the missing-data
country for the year at issue?   Here is a counter-example using the auto.dta
dataset, showing that, when the variable of interest (mpg) is missing in a cell,
collapse returns sd = ., not zero.

use c:\stata\auto,replace
(1978 Automobile Data)

. drop if rep78 <3 | rep78==.            [to simplify the dataset]
(15 observations deleted)

. replace mpg = . if foreign ==0 & rep78 ==5    [to setup a cell with missing
mgp]
(2 real changes made, 2 to missing)

. tab foreign rep78

|        Repair Record 1978
Car type |         3          4          5 |     Total
-----------+---------------------------------+----------
Domestic |        27          9          2 |        38
Foreign |         3          9          9 |        21
-----------+---------------------------------+----------
Total |        30         18         11 |        59

. tab foreign rep78, sum(mpg)

Means, Standard Deviations and Frequencies of Mileage (mpg)

|      Repair Record 1978
Car type |         3          4          5 |     Total
-----------+---------------------------------+----------
Domestic |        19  18.444444          . | 18.861111
| 4.0856221  4.5856055          . | 4.1552224
|        27          9          0 |        36
-----------+---------------------------------+----------
Foreign | 23.333333  24.888889  26.333333 | 25.285714
| 2.5166115  2.7131368   9.367497 | 6.3098562
|         3          9          9 |        21
-----------+---------------------------------+----------
Total | 19.433333  21.666667  26.333333 |  21.22807
| 4.1413252  4.9348699   9.367497 | 5.8979946
|        30         18          9 |        57

. collapse (sd) sdmpg = mpg , by( foreign rep78)

. list
rep78   foreign     sdmpg
1.        3  Domestic   4.08562
2.        4  Domestic   4.58561
3.        5  Domestic         .
4.        3   Foreign   2.51661
5.        4   Foreign   2.71314
6.        5   Foreign    9.3675

john

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

• References:
• st: collapse