Announcement

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

  • Keep observations if a variable equals 1 for an entire date range

    Hey all,
    I want to do an event study for a variable (price) over a particular interval of time. However, I only want to do this for locations where some variable X=1 for the entirety of a date range (for example, from March to May of a particular year).
    How do I perform this?

    I feel like this context should be sufficient, but if not I can expand further..

    Best,
    Josh

  • #2
    The context is mostly clear, but you need to give the actual specific date range you want the code to use. There is no Stata code for "March to May of a particular year."

    More problematic, you have provided no example data, so we are forced to imagine how your data set is organized. Writing code for imaginary data sets is usually waste of my time, and it wastes yours too because it is likely not to work and then you have to post back with clarifications and wait for another response. So please post back showing example data, and use the -dataex- command to do so. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.

    Comment


    • #3
      No Problem.

      Here is my data example:

      -dataex-

      input int(location date) byte Firms
      227 20172 1
      227 20156 1
      227 20171 1
      227 20177 1
      227 20178 1
      227 20174 1
      227 20159 1
      227 20175 1
      227 20163 1

      There are 100 locations, the date is a daily date over a 3 year period. Firms can be from one to 20.
      I want the to look at prices for a range that corresponds to march 2015 to May 2015, but only if firms=1 for the entire range.

      Comment


      • #4
        Additionally, I figure it is easier to just keep the observations I want, then run the sepscatter code I already have, which is why I did not include the price variable in the dataex example. If you think there is a better way, I can add price to dataex, and give any additional context or code. thank you for your tutelage, Clyde!

        Comment


        • #5
          I think your approach is fine. To be clear, if a location as Firms == 1 for every observation dated between 1 March 2015 and 31 May 2015 (inclusive), all of that location's observations (even outside that date range) will be kept. If any observation dated between 1 March 2015 and 31 May 2015 (inclusive) is not 1, then all of that location's observations are dropped.

          Code:
          by location (date), sort: egen byte keeper = ///
              min(cond(inrange(date, td(1mar2015), td(31may2015)), Firms == 1, 1))

          Comment


          • #6
            Thank you Clyde! That worked well. I definitely wasn't sure if I could embed those functions within each other like that. I appreciate your help!

            Comment

            Working...
            X