The very useful -statsby- has a bug in posting the results, and overwrites missing values with 0s.

I have reported it to Stata Corp, and they have kindly promised that they will fix it in the future.

However, I am doing something now, which is build on -statsby- and this bug bites me now, so I decided to give it a shot here. I was not able to figure out where the bug comes from, maybe somebody here will figure it out...

The following code illustrates the problem:

Code:
. sysuse auto, clear
(1978 Automobile Data)

. keep in 1/2
(72 observations deleted)

. gen n=_n

. bysort n: summ price [w=weight]

----------------------------------------------------------------------------------------------------------
-> n = 1
(analytic weights assumed)

    Variable |     Obs      Weight        Mean   Std. Dev.       Min        Max
-------------+-----------------------------------------------------------------
       price |       1        2930        4099          .       4099       4099

----------------------------------------------------------------------------------------------------------
-> n = 2

    Variable |     Obs      Weight        Mean   Std. Dev.       Min        Max
-------------+-----------------------------------------------------------------
       price |       1        3350        4749          .       4749       4749


. statsby sd = r(sd), by(n) clear nodots: summ price [w=weight]

      command:  summarize price [fweight= weight]
           sd:  r(sd)
           by:  n


. list

     +--------+
     | n   sd |
     |--------|
  1. | 1    0 |
  2. | 2    0 |
     +--------+
As we can see above, because we have one observation per group, the Standard Deviation is missing, as it should be. But then when I do -statsby-, it turns the missing Standard Deviations into 0s.

Curiously enough, the problem does not arise if I do not apply weighted estimation:

Code:
. sysuse auto, clear
(1978 Automobile Data)

. keep in 1/2
(72 observations deleted)

. gen n=_n

. bysort n: summ price

----------------------------------------------------------------------------------------------------------
-> n = 1

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
       price |          1        4099           .       4099       4099

----------------------------------------------------------------------------------------------------------
-> n = 2

    Variable |        Obs        Mean    Std. Dev.       Min        Max
-------------+---------------------------------------------------------
       price |          1        4749           .       4749       4749


. statsby sd = r(sd), by(n) clear nodots: summ price

      command:  summarize price
           sd:  r(sd)
           by:  n


. list

     +--------+
     | n   sd |
     |--------|
  1. | 1    . |
  2. | 2    . |
     +--------+
Here -statsby- correctly passes over the missing values of Standard Deviation.

The code of -statsby- is very long, and on the basis of the information above I do not know where in the code to look for the error. Hopefully somebody here can figure this out.