Announcement

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

  • Drop variables with common suffix under certain conditions

    Dear all,

    I am facing difficulties in dropping some variables conditional on the missing observations of one variable.
    I have a dataset of prices, volumes and a measure of liquidity for 200 stocks. The dataset is a time series made of 601 variables, date, p1-p200, vo1-vo200 and liq1-liq200.
    I want to drop p*, vo* and liq* if liq* has less than 30 observations.
    I have tried
    Code:
    foreach var of varlist * {   
        qui count if missing(`var')     
        if r(N) >= 7535 drop `var'   
    }
    But there is a big problem with this approach. If let's say p1 has less than 30 but not vo1 than it drops p1 and keeps vo1.

    Any suggestion?

    Best

    Stefano

  • #2
    Code:
    forval j = 1/200 {
           count if missing(liq`j')
           if r(N) >= 7535 drop p`j' vo`j' liq`j'
    }
    or

    Code:
    count if missing(p`j', vo`j', liq`j')

    Comment


    • #3
      Thank you very much Nick, I was kind of sure you'd have replied .

      Best

      Stefano

      Comment


      • #4
        Dear all, I am also facing difficulties in dropping observations of some 300 variables and 140 of them have a common suffix. All these 140 variables end by _xt and I want to drop observations for all the 300 variables if at least one the 140 variables ending by _xt has a value <= 5. The following command does not work : drop if *_xt <=5 . Thank you in advance!

        Comment

        Working...
        X