Announcement

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

  • Creating event window with loops

    Dear users,

    I am performing an event study and would need you help.
    Imagine the following panel data, where the variable "event" is a dummy variable (1= earnings announcement, 0=no earnings announcement)
    date event
    01 February 2018 0
    02 February 2018 0
    05 February 2018 0
    06 February 2018 1
    07 February 2018 0
    08 February 2018 0
    09 February 2018 0
    12 February 2018 0
    13 February 2018 0
    14 February 2018 0
    15 February 2018 1
    16 February 2018 0
    19 February 2018 0
    20 February 2018 0


    I would like to create a new variable called "event_2" that will look like this: (It will give values 1 to n=+2, n=+1, n, n=-1 and n=-2)
    date event event_2
    01 February 2018 0 0
    02 February 2018 0 1
    05 February 2018 0 1
    06 February 2018 1 1
    07 February 2018 0 1
    08 February 2018 0 1
    09 February 2018 0 0
    12 February 2018 0 0
    13 February 2018 0 1
    14 February 2018 0 1
    15 February 2018 1 1
    16 February 2018 0 1
    19 February 2018 0 1
    20 February 2018 0 0

    I know there is the possibility to do it with the following function:

    gen event_2=0
    replace event_2 = 1 if event[_n+2] == 1
    replace event_2 = 1 if event[_n+1] == 1
    replace event_2 = 1 if event[_n] == 1
    replace event_2 = 1 if event[_n-1] == 1
    replace event_2 = 1 if event[_n-2] == 1

    But as I am planning to consider bigger event windows (-30,+30) I have heard that it can be done much easier with loops, does anyone know how to do it?


    Thanks in advance.


  • #2
    Please do read and act on FAQ Advice #12

    https://www.statalist.org/forums/help#stata

    in posting data examples. Yours is helpful but required some surgery before it could be used to do something useful.

    An important detail here is that your dates exclude weekends. The best solution for that is to implement a business calendar in Stata. As a quick work-around, I use a time variable that is just observation number.

    rangestat (SSC) may be used for this purpose. You must install it before you can use it. Search the forum for other examples using rangestat as a search term.

    The syntax is easy to modify for longer windows. No loops needed: you need to consult a better class of expert!


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(event date)
    0 21216
    0 21217
    0 21220
    1 21221
    0 21222
    0 21223
    0 21224
    0 21227
    0 21228
    0 21229
    1 21230
    0 21231
    0 21234
    0 21235
    end
    format %td date
    
    gen t = _n
    
    rangestat (max) event, int(t -2 2)
    
    list date t event*, sepby(event_max)
    
         +-----------------------------------+
         |      date    t   event   event_~x |
         |-----------------------------------|
      1. | 01feb2018    1       0          0 |
         |-----------------------------------|
      2. | 02feb2018    2       0          1 |
      3. | 05feb2018    3       0          1 |
      4. | 06feb2018    4       1          1 |
      5. | 07feb2018    5       0          1 |
      6. | 08feb2018    6       0          1 |
         |-----------------------------------|
      7. | 09feb2018    7       0          0 |
      8. | 12feb2018    8       0          0 |
         |-----------------------------------|
      9. | 13feb2018    9       0          1 |
     10. | 14feb2018   10       0          1 |
     11. | 15feb2018   11       1          1 |
     12. | 16feb2018   12       0          1 |
     13. | 19feb2018   13       0          1 |
         |-----------------------------------|
     14. | 20feb2018   14       0          0 |
         +-----------------------------------+

    Comment


    • #3
      I think it should be something like this.
      Code:
      bysort event: egen event_2=event+1
      Trying it doesn't give me the desired output, but take it like an hint.
      As I'll have more time, I'll help you with your issue if no one can help you!

      Best,

      Antonio


      EDIT: I saw that Nick already gave a perfect answer!

      Comment


      • #4
        Antonio: That is not even legal, as egen needs a function call.

        As you report trying it, I have to guess that you tried something else. If you wrote gen instead, it's legal, but still wrong as it just bumps 0 to 1 and 1 to 2 and does nothing in terms of windows.

        Comment

        Working...
        X