Announcement

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

  • Delete panel data observation if price i below certain threshold

    Hi,

    I'm working with a panel data set that consists of daily stock prices. I would like to create a dummy variable that is equal to 1 if the price of that specific stock was below 1 USD on any day within the last year. Finally, I want to delete that stock from my data set.

    Could anyone help me with this?

    Thanks in advance!

    Peter


  • #2
    On the assumption that your data is laid out long and that your date variable is a bona fide Stata internal format daily date variable, and that by "within the last year" you mean within the past 365 days, including the current date (as opposed to just being in the same calendar year):
    Code:
    rangestat (min) low = price, by(stock_id) interval(date -364 0)
    by stock_id (low), sort: drop if low[1] < 1
    sort stock_id date
    -rangestat- is written by Robert Picard, Nick Cox, and Roberto Ferrer. It is available from SSC.

    By the way, since you are deleting the entire stock from your data set if it ever prices below 1, the "variable" you ask to create will always be zero in the surviving data. So it isn't really useful at that point. I have, for that reason, not created the variable. It would serve no purpose. I have left behind the variable low which shows the lowest price the stock has attained in the last year. If you have no other need for it, you might as well just drop it.

    In the future, when asking for help with code, please show example data. The code I have written relies on many assumptions about the contents and organization of your data set that cannot be discerned from your, or, for that matter, from any description. While there is a good chance I have guessed right, if I have guessed wrong, then we have both wasted our time.

    When showing data examples, please 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-.
    Last edited by Clyde Schechter; 14 Feb 2023, 16:55.

    Comment

    Working...
    X