Announcement

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

  • Egen rowtotal only complete cases

    Hi, say I want to create a summed var like so:

    Code:
    egen index = rowtotal(var1 var2 var3 var4), miss
    This will tell Stata that if an observation has all missing values for var1 - var4, then the index var will = . as well. But is there an option for telling Stata I want index to =. if any of var1 - var4 == . ? That is, I only want a non-missing value for index for complete cases. Thanks.


  • #2
    The direct sum

    Code:
    var1 + var2  + var3 + var4
    will be missing if any component is.

    Orherwise, write your own loop OR use egen to calculate the number of missing values and replace with missing if there are any such.
    Last edited by Nick Cox; 02 Nov 2022, 07:16.

    Comment


    • #3
      Suppose you know that you want to loop over several variables whose names start with var. That could be done with

      Code:
      gen rowtotal = 0 
      
      foreach v of var var* { 
          replace rowtotal = rowtotal + `v'
      }
      and then missing in any individual variable will mean missing result in that observation (row of dataset, if you will).

      If var* captures too much, you need a different wildcard or a different set of variable names that is more informative.

      Comment

      Working...
      X