Announcement

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

  • How generate variable that indicates current and prior event occurrence for id in panel data

    Dear Statalist.

    I am trying to generate a variable *wanted* that takes on the value of 1 for each actor_id, and all subsequent observations for each actor_id, that experiences event == 1. Event == 1 may occur several times in the panel for a given actor_id, so I am trying to generate *wanted* based on the first occurrence of event==1 for each actor_id.

    My data structure looks like this:
    time group_id actor_id event wanted
    1 1 1
    1 1 2
    1 2 3
    1 2 4 1 1
    2 3 1 1 1
    2 3 5
    2 3 3
    2 4 4 1
    2 4 8
    3 5 1 1
    3 5 2
    3 6 3
    3 6 4 1 1

    Any and all input on this problem would be much appreciated.

    Thank you so much.

    Regards,
    Erik

  • #2
    http://www.stata.com/support/faqs/da...t-occurrences/

    Comment


    • #3
      Thank you, Nick.

      I applied the following code following the suggested FAQ:

      by actor_id (time), sort : gen byte wanted = sum(event) == 1


      The results I the code generates are as follows:
      time group_id actor_id event wanted
      1 1 1 0 0
      2 3 1 1 1
      3 5 1 0 1
      1 1 2 0 0
      3 5 2 0 0
      1 2 3 0 0
      2 3 3 0 0
      3 6 3 0 0
      1 2 4 1 1
      2 4 4 0 1
      3 6 4 1 0
      2 3 5 0 0
      2 4 8 0 0
      The code does not generate wanted==1 for actor_id ==4 at time == 3.

      Is there a way to tweak the code to generate wanted == 1 for actor_id ==4 at time==3 as well?

      Sincerely,
      Erik

      Comment


      • #4
        The sum() function generates running totals, so for actor_id==4 at time==3 sum(event) = 2 . What you want is
        Code:
        by actor_id (time), sort : gen byte wanted2 = sum(event) >= 1

        Comment


        • #5
          Thank you, William.
          That worked splendidly.

          Sincerely,
          Erik.

          Comment

          Working...
          X