Announcement

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

  • Filling missing variable

    I have panel dataset that observes individuals of organisations over a 16 year period. During the 16 year period, some organisation undergoes a treatment. Currently, i have a marker (tretament_flag) that indicate whether the organisation is treated or no. But it only indicates (takes value 1) only in the year of treatment. But I want to extend the treatment_flag further to take value 1 after the treatment has occurred. Currently, these values are shown as missing.

    I tried:
    sort organisation year
    by organisation year: replace treatment_flag = treatment_flag[1]

    but this doesn't seem to be working

    Any help would be much appreciated

    Best
    Danula

  • #2

    That is legal but won't change anything.

    Code:
    bysort organisation (year): replace treatment_flag = treatment_flag[1]

    Comment


    • #3
      I am guessing that your treatment_flag is not only missing after the observation with the value 1, but is also missing before that observation. If so, you will want to replace the missing values with 0 before the observation with the 1, and with 1 after that observations. Missing values should be reserved for data values that you do not know, not used to mean "no treatment".

      With that said, this example should be a starting point.
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(org year tf)
      1 2011 1
      1 2012 .
      1 2013 .
      1 2014 .
      2 2011 .
      2 2012 1
      2 2013 .
      2 2014 .
      3 2011 .
      3 2012 .
      3 2013 1
      3 2014 .
      4 2011 .
      4 2012 .
      4 2013 .
      4 2014 1
      5 2011 .
      5 2012 .
      5 2013 .
      5 2014 .
      end
      bysort org (year): replace tf = 0 if _n==1 & tf==.
      bysort org (year): replace tf = tf[_n-1] if tf==.
      list, sepby(org) noobs
      Code:
      . list, sepby(org) noobs
      
        +-----------------+
        | org   year   tf |
        |-----------------|
        |   1   2011    1 |
        |   1   2012    1 |
        |   1   2013    1 |
        |   1   2014    1 |
        |-----------------|
        |   2   2011    0 |
        |   2   2012    1 |
        |   2   2013    1 |
        |   2   2014    1 |
        |-----------------|
        |   3   2011    0 |
        |   3   2012    0 |
        |   3   2013    1 |
        |   3   2014    1 |
        |-----------------|
        |   4   2011    0 |
        |   4   2012    0 |
        |   4   2013    0 |
        |   4   2014    1 |
        |-----------------|
        |   5   2011    0 |
        |   5   2012    0 |
        |   5   2013    0 |
        |   5   2014    0 |
        +-----------------+

      Comment


      • #4
        William read this much more carefully than I did!

        Comment


        • #5
          Thanks alot both it works!

          Comment


          • #6
            On a different note. Now I am changing the treatment. I want the treatment to last only three consecutive years and takes 0 afterwards. For example:

            Inst Year Treatment

            1 2006 1
            1 2007 .
            1 2008 .
            1 2009 .
            1 2010
            1 2011
            1 2012
            1 2013 1
            1 2014
            1 2015
            1 2016
            2 2006
            2 2007 1
            2 2008
            2 2009
            2 2010 1
            2 2011
            2 2012
            2 2013
            2 2014 1
            2 2015
            2 2016

            So I want to extend the treatment to only 3 years following treatment.

            How can i do this.

            Many Thanks
            Best
            Danula

            Comment


            • #7
              I think this is what you want.
              Code:
              * Example generated by -dataex-. To install: ssc install dataex
              clear
              input float(org year tf)
              1 2006 1
              1 2007 .
              1 2008 .
              1 2009 .
              1 2010 .
              1 2011 .
              1 2012 .
              1 2013 1
              1 2014 .
              1 2015 .
              1 2016 .
              2 2006 .
              2 2007 1
              2 2008 .
              2 2009 .
              2 2010 1
              2 2011 .
              2 2012 .
              2 2013 .
              2 2014 1
              2 2015 .
              2 2016 .
              end
              clonevar tf_ext = tf
              bysort org (year): replace tf_ext = 1 if inlist(1,tf,tf[_n-1],tf[_n-2])
              list, sepby(org) noobs
              Code:
              .  list, sepby(org) noobs
              
                +--------------------------+
                | org   year   tf   tf_ext |
                |--------------------------|
                |   1   2006    1        1 |
                |   1   2007    .        1 |
                |   1   2008    .        1 |
                |   1   2009    .        . |
                |   1   2010    .        . |
                |   1   2011    .        . |
                |   1   2012    .        . |
                |   1   2013    1        1 |
                |   1   2014    .        1 |
                |   1   2015    .        1 |
                |   1   2016    .        . |
                |--------------------------|
                |   2   2006    .        . |
                |   2   2007    1        1 |
                |   2   2008    .        1 |
                |   2   2009    .        1 |
                |   2   2010    1        1 |
                |   2   2011    .        1 |
                |   2   2012    .        1 |
                |   2   2013    .        . |
                |   2   2014    1        1 |
                |   2   2015    .        1 |
                |   2   2016    .        1 |
                +--------------------------+

              Comment


              • #8
                Here is another way to do it, with rangestat (SSC): As usual taking a maximum ignores missings unless those are all the values that there are (in an interval, in this instance).

                Code:
                clear
                input float(org year tf)
                1 2006 1
                1 2007 .
                1 2008 .
                1 2009 .
                1 2010 .
                1 2011 .
                1 2012 .
                1 2013 1
                1 2014 .
                1 2015 .
                1 2016 .
                2 2006 .
                2 2007 1
                2 2008 .
                2 2009 .
                2 2010 1
                2 2011 .
                2 2012 .
                2 2013 .
                2 2014 1
                2 2015 .
                2 2016 .
                end
                rangestat (max) tf_ext=tf, interval(year -2 0) by(org)
                list, sepby(org) noobs
                
                  +--------------------------+
                  | org   year   tf   tf_ext |
                  |--------------------------|
                  |   1   2006    1        1 |
                  |   1   2007    .        1 |
                  |   1   2008    .        1 |
                  |   1   2009    .        . |
                  |   1   2010    .        . |
                  |   1   2011    .        . |
                  |   1   2012    .        . |
                  |   1   2013    1        1 |
                  |   1   2014    .        1 |
                  |   1   2015    .        1 |
                  |   1   2016    .        . |
                  |--------------------------|
                  |   2   2006    .        . |
                  |   2   2007    1        1 |
                  |   2   2008    .        1 |
                  |   2   2009    .        1 |
                  |   2   2010    1        1 |
                  |   2   2011    .        1 |
                  |   2   2012    .        1 |
                  |   2   2013    .        . |
                  |   2   2014    1        1 |
                  |   2   2015    .        1 |
                  |   2   2016    .        1 |
                  +--------------------------+
                Last edited by Nick Cox; 02 Mar 2018, 10:52.

                Comment


                • #9
                  Thanks, Nick! This is perfect!

                  best
                  Danula

                  Comment

                  Working...
                  X