Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Flag variation within unit by creating new variable

    Hi all,

    Quite a silly question, but somehow I can't get what I want from Stata.

    My data:

    Code:
    input float(token1 time) byte canton
      1  2  4
      1  3 21
      1 11 23
      8 12  6
      8 14  6
      8 15  6
      8 17  6
      8 18  6
      8 23  6
      8 24  6
     10 11  6
     10 13  6
     10 14  6
     10 17  6
     10 25  5
    I would like to create a dummy assuming the value of 1 if within token1, there is variation in canton. This new dummy, let's call it
    Code:
    var_canton_within_tok
    should therefore be equal to 1 for tokens 1 and 10, but equal to 0 for token 8.

    The code I've written is
    Code:
    bys token1: g var_canton_within_tok=cond(canton[_n]!=canton[_n-1],1,0)
    (My data are not xtset so I cannot use the lag operator)

    But this code just gives me 1s everywhere, even when there is in fact no variation of canton within token.

    Could anyone highlight the mistake I'm making?

  • #2
    See this FAQ: https://www.stata.com/support/faqs/d...ions-in-group/

    Comment


    • #3
      Ah it was the sort order. I somehow forgot about that post, thanks!

      Comment

      Working...
      X