Announcement

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

  • dropping observation with a con

    Hello all,
    I would like to drop certain observations from my database and I am not sure how to do so.
    My data consists of stock' ticker, dates, additional date variable, and a binary variable which equals 1 if the additional date variable is equal to the date under each ticker

    for example:
    Name Date date1 binary
    apple 01jan2010 . 0
    apple 02jan2010 02jan2010 1
    apple 03jan2010 . 0
    google 01jan2010 . 0
    google 02jan2010 . 0
    google 03jan2010 03jan2010 1
    tesla 01jan2010 . 0
    tesla 02jan2010 . 0
    tesla 03jan2010 . 0

    I would like to drop every stock that has no date1 (or binary=1) observations, so in the case of my example, that would mean drop all the observations of tesla.
    Note: I would like to keep the observations of the other stocks, even though some of them do not coincide with date1.
    additional note: 1 observation is enough to keep the stock in the dataset.

    result:
    Name Date date1 binary
    apple 01jan2010 . 0
    apple 02jan2010 02jan2010 1
    apple 03jan2010 . 0
    google 01jan2010 . 0
    google 02jan2010 . 0
    google 03jan2010 03jan2010 1

    Thank you all!



  • #2
    Code:
    bysort name (binary): drop if binary[_N]==0

    Comment


    • #3
      Code:
      bys Name: drop if sum(binary) <1

      Comment


      • #4
        Originally posted by Ali Atia View Post
        Code:
        bysort name (binary): drop if binary[_N]==0
        Thank you for the reply, for some reason, the command drops additional observations (for example a row of apple)

        The same goes for Andrew's command.
        also, each of the two commands drops a different number of observations (which should not happen I believe?)

        Comment


        • #5
          You probably have some leading or trailing spaces in your names so that Stata does not recognize that the dropped observation is part of a particular group which should not be dropped. Also, there is an assumption that you do not have missing values, although this should not cause a difference between my code and Ali's. In any case, you need to present a data example using dataex that replicates your issue.

          Comment

          Working...
          X