Announcement

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

  • Drop if in range

    Hello,

    how can I drop a group if there is a missing value in any variable per group. In the example blow, my group is identified by "id". So, I would like to delete all 1, because there is one missing value for this group. Another condition is that it should only be excluded if the missing value for price is within the range (x,y) of var5.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str10 date float(Price dividend id var5)
    "01/01/2006" 5 . 1 -3
    "02/01/2006" 3 . 1 -2
    "03/01/2006" . 3 1 -1
    "04/01/2006" 5 . 1  0
    "05/01/2006" 2 . 1  1
    "01/01/2006" 5 . 2 -2
    "02/01/2006" 4 . 2 -1
    "03/01/2006" 2 . 2  0
    "04/01/2006" 1 2 2  1
    "05/01/2006" 4 . 2  2
    end
    Thank you!

  • #2
    If the price is missing (aka, there isn't a number), how could you tell if that number is within the range of var5? And what are x and y? Min, max, quartile, etc.?

    Comment


    • #3
      I mean, you could ask whether there is a missing value within the range of x y in var5. X could be -1 and y -3 in this case. The range of var5 does not depend on the Price. It depends on the date

      Comment


      • #4
        Something like this? I'm still not sure how -3 and -1 are considered. But you can change that range as you see fit:
        Code:
        gen flag = Price == . & inrange(var5, -3, -1)
        egen todrop = max(flag), by(id)
        drop if todrop == 1
        drop flag todrop

        Comment


        • #5
          Hi Ken,

          this is exactly, what I was looking for. Thank you very much.

          Comment


          • #6
            @Ken Chui's helpful code can be simplified a little:

            Code:
            egen todrop = max(Price == . & inrange(var5, -3, -1)), by(id)
            drop if todrop 
            drop todrop

            Comment

            Working...
            X