Announcement

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

  • Dropping observations after the first time the treatment turns off

    Hello,
    I need some help finding a solution to the following problem:
    I want to drop all the observations that occur at and after the first time a treatment turns off (goes from 1 to 0), no matter if it turns on again.

    The following table is an example of my data:
    id year treatment
    1 2012 0
    1 2013 1
    1 2014 1
    1 2015 1
    1 2016 1
    1 2017 0
    1 2018 0
    1 2019 1
    1 2020 1
    1 2021 0
    2 2012 0
    2 2013 0
    2 2014 1
    2 2015 1
    2 2016 0
    2 2017 0
    2 2018 0
    2 2019 1
    2 2020 1
    2 2021 1
    3 2012 0
    3 2013 0
    3 2014 0
    3 2015 0
    3 2016 0
    3 2017 1
    3 2018 1
    3 2019 1
    3 2020 0
    3 2021 0
    I want to drop the following observations: for id 1, drop observations for years 2017 through 2021 as they come at or after the first time treatment turns off (in 2017). Similarly, for id 2, drop observations for years 2016 through 2021, and for id 3, drop observations for years 2020 and 2021.

    I am unable to figure this out since the treatment can turn on again for each id and the criterion I set could apply to those observations as well, but I do not want those observations as the treatment has turned off once already.
    Any help would be greatly appreciated!

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte id int year byte treatment
    1 2012 0
    1 2013 1
    1 2014 1
    1 2015 1
    1 2016 1
    1 2017 0
    1 2018 0
    1 2019 1
    1 2020 1
    1 2021 0
    2 2012 0
    2 2013 0
    2 2014 1
    2 2015 1
    2 2016 0
    2 2017 0
    2 2018 0
    2 2019 1
    2 2020 1
    2 2021 1
    3 2012 0
    3 2013 0
    3 2014 0
    3 2015 0
    3 2016 0
    3 2017 1
    3 2018 1
    3 2019 1
    3 2020 0
    3 2021 0
    end
    
    by id (year), sort: gen turn_offs = sum(treatment == 0 & treatment[_n-1] == 1)
    drop if turn_offs > 0
    In the future, when showing data examples, please use the -dataex- command to do so, as I have done here. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      Apologies, I will post the data in the correct format in the future.

      Thank you so much for your help! The code worked as I needed.

      Comment


      • #4
        Another way in to such problems is to ask what is the first date that something happens in each panel?

        Code:
        . bysort id (year) : gen event = treatment == 0 & treatment[_n-1] == 1
        
        . by id : egen first = min(cond(event, year, .))
        
        .
        . tabdisp id, c(first)
        
        ----------------------
               id |      first
        ----------+-----------
                1 |       2017
                2 |       2016
                3 |       2020
        ----------------------
        For more general discussions see https://www.stata.com/support/faqs/d...t-occurrences/

        https://www.stata-journal.com/articl...article=dm0055 esp. Section 9

        EDIT And another

        Code:
        . bysort id (year) : gen event = treatment == 0 & treatment[_n-1] == 1
        
        . gsort id -event year 
        
        . by id : gen first = year[1] if event[1]
        
        . 
        . tabdisp id, c(first)
        
        ----------------------
               id |      first
        ----------+-----------
                1 |       2017
                2 |       2016
                3 |       2020
        ----------------------
        Last edited by Nick Cox; 01 Mar 2023, 11:59.

        Comment

        Working...
        X