Announcement

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

  • Generating a Chronologically Ordered Count of an Event

    Hello,

    I am trying to generate a variable that tracks the number of times a unique event occurred to a specific actor in chronological order. For instance, the number of times Person 001 received traffic citations over the course of several years. The issue I am running into is the unique event "numbers" were randomly assigned in the original data, so when I try the following command:

    bysort specific_actor(ticket_number): gen ordered_count_of_citations=sum(traffic_ticket_numb er!=traffic_ticket_number[_n-1])

    I'm getting things counted in the (nonsensical) order of the ticket number rather than the order in which they occurred. I've tried to troubleshoot a few different ways, including creating a unique string identifier based on the year of violation and the ticket number but no matter what I do these randomized ticket numbers are mucking things up.

    Here is what my data looks like, and a column of what I WANT the data to look like when I'm done (thing_i_want)
    specific_actor violation_year ticket_number ordered_count_of_citations thing_i_want
    001 2001 78 4 1
    001 2004 63 3 2
    001 2004 63 3 2
    001 2006 21 1 3
    001 2007 22 2 4
    002 2010 88 1 1
    002 2011 89 2 2
    003 1999 54 1 1
    003 1999 93 3 2
    003 2001 84 2 3
    Thanks in advance for the help!
    Last edited by Kate KO; 25 Apr 2022, 12:38.

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str3 specific_actor int violation_year byte(ticket_number ordered_count_of_citations thing_i_want)
    "001" 2001 78 4 1
    "001" 2004 63 3 2
    "001" 2004 63 3 2
    "001" 2006 21 1 3
    "001" 2007 22 2 4
    "002" 2010 88 1 1
    "002" 2011 89 2 2
    "003" 1999 54 1 1
    "003" 1999 93 3 2
    "003" 2001 84 2 3
    end
    
    gen seq=_n
    bys specific_actor (violation_year seq): gen wanted= sum(ticket_number!= ticket_number[_n-1])
    Res.:

    Code:
    . l specific_actor violation_year ticket_number thing_i_want wanted , sepby(sp)
    
         +----------------------------------------------------+
         | specif~r   violat~r   ticket~r   thing_~t   wanted |
         |----------------------------------------------------|
      1. |      001       2001         78          1        1 |
      2. |      001       2004         63          2        2 |
      3. |      001       2004         63          2        2 |
      4. |      001       2006         21          3        3 |
      5. |      001       2007         22          4        4 |
         |----------------------------------------------------|
      6. |      002       2010         88          1        1 |
      7. |      002       2011         89          2        2 |
         |----------------------------------------------------|
      8. |      003       1999         54          1        1 |
      9. |      003       1999         93          2        2 |
     10. |      003       2001         84          3        3 |
         +----------------------------------------------------+
    
    .

    Comment

    Working...
    X