Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Collapse with sum function and missing values


From   Scott Merryman <[email protected]>
To   [email protected]
Subject   Re: st: Collapse with sum function and missing values
Date   Wed, 10 Feb 2010 16:29:06 -0600

Here one way, though calling -collapse- twice might not be the most
efficient method:

clear
input group x
  group           x
 1 10
 1 11
 1 12
 2 20
 2 21
 2 22
 3 .
 4 .
 4 .
 5  0
 5  0
 end
 tempfile tmp1 tmp2
 save `tmp1'
 collapse (sum) x, by(gro)
 sort gr

 save `tmp2'
 use `tmp1'
 collapse (sum) x, by(gro) cw
 sort gr
 merge 1:m  gr using `tmp2',
 replace x = . if _m ==2
 drop _m
 l


Scott

On Wed, Feb 10, 2010 at 4:13 PM, Michael Mitchell
<[email protected]> wrote:
> Thanks all, both for the general thoughts on the underlying rationale
> for this, as well as the practical suggestions. As some noted, I could
> do this...
>
> . collapse (sum) x (count) nonmiss=x, by(group)
> . replace x=. if nonmiss==0
>
> I have about 100 "x" values, and there is no pattern to the names. All
> methods I can think of for ensuring that the "x" values are . when all
> members of the group are . involve either substantial typing or a fair
> amount of programming. Am I overlooking a simple solution for this?
>
> Thanks,
>
> Michael
>
>
>
> On Wed, Feb 10, 2010 at 7:14 AM, Martin Weiss <[email protected]> wrote:
>>
>> <>
>>
>>
>>
>> As in http://www.stata.com/statalist/archive/2010-02/msg00428.html ?
>>
>>
>>
>> HTH
>> Martin
>>
>>
>> -----Ursprüngliche Nachricht-----
>> Von: [email protected]
>> [mailto:[email protected]] Im Auftrag von Elizabeth
>> Allred
>> Gesendet: Mittwoch, 10. Februar 2010 16:12
>> An: [email protected]
>> Betreff: Re: st: Collapse with sum function and missing values
>>
>> Michael might use collapse (count) and then set his sum to missing when
>> count=0.
>>
>>>>> On 2/10/2010 at 10:06 AM, in message
>> <[email protected]>, Kit Baum <[email protected]> wrote:
>> <> >
>>> This is somewhat semantic. The presence of 3 and 4 in the group id
>> suggests
>>> that such groups exist; they merely have no members in the present sample.
>> It
>>> should be easy enough to -mvdecode- x==0 to x==.
>>>
>>> This reminds me of a grouse I had about the calculation of the s.d. of
>> data
>>> that were all missing. The mean of these data was computed properly as
>>> missing, but the s.d. was reported as 0. Pedantically, as all values took
>> on
>>> the same NAN value, there was indeed zero variance. I convinced StataCorp
>>> that this was not a good idea, and that the s.d. or variance of data that
>> are
>>> all missing is indeed missing. That is now what -tabstat- does.
>>>
>>> Kit
>>>
>>> On Feb 10, 2010, at 2:33 AM, Michael wrote:
>>>
>>>>  Shouldn't the value of -x- for groups 3 and 4 be missing, not zero.
>>>> To me, the sum of a series of missing values is a missing value. I am
>>>> doing a collapse for about 100 variables (100 x values) and need the
>>>> value to be defined as missing (not 0) in such cases. Any ideas?
>>>
>>>
>>> Kit Baum   |   Boston College Economics & DIW Berlin   |
>>> http://ideas.repec.org/e/pba1.html
>>>                               An Introduction to Stata Programming  |
>>> http://www.stata-press.com/books/isp.html
>>>    An Introduction to Modern Econometrics Using Stata  |
>>> http://www.stata-press.com/books/imeus.html
>>>
>>>
>>> *
>>> *   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/
>>
>>
>> *
>> *   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/
>

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



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index