Announcement

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

  • Generating a True-False Binary from by virtue of the order of events

    Hello all,

    I have a dataset that has an ID indicator, a year and an X variable. The data looks like so:
    ID Year X1
    1 2005 A
    1 2011 B
    2 2006 B
    2 2007 A
    3 2003 A
    3 2012 B
    4 2001 B
    4 2003 A
    5 2005 B
    5 2009 A
    For some units, "A" occurs first chronologically and for others, "B" occurs first chronologically. I want to generate a binary variable by ID that will be 1 if A occurs first chronologically and 0 if B occurs first. I'm sure this is a bysort: egen operation but I'm having a hard time wrapping my head around how to accomplish this.value

    Thank you very much in advance!

  • #2
    Code:
    . clear
    
    . input id        year    str1 x
    
                id       year          x
      1. 1       2005    A
      2. 1       2011    B
      3. 2       2006    B
      4. 2       2007    A
      5. 3       2003    A
      6. 3       2012    B
      7. 4       2001    B
      8. 4       2003    A
      9. 5       2005    B
     10. 5       2009    A
     11. end
    
    . bys id (year) : gen byte afirst = x[1] == "A"
    
    . list, sepby(id)
    
         +------------------------+
         | id   year   x   afirst |
         |------------------------|
      1. |  1   2005   A        1 |
      2. |  1   2011   B        1 |
         |------------------------|
      3. |  2   2006   B        0 |
      4. |  2   2007   A        0 |
         |------------------------|
      5. |  3   2003   A        1 |
      6. |  3   2012   B        1 |
         |------------------------|
      7. |  4   2001   B        0 |
      8. |  4   2003   A        0 |
         |------------------------|
      9. |  5   2005   B        0 |
     10. |  5   2009   A        0 |
         +------------------------+
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Worked great! Thank you so much!

      Comment

      Working...
      X