Announcement

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

  • Creating a binary variable for reconviction

    Hello!

    I am trying to create a variable (Reconvicted) that takes on a value of 1 if a person has reconvicted a crime and 0 if they have not. Each offender in my dataset has a unique ID. The variable, "booking year" is the year that they were convicted (i.e., if a person has multiple booking years, they have been reconvicted of a crime). I was hoping that someone could please give me some pointers on how to do this. Thank you!

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long offenderid int bookingyear
     21 2021
     21 2021
     21 2021
     21 2021
     21 1988
     21 1988
     21 1988
     21 1988
     21 1988
     61 1991
     61 1991
     61 1991
     70 1991
     70 1991
     70 1991
     70 1991
     70 1991
     70 1991
     70 1991
     70 1991
     70 1991
     70 1991
     70 1991
     70 2018
     70 2018
     70 2018
     70 2018
     70 2018
     70 2018
    132 2017
    132 2017
    132 2017
    132 2017
    132 2017
    132 2017
    132 2023
    148 2016
    170 2021
    246 2018
    246 2018
    246 2018
    275 1992
    275 1992
    275 2019
    275 2019
    294 2016
    294 2016
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    304 1987
    332 2018
    332 2018
    345 2016
    345 2016
    345 2020
    345 2019
    345 2019
    357 2017
    357 2017
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 1993
    357 2018
    end

  • #2
    I don't understand what you want. Let's look at id 21. That person has four convictions in the record, but all of them are in year 2021. So is this person reconvicted in all observations other than the first one? Or are they never reconvicted because there aren't two or more different years?

    If it's just a matter of more than one booking, it would be:
    Code:
    sort offenderid bookingyear, stable
    by offenderid: gen byte reconvicted = _n > 1
    If it's a matter of bookings in different years, then:
    Code:
    by offenderid: gen n_distinct_years = sum(bookingyear != bookingyear[_n-1])
    gen byte reconvicted = (n_distinct_years > 1)

    Comment


    • #3
      Thank you so much Clyde!

      Comment

      Working...
      X