Announcement

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

  • Multiple forms

    Hi everyone,
    Am doing a study and my outcome variable is Multiple forms of undernutrition. I have calculated my z scores and gotten variables for stunting, wasting, underweight, and anemia. I need to generate a variable ( Coexisted) that will mean a child that experiences at least two or more of the forms of undernutrition concurrently so I can continue with my analysis.
    I tried this syntax but it didn't work
    //generate Coexisted=1. if stunting=1|wasting=1|underweight=1|anemia=1
    replace Coexisted=0 if stunting=0|wasting=0|underweight=0|anemia=0
    replace Coexisted=. if stunting==.|wasting==.|underweight==.|anemia=.
    label define 1 “Coexisted “ 0 “Not Coexisted”, replace
    label values Coexisted Coexisted //

  • #2
    The code does three main things.

    Code:
    generate Coexisted=1. if stunting=1|wasting=1|underweight=1|anemia=1
    
    replace Coexisted=0 if stunting=0|wasting=0|underweight=0|anemia=0
    
    replace Coexisted=. if stunting==.|wasting==.|underweight==.|anemia=.
    The new variable is 1 if any of four variables is 1, but then overwritten by 0 if any is 0, but then overwritten by missing if any is missing.

    Again: Watch the way the voting works. A "vote" of missing anywhere overwrites a vote of 0 anywhere and one of 0 overwrites a vote of 1 anywhere.

    Nothing in the code counts instances of 1. You can do that directly.

    Code:
    egen wanted = rowtotal(stunting wasting underweight anemia)
    adds rowwise, ignoring missings. Then

    Code:
    gen Coexisted = wanted >= 2
    identifies observations with counts of 2, 3 or 4.

    See also https://www.stata.com/support/faqs/d...ummy-variables

    https://www.stata.com/support/faqs/d...rue-and-false/
    Last edited by Nick Cox; 05 Feb 2022, 05:28.

    Comment


    • #3
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(id stunting wasting underweight anemia)
      1 1 0 1 0
      2 0 0 0 1
      3 . 0 1 .
      end
      
      egen forms = rowtotal(stunting wasting underweight anemia)
      gen wanted = forms>1
      Last edited by Øyvind Snilsberg; 05 Feb 2022, 04:57. Reason: crossed with #2

      Comment


      • #4
        Thank you works perfectly now

        Comment


        • #5
          This is for any future readers. The code in #1 has expressions like this

          Code:
          stunting=1|wasting=1|underweight=1|anemia=1
          where each equals sign should be doubled

          Code:
          stunting==1|wasting==1|underweight==1|anemia==1
          My bad for not noticing this in #2. but that oversight doesn't undermine the suggestion there.

          Comment

          Working...
          X