Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: adding or counting values as an array


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: adding or counting values as an array
Date   Thu, 6 Oct 2011 08:41:51 +0100

As yet another way, see the -egen- function -anycount()-, to be called
with values 0 and 1 separately. However, this ignores any observation
in which any of these variables is missing.

On Thu, Oct 6, 2011 at 8:20 AM, Nick Cox <njcoxstata@gmail.com> wrote:
> gen ones = 0
> gen zeros = 0
>
> qui foreach v of var y2006q_1-y2010q_3 {
>     replace ones = ones + (`v' == 1)
>     replace zeros = zeros + (`v'' == 0)
> }
>
> If there are no missings,
>
> egen ones = rowtotal(y2006q_1-y2010q_3)
> gen zeros = <number of variables> - ones
>
> The yearwise calculation is a variation on the same theme.
>
> See also SJ-9-1  pr0046  . . . . . . . . . . . . . . . . . . .
> Speaking Stata: Rowwise
>        (help rowsort, rowranks if installed) . . . . . . . . . . .  N. J. Cox
>        Q1/09   SJ 9(1):137--157
>        shows how to exploit functions, egen functions, and Mata
>        for working rowwise; rowsort and rowranks are introduced
>
> For most Stata analyses, -reshape long- is advised.
>
> On Thu, Oct 6, 2011 at 6:49 AM, Daniel Exeter <d.exeter@auckland.ac.nz> wrote:
>
>> In Stata 11 (win7, 64bit),  I have a dataset with quarterly observations of events from 2006 to 2011.
>> Each field is named y2006_q1, y2006_q2 .. y2011_2
>>
>> The data is dichotomous (0/1) and I want to know:
>> -for the entire period (q1 2006 to q3 2011)  and separately for each year
>> -       how many quarters each event was observed
>> -       how many quarters each event was not observed
>>
>>
>> is there an easy way to code something that would do this:
>>
>> gen included = sum (yr20*) if yr20* ==1
>> gen excluded = count (yr20*) if  yr20* ==0
>>
>> and
>>
>> gen inc06 = sum (yr2006*) if yr2006 ==1
>> gen exc06 = count(yr2006*) if yr2006 ==0
>>
>

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index