Announcement

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

  • Sum binary variables

    Hello all,

    My problem seems simple but I can't find a way to solve it.

    I want to create an new variable (index) being the sum of 6 variables : chom_i nodip_i surocc_i fmonop_i imm_i pauv_i
    These variables refer to binary concepts and are coded as such : 1 = yes; 2 = no; 9 = missing data.
    I want index to sum the value of the 6 variables only when 1 or 0, never when 9 so that the range of index remains between 0 and 6. When it's 9 it must add 0.
    I have tried several ways but can't find a good solution..

    Thanks in advance for much needed help )

    Sybille

  • #2
    here is an way in two steps:
    Code:
    mvdecode chom_i nodip_i surocc_i fmonop_i imm_i pauv_i,  mv(9=.)
    egen wanted=rowtotal(chom_i nodip_i surocc_i fmonop_i imm_i pauv_i_), missing
    warning - since no data example was given (please read the FAQ), there may be typos or other errors in the code

    Comment


    • #3
      Your values are stated to be 1, 2, 9 and then later 0, 1, 9. Whichever it is,

      Code:
      egen wanted = anycount(chom_i nodip_i surocc_i fmonop_i imm_i pauv_i), values(1)
      will count the number of 1s in an observation, which is precisely the same as their sum.

      Comment


      • #4
        Thank you both for your answers and time!

        It is 0; 1; 9 sorry for the mistake

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str9 IRIS float(nbcas_cat chom_i nodip_i surocc_i fmonop_i imm_i pauv_i)
        "751051704" 0 0 0 0 0 0 0
        "751051705" 0 0 1 0 0 0 0
        "751051706" 0 9 9 9 1 1 .
        "751051799" 0 9 1 9 9 9 .
        "751051801" 1 0 0 0 0 0 9
        Sorry I was aware of the advice of giving data example but had a hard time doing despite consulting help and videos

        Comment

        Working...
        X