Announcement

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

  • Dropping all observations from a subgroup if a variable of only or more of them is negative

    Hi there,

    I'm trying to drop all observations from a subgroup if a variable of only or more of them is negative.
    Code:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long(hhnum permno) float edate str9 date float(quantityb priceb quantitys prices inv)
      629 19561 11820 "12may1992"  300   43.75    .      .  300
      629 19561 13059 "03oct1995"    .       .  300   66.5    0
      629 40707 11611 "16oct1991"  300    40.5    .      .  300
      629 40707 12387 "30nov1993"  300  26.375    .      .  600
      629 40707 12417 "30dec1993"    .       .  300 26.375  300
      629 40707 12737 "15nov1994"    .       .  300 24.125    0
      629 43916 13075 "19oct1995"  100    52.5    .      .  100
      629 43916 13333 "03jul1996"    .       .  100     39    0
      629 51692 12093 "09feb1993"  500   11.25    .      .  500
      629 51692 12198 "25may1993"    .       .  500  8.625    0
      629 55976 11458 "16may1991"  200 41.0625    .      .  200
      629 55976 11550 "16aug1991"    .       .  200  47.25    0
      629 55976 13268 "29apr1996"  200    23.5    .      .  200
      629 56223 13275 "06may1996"  200  27.375    .      .  200
      629 57795 11806 "28apr1992"  800    12.5    .      .  800
      629 57795 12015 "23nov1992"  800   7.375    .      . 1600
      629 57795 12052 "30dec1992"    .       .  800      7  800
      629 57795 12540 "02may1994"    .       .  800   .375    0
      629 62148 11753 "06mar1992"  200    57.5    .      .  200
      629 62148 11819 "11may1992"    .       .  200     66    0
      629 65568 12501 "24mar1994"  300  13.125    .      .  300
      629 66069 11491 "18jun1991" 1000   9.375    .      . 1000
      629 66069 11826 "18may1992"    .       .  500 12.625  500
      629 66069 12079 "26jan1993"    .       .  500 14.625    0
      629 69489 12737 "15nov1994"  300  10.875    .      .  300
      629 76129 12211 "07jun1993"  200  26.375    .      .  200
      629 76129 12220 "16jun1993"    .       .  200  22.25    0
      629 77064 12205 "01jun1993"  300  16.375    .      .  300
      629 77064 12282 "17aug1993"    .       .  300 14.375    0
      629 78203 12222 "18jun1993"  100      42    .      .  100
      629 78203 12492 "15mar1994"    .       .  200  22.75 -100
      629 78203 12715 "24oct1994"  300    15.5    .      .  200
      629 79856 12246 "12jul1993" 1000  3.8125    .      . 1000
     6664 11749 12303 "07sep1993"  800     2.5    .      .  800
     6664 11749 12436 "18jan1994"    .       .  800 3.4375    0
     6664 76351 13200 "21feb1996" 4000    .625    .      . 4000
     6664 76809 12269 "04aug1993"  500     5.5    .      .  500
     6664 76809 12758 "06dec1994"    .       .  500 3.0625    0
     6664 87127 12390 "03dec1993"  200   23.25    .      .  200
     7980 10775 12214 "10jun1993" 1000   6.375    .      . 1000
     7980 10775 12395 "08dec1993"    .       . 1000   6.25    0
     7980 75607 12312 "16sep1993"  300    17.5    .      .  300
     7980 75607 13181 "02feb1996"  300  13.125    .      .  600
    Basically, the data tracks stock purchases and sells by households. I'm tracking the running inventory in inv, but sometimes that one turns negative (which means stocks are bought before the period). If a stock has negative inventory, all purchases and sells from that stock by that specific investor should be dropped. Any ideas?

    Many thanks in advance!

  • #2
    I'm assuming you meant to write "if a variable of *one* or more of them... " You forgot to tell us what variable(s) define a subgroup, so the following might be wrong on that basis, but this will show you some technique:
    Code:
    egen min_of_inv = min(inv), by(permno) // guessing that permno defines subgroup
    drop if min_of_inv < 0

    Comment


    • #3
      Code:
      bysort permno (inv) : drop if inv[1] < 0
      is another way to do this.

      Comment

      Working...
      X