Announcement

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

  • event study

    Hello dear members,

    I want to calculate difference between target_date and event_date . If I write
    gen dif=target_date-event_date
    I get only dif=0 in the case if event_date=328. But I need dif for all cases, for example, dif=315-328=-13, dif=316-328=-12, dif=317-328=-11,.... How I can do it?
    I'm really thankful for your help.

    Click image for larger version

Name:	Clipboard01.jpg
Views:	1
Size:	70.2 KB
ID:	195461

  • #2
    It is hard to read a screen shot. As point 12 of the FAQ says,

    Stata code (i.e. the exact commands issued) is easier to read if presented as such. Click on the Editor's toggle to Advanced settings (an underlined A) and click on the # button to insert
    Code:
     and
    mark-up. Write your code between, paying particular attention to linebreaks and indentation. Or just insert those mark-ups manually before, or indeed after, you insert your code.
    It is hard to see the problem. In the part you posted, only one case has a value for event date. Why don't the others? You are only going to get a nonmissing value for dif when both target date and event date have misisng values.

    Also odd is that one of your cases has a value of 3.27 for day_cnt; I assume it should be 327.

    Anyway, it looks like there is something wrong with your data.

    -------------------------------------------
    Richard Williams, Notre Dame Dept of Sociology
    Stata Version: 17.0 MP (2 processor)

    EMAIL: [email protected]
    WWW: https://www3.nd.edu/~rwilliam

    Comment


    • #3
      Thank you for your advice. I have multiple companies and multiple events for each company. I inserted a little cut-out from my results. I want to calculate the difference between target_date and event_date . To do this, I created a variable dy_cnt
      Code:
      by id: gen day_cnt =_n
      A variable "id" numbers the events for each company from 1 to however many there are. The screenshot is presented for one event, i.e for on eid.
      Also I created a variable event_date
      Code:
      gen event_day = day_cnt if _date==_event_date
      and a variable target_date
      Code:
      gen target_day = day_cnt if _date-_event_date>-20 & _date-_event_date<20

      Now I need a commando, that calculates the difference between
      target_date and event_date ( in this case, dif=315-328=-13, dif=316-328=-12, dif=317-328=-11).
      With commando
      Code:
      gen dif=target_date-event_date if target_date~=.

      I get only dif=0

      Comment


      • #4
        I am not totally sure what you want to do, but I doubt that this is the way to do it.

        Code:
         by id: gen day_cnt =_n
        My guess is that you want something more like

        Code:
        bysort id (date): gen day_cnt =_n
        Otherwise things can be randomly sorted within id, e.g. a later date can be sorted before a later date.

        Code:
        gen event_day = day_cnt if _date==_event_date]
        The only time this will be true is when _date = _event_date. In the data you presented, the only time this occurred was for a record that had October 12, 2010 in it. Do you really want event_day to be missing for all other cases?

        Further, when you do your differencing, the only time you have nonmissing values is when date and event_date are one and the same, so of course the difference is zero.

        I think you need to rethink how you want to code event_day.

        I still don't understand how you got a value of 3.27 for day_cnt. I suspect you aren't showing us what you really have. Doing something like

        Code:
        list, clean noobs
        can be much easier to read. Limit it to the key variables if there are many more than you are showing us.


        -------------------------------------------
        Richard Williams, Notre Dame Dept of Sociology
        Stata Version: 17.0 MP (2 processor)

        EMAIL: [email protected]
        WWW: https://www3.nd.edu/~rwilliam

        Comment


        • #5
          I'm going to go out on a limb here and guess that Svetlana actually wants to set event day to equal the day_cnt value where _date == _event_date, and that she wants that same value to be used in every observation for the same company, not just the one observation where _date = _event_date. If I'm write about that, the code could be:

          Code:
          // FIRST VERIFY THAT EACH COMPANY HAS ONE AND ONLY ONE DAY WHERE
          // _date == _event_date
          egen check_it = total(_date == _event_date), by(company)
          assert check_it == 1
          
          // NOW TRANSMIT THE VALUE OF day_cnt FOR THAT COMPANY's
          // UNIQUE EVENT DATE TO A NEW VARIABLE, event_day IN ALL
          // OBSERVATIONS FOR THAT COMPANY
          egen event_day = max(day_cnt*(_date == _event_date)), by(company)
          That said, I agree very much with RIchard Williams' concerns about the way you have generated the day_cnt variable, because it appears to depend on a random sorting of the data. In fact, I don't really understand why you don't just transform date and event_date to real Stata dates (I gather what you have there are string variables) and work with those directly. The whole problem would appear to then be very simple:

          Code:
          gen long date_st = date(date, "MDY")
          gen long event_date_st = date(event_date, "MDY")
          format date_st event_date_st %td // THIS LINE IS OPTIONAL
          gen dif = date_st - event_date_st
          // AND I DON'T QUITE UNDERSTAND WHAT WAS GOING ON WITH target_date
          // IN YOUR ORIGINAL POST, BUT MAYBE YOU WANT TO RESTRICT HAVING
          // VALUES OF dif TO 20 DAYS BEFORE AND AFTER THE EVENET DATE.
          // IF THAT'S CORRECT DO THE FOLLOWING:
          replace dif = . if !inrange(dif, -20, 20)
          Hope this helps. If I have misunderstood what you are trying to do, it would help if you would work out some cases by hand and then post some data showing us what the correct result would look like.
          Last edited by Clyde Schechter; 31 Aug 2014, 14:14. Reason: Edit in an omitted line of code

          Comment

          Working...
          X