Announcement

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

  • Drop if ALL specified variables fulfill a certain condition

    Dear statalist,
    is there a way to write the following command

    Code:
    drop if intention1==9 & intention2==9 & intention3==9 & intention4==9 ///
    & intention5==9 & intention6==9 & intention7==9 & intention8==9
    in a way that I do not explicitly have to write out all variables names? I have to write similar commands including many more variables. I thought about a forvalues loop but that will read as "or" and not "and".
    Thank you,
    Sarah

  • #2
    Intend to drop all observations, but reprieve them if there is a value other than 9. Otherwise put: the complement of "all values 9" is "at least one value is not 9".

    Code:
    gen todrop = 1
    
    forval j = 1/9 {
          replace todrop = 0 if intention`j' != 9
    }
    
    drop if todrop
    That could be a loop over variables, not suffixes

    Code:
    foreach v of var intention* {
          replace todrop = 0 if `v' != 9
    }

    Comment


    • #3
      Thank you, that works perfectly.

      Comment


      • #4
        For scenarios like this I wrote the package -multif- which you can install via
        Code:
        ssc install multif
        and then run
        Code:
        multif  intention*, condition(VAR !=9 ) connection(&) command(drop)

        Comment

        Working...
        X