Announcement

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

  • Normalize observations according to year of policy introduction

    Dear all,

    I have a panel data set where I observe IDs over time and when each ID was treated by a policy.

    I want to make a figure where I show trends in observations over time, but where I normalize my observations according the year of a policy introduction. So that, instead of the year on the x-axis I would have the year relative to the introduction on the x-axis (…,-2,-1,0,2,1).

    In my example below, I therefore want to generate a NewVar that takes the value 0 when the dummy Treatment was first turned on and the values -1 and +1 in the years before and after, respectively. This will hopefully allow me to graph tendencies in another variable (not in the table) before and after the policy introduction.

    Example:
    ID Time Treatment NewVar
    1 2005 0 -1
    1 2006 1 0
    1 2007 1 1
    1 2008 1 2
    2 2005 0 -2
    2 2006 0 -1
    2 2007 1 0
    2 2008 1 1
    3 2005 0 -3
    3 2006 0 -2
    3 2007 0 -1
    3 2008 1 0
    My initial thoughts was that a code starting with the following could be of use:
    Code:
    bysort ID (Time) : gen NewVar=0 if ...
    Thank you in advance for any help.

    Best regards,
    Morten

    And apologies for not using -dataex-. I tried but as a novice mind I was not able to create an appropriate example that could present the above question in a simple and concise manner. I will make myself more acquainted with the function for possible future questions.


  • #2
    I don't understand why the dataex command (not function) is difficult here. But your data example is helpful.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte id int time byte(treatment newvar)
    1 2005 0 -1
    1 2006 1  0
    1 2007 1  1
    1 2008 1  2
    2 2005 0 -2
    2 2006 0 -1
    2 2007 1  0
    2 2008 1  1
    3 2005 0 -3
    3 2006 0 -2
    3 2007 0 -1
    3 2008 1  0
    end
    
    egen first = min(time / treatment) , by(id)
    
    gen wanted = time - first
    
     l, sepby(id)
    
         +------------------------------------------------+
         | id   time   treatm~t   newvar   first   wanted |
         |------------------------------------------------|
      1. |  1   2005          0       -1    2006       -1 |
      2. |  1   2006          1        0    2006        0 |
      3. |  1   2007          1        1    2006        1 |
      4. |  1   2008          1        2    2006        2 |
         |------------------------------------------------|
      5. |  2   2005          0       -2    2007       -2 |
      6. |  2   2006          0       -1    2007       -1 |
      7. |  2   2007          1        0    2007        0 |
      8. |  2   2008          1        1    2007        1 |
         |------------------------------------------------|
      9. |  3   2005          0       -3    2008       -3 |
     10. |  3   2006          0       -2    2008       -2 |
     11. |  3   2007          0       -1    2008       -1 |
     12. |  3   2008          1        0    2008        0 |
         +------------------------------------------------+
    .

    The trickiest part here is getting the first dates in a variable, but that is documented, e.g. http://www.stata.com/support/faqs/da...t-occurrences/ although I used the trick in http://www.stata-journal.com/sjpdf.h...iclenum=dm0055 #`10.

    Comment

    Working...
    X