Announcement

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

  • filling in values between dates

    I want to fill in values between date ranges.

    For example, someone is good until they are bad. And then once they are bad, they remain bad until they switch to good.

    Here is my example:

    Code:
    clear
    input year int good int bad
    2000 1 0
    2001 0 0
    2002 0 1
    2003 0 0
    2004 0 0
    2005 0 0
    2006 1 0
    2007 0 0
    2008 0 1
    end
    And the solution would look like this:

    Code:
    clear
    input year int good int bad goodpath badpath
    2000 1 0 1 0
    2001 0 0 1 0
    2002 0 1 0 1
    2003 0 0 0 1
    2004 0 0 0 1
    2005 0 0 0 1
    2006 1 0 1 0
    2007 0 0 1 0
    2008 0 1 0 1
    end
    Where the goodpath/badpath variables take on the trailing values until there is a switch from good to bad or vice versa. To be clear, I am looking for a way to create the goodpath and badpath variables.

    I can do this if there is only 1 switch (from good to bad or vice versa). If only one switch, you find first occurrence of good and first occurrence of bad.

    Code:
    egen first_yr_good = min(cond(good == 1, year, .))
    egen first_yr_bad = min(cond(bad == 1, year, .))
    gen goodpath=1 if year>=first_yr_good
    replace goodpath=0 if year>=first_yr_bad & first_yr_good < first_yr_bad
    But I get tripped up when there are multiple switches (good --> bad --> good, etc). Don't know how to deal with the third, fourth, etc switches.

    I could code one line for every possible scenario, but that seems inefficient.

    Thanks for your help in advance

  • #2
    Code:
    gen goodpath= cond(good, 1, cond(bad, 2, 0))
    replace goodpath= goodpath[_n-1] if goodpath==0 & _n!=1
    replace goodpath=0 if goodpath==2
    Same logic for the "badpath" variable.

    Comment


    • #3
      Thanks alot Andrew.

      Comment

      Working...
      X