Announcement

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

  • Dropping many variables if all observations contain a certain text

    Dear All,
    I am having trouble with a command in Stata and no matter how much I researched beforehand, I could not find a solution. I have a dataset that contains cross sectional data. There are around 1500 variables, where many of them contain from observation 1 to observation 29000 the value -1, i.e. ''missing''. I am trying to create a command that tells Stata that if a variable has only the value -1 from the first observation until the last one, then it should drop this variable and this should be done for all the variables that have this problem. Does anyone know how to proceed?

    I thank you all for your time and your opinion.
    Best.
    Lola

  • #2
    This is not tested

    Code:
    foreach var of varlist * {
        summarize `var' , meanonly
        if (r(min) != -1) continue
        if (r(max) == -1) drop `var'
    }
    Perhaps even quicker

    Code:
    foreach var of varlist * {
        capture assert `var' == -1 , fast
        if (!_rc) drop `var'
    }
    Best
    Daniel
    Last edited by daniel klein; 04 Apr 2019, 04:13.

    Comment


    • #3
      Dear Daniel,
      It worked! Thank you a lot, you saved my day!

      All the best,
      Lola

      Comment


      • #4
        Daniel Klein's solution is excellent. Also with findname (Stata Jourmal)

        Code:
        findname, all(@ == -1)
        would find all such variables after which

        Code:
        drop `r(varlist)'
        would clear them out of the way.

        Comment


        • #5
          Dear Nick,
          Thank you for the suggestion. I will try it as well.

          All the best,
          Lola

          Comment

          Working...
          X