Announcement

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

  • Create dummy for group variable based on status at a specific time

    Hello,
    I have a dataset that is of the format group | year | value. I would like to create an additional variable "started_by_2012" that is 1 for all observations for group if "value" is 1 before or during year 2012. Here is an example dataset:
    Code:
    Group  |   Year   |  Value   |    started_by_2012
    1             2011       0
    1             2012       0
    1             2013       0
    1             2014       0
    2             2011       1
    2             2012       1
    2             2013       1
    2             2014       1
    3             2011       1
    3             2012       0
    3             2013       0
    3             2014       0
    4             2011       0
    4             2012       0
    4             2013       1
    4             2014       1
    I want the value of "started_by_2012" to be 0 for all obs for group 1, 1 for all obs for group 2, 1 for all obs for group 3 (since the Value for group 3, year 2011 is 1, even though later years are 0), and 0 for all obs for group 4 (even though Value is 1 for 2013 and 2014, it was 0 prior to 2012 and in 2012).

    I believe I should be using some version of egen and by (or bysort), but have not been able to find the right way to do this despite scouring the Statalist.

    Thanks in advance!

    Best,
    Ed

  • #2
    Originally posted by Ed Nagel View Post
    I believe I should be using some version of egen and by (or bysort), but have not been able to find the right way to do this despite scouring the Statalist.
    Maybe something like this:
    Code:
    bysort Group: egen byte started_by_2012 = max(Value & Year <= 2012)

    Comment


    • #3
      Thanks Joseph. I believe that has worked, though I don't quite understand why. Could you possibly explain how that works just so I can confirm it is doing what I want? Thanks!

      Comment


      • #4
        Originally posted by Ed Nagel View Post
        Could you possibly explain how that works. . . ?
        The argument is a Boolean expression, which in Stata evaluates to a value of either zero or one. Within each group egen assigns the maximum of that rowwise evaluation to the variable created.

        Comment


        • #5
          Ah, okay. That makes sense. Thank you very much for your help!

          Comment


          • #6
            See also

            https://www.stata.com/support/faqs/d...ble-recording/

            https://journals.sagepub.com/doi/pdf...36867X19830921

            for longer-winded or more extensive discussions on the same theme.

            Comment

            Working...
            X