Announcement

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

  • egen rowtotal if not missing

    I want to get a row total of nine variables if none is missing. Is my code correct?
    egen phq_total=rowtotal(phq1 - phq9) if !missing(phq1 | phq2 | phq3 | phq4 | phq5 | phq6 | phq7 | phq8 | phq9)

  • #2
    No. It's not correct. Consider just phq1 | phq2 which will be returned as 1 or true is either variable is missing (because missing is not zero) and as 0 or false if neither is missing. By an extension of the same argument what you feed to !missing() will be either 1 or 0 and it can never be missing. If you change the pipes to commas, I think you are where you want to be.

    However, I'd go

    Code:
    egen phq_miss = rowmiss(phq?)
    
    egen phq_total = rowtotal(phq?) if phq_miss == 0
    as I would trade off creating a new variable against less typing and a less error-prone code.


    All that said

    Code:
    gen wanted = phq1 + phq2 + phq3 + phq4 + phq5 + phq6 + phq7 + phq8 + phq9
    will be missing if any of the variables on the right is missing, which is what you are asking for.
    Last edited by Nick Cox; 18 Oct 2021, 10:00.

    Comment


    • #3
      Thanks, Nick. I used the simple option:
      Code:
       
       gen wanted = phq1 + phq2 + phq3 + phq4 + phq5 + phq6 + phq7 + phq8 + phq9

      Comment

      Working...
      X