Announcement

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

  • create dummy with missing values

    Hello!

    I am trying to create a dummy that equals one whenever one of two chosen dummies equals one, no matter if one of them is missing. However, I want to avoid stata to generate a zero if both are missing, because in that case I want to return a missing value.

    for now, I did it like this:

    Code:
    generate SME2 = SME2011 == 1 | SME2012 == 1
    however, stata returns a zero even if both values are missing. If I add the condition if SME2011 != . & SME2012 != . stata returns a missing value from the moment one of them is missing, but I only want to return a . if both are missing.

    kind regards,
    Timea

  • #2

    Code:
     if SME2011 != . & SME2012 != .
    does indeed not do what you want. The reason is that being equal to 0 or 1 qualifies as being not missing.

    Here are two ways that work.

    Code:
    clear 
    input sme2011 sme2012 
    1 1 
    1 0 
    0 1 
    0 0 
    1 . 
    0 . 
    . . 
    end 
    
    gen wanted = cond(missing(sme2011) & missing(sme2012), ., sme2011 == 1 | sme2012 == 1)
    
    gen wanted2 = max(sme2011, sme2012) 
    
     list, sep(0)
    
         +--------------------------------------+
         | sme2011   sme2012   wanted   wanted2 |
         |--------------------------------------|
      1. |       1         1        1         1 |
      2. |       1         0        1         1 |
      3. |       0         1        1         1 |
      4. |       0         0        0         0 |
      5. |       1         .        1         1 |
      6. |       0         .        0         0 |
      7. |       .         .        .         . |
         +--------------------------------------+

    Comment


    • #3
      thank you very much!

      Comment

      Working...
      X