Announcement

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

  • Sorting the data set

    Hello!

    Currently, I am working with Stata 18.0. My data looks like,

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(ID event year Event_year)
    1    . 2001 2002
    1 2002 2002 2002
    1    . 2003 2002
    1    . 2004 2002
    1    . 2005 2002
    1    . 2006 2002
    1    . 2007 2002
    2    . 2001 2005
    2    . 2002 2005
    2    . 2003 2005
    2    . 2004 2005
    2 2005 2005 2005
    2    . 2006 2005
    2    . 2007 2005
    3    . 2001 2003
    3    . 2002 2003
    3 2003 2003 2003
    3    . 2004 2003
    3    . 2005 2003
    3    . 2006 2003
    3    . 2007 2003
    4    . 2001    .
    4    . 2002    .
    4    . 2003    .
    4    . 2004    .
    4    . 2005    .
    4    . 2006    .
    4    . 2007    .
    end

    Now, here, an event occurs at a different period for every ID, listed under the variable event. I want to create a variable Event_year, which will list the event period for every year. That is, I want to get the following result:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input float(ID event year Event_year)
    1    . 2001 2002
    1 2002 2002 2002
    1    . 2003 2002
    1    . 2004 2002
    1    . 2005 2002
    1    . 2006 2002
    1    . 2007 2002
    2    . 2001 2005
    2    . 2002 2005
    2    . 2003 2005
    2    . 2004 2005
    2 2005 2005 2005
    2    . 2006 2005
    2    . 2007 2005
    3    . 2001 2003
    3    . 2002 2003
    3 2003 2003 2003
    3    . 2004 2003
    3    . 2005 2003
    3    . 2006 2003
    3    . 2007 2003
    4    . 2001    .
    4    . 2002    .
    4    . 2003    .
    4    . 2004    .
    4    . 2005    .
    4    . 2006    .
    4    . 2007    .
    end

    Could anyone please help me with the code here?

    Thank you so much!

  • #2
    One event per ID?

    Code:
    sort ID event
    by ID (event) : generate int Event_year = event[1]

    Comment


    • #3
      Dear daniel klein,

      Yes, thank you so much for the code. It worked!

      Meanwhile, I applied the following code.

      Code:
      bys ID (event): gen wanted= event[1]
      Could you please tell me how this code is different from yours? Or do they work in the same way?

      Thank you so much!

      Comment


      • #4
        Your code stores wanted as a float (or double, depending on your settings), mine stores Event_year as an int. Otherwise, there is no difference. I have recently started to prefer

        Code:
        sort ...
        by ...
        over

        Code:
        bys ...
        because I find the former makes it more explicit that the data are sorted.

        Comment


        • #5
          I got it! Thank you!

          Comment

          Working...
          X