Announcement

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

  • First occurrence for every categorical variable

    Dear Stata Users,

    I have the following problem. I've panel data and I would like to create a dummy which indicates whether for that specific individual, the categorie occurred for the first time or not.

    So, my data looks like this:

    Code:
    ID Time Category
    1  1    A
    1  1    B
    1  1    C
    1  1    D
    1  2    B
    1  2    D
    1  2    E
    1  3    A
    1  3    B
    1  3    E
    2  1    B
    2  1    D
    2  1    E
    2  2    B
    2  2    E
    2  3    A
    2  3    E
    3  1    B
    3  2    A
    3  2    B
    3  2    D
    3  2    E
    3  3    A
    3  3    B
    3  3    C
    3  3    D
    And I would like to have a variable 'first', like this:

    Code:
    ID Time Category First 
    1  1    A        1
    1  1    B        1
    1  1    C        1
    1  1    D        1
    1  2    B        0
    1  2    D        0
    1  2    E        1
    1  3    A        0
    1  3    B        0
    1  3    E        0
    2  1    B        1
    2  1    D        1
    2  1    E        1
    2  2    B        0
    2  2    E        0
    2  3    A        1
    2  3    E        0
    3  1    B        1
    3  2    A        1 
    3  2    B        0
    3  2    D        1
    3  2    E        1
    3  3    A        0
    3  3    B        0
    3  3    C        1
    3  3    D        0
    This seemed like a relatively simple problem. However, my problem is that I have a lot of different categories, so I think I need some kind of loop and I have some difficulties with figuring out how to set up a loop. Could you maybe suggest me how to deal with this?

    Kind regards,
    Frank

  • #2
    I believe this will work for you:
    Code:
    bysort ID Category (Time): gen byte first = (_n ==1)

    Comment


    • #3
      Originally posted by Mike Lacy View Post
      I believe this will work for you:
      Code:
      bysort ID Category (Time): gen byte first = (_n ==1)
      Great! This does work indeed. Much simpler than I thought. Thanks a lot.

      Comment

      Working...
      X