Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: Re: st: Count variable


From   n j cox <[email protected]>
To   [email protected]
Subject   Re: Re: st: Count variable
Date   Wed, 21 Mar 2007 18:51:23 +0000

Yes and no.

Your version of the code runs (correcting a mistake
pointed out separately):

gen count = 0
by id office : replace count =
     cond(_n == 1, 1, 1 + count[_n-1]) if win

That would be wrong if the data are not sorted
-id office electionyear-.

In giving code solutions, it is best not
to make assumptions about -sort- order.
Specifying the -sort- order within code
will do no harm if data are sorted as they
should be, but the converse isn't true.

There are two solutions which I think
are acceptable (modulo use of variations
on -bysort-):

(1)
bysort id office (electionyear) : gen count = 0
by id office : replace count =
     cond(_n == 1, 1, 1 + count[_n-1]) if win

(2)
gen count = 0
bysort id office (electionyear) : replace count =
     cond(_n == 1, 1, 1 + count[_n-1]) if win

You might prefer (2) on the grounds that -count-
is initialised as 0, regardless of anything else.
I have no objection to that at all -- in fact
it is probably clearer -- but if so, the -sort-
order should be spelled out in the following line.

Nick
[email protected]

Jeph Herrin

um, I think that first line can be:

  gen count = 0


n j cox (corrected)

> Try
>
> bysort id office (electionyear) : gen count = 0
>
> by id office : replace count =
>     cond(_n == 1, 1, 1 + count[_n-1]) if win

> Felipe Botero, modulo a mass of MIME/HTML, asked
>
> I have data on congressional careers and need to create a variable that
> counts the number of *consecutive* terms served in the same office.
>
>
> I used the following code but it didn't do the trick:
>
> sort id office electionyear
>
> by id office: gen count = cond(win==0,0,sum(win))
>
> list id office win count
>
>   +---------------------------------+
>
>   |  id   office    win       count |
>
>   |---------------------------------|
>
>   | 153          1     0          0 |
>   | 153          1     1          1 |
>   | 153          1     0          0 |
>   | 153          1     1          2 |  <-- should start again with 1
>   | 153          1     1          3 |
>   | 153          1     1          4 |
>   +---------------------------------+
>
> The problem is that I need the counter to start back at 1 after each
> occurrence of win==0.

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



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