Announcement

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

  • More concise way to use missing() function

    The only way I can see to jointly test whether a list of variables are all missing is to include each variable in a separate missing() function, which can be pretty verbose,
    Code:
    gen x = 1 if mi(var1) & mi(var2) & mi(var3) & mi(var4) & mi(var5) & mi(var6) & mi(var7)
    I know I could easily assess whether at least one variable is missing,
    Code:
    gen x = 1 if mi(var1, var2, var3, var4, var5, var6, var7)
    It is also easy to test whether zero variables are missing,
    Code:
    gen x = 1 if ~mi(var1, var2, var3, var4, var5, var6, var7)
    What I want to know is whether there is a more concise way to create the same effect as the first code block? As previous posts have pointed out, there is no way to create a user-defined function like missing() since it is part of the executable, but I was curious if someone has devised any other method to accomplish this task, ideally in a list-like fashion as in the second and third code block.

  • #2
    egen allows you to count non-missings across rows.

    Code:
    egen nonmiss = rownonmiss(var?)
    At some point one prefers a loop any way.

    Code:
    gen nonmissing = 0 
    
    quietly forval j = 1/7 { 
          replace nonmissing = nonmissing + !missing(var`j') 
    }
    In either case the result is zero if and only if all variables are missing. Add strok if string variables are included.

    Comment

    Working...
    X