Announcement

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

  • What is wrong with my code?

    Dear all,

    I have two variables: i) gender of household members; and ii) the relationship between those members and respondents. I want to generate a gender variable of children of respondent (either sons or daughters), I have tried to use loop but I got strange results. For example, the data says the child is a son but in some cases my code gave me a daughter. Any advice on my code is appreciated. Thank you.

    Please note that data is in a wide layout and I can transform it into a long layout, but let's leave its format as it is for now. Variable b22s-b28s is gender of 2nd-8th members in the household, b32r-b38r is the corresponding relationship of those members with the respondents.
    Data
    Code:
    clear
    input byte(b22s b23s b24s b25s b26s b27s b28s b32r b33r b34r b35r b36r b37r b38r)
    2 1 1 1 . . .  4 10 10 10  .  .  .
    1 1 1 . . . .  2  3  3  .  .  .  .
    2 1 2 1 1 . .  2  3  5 10 10  .  .
    2 2 . . . . .  4  4  .  .  .  .  .
    2 . . . . . .  2  .  .  .  .  .  .
    1 1 2 1 2 . .  2  3  5 10 10  .  .
    1 1 1 2 2 1 .  2  3  3  5 10 10  .
    1 2 2 . . . .  2 10 10  .  .  .  .
    . . . . . . .  .  .  .  .  .  .  .
    1 1 2 2 1 . .  2  3  5 10 10  .  .
    2 1 2 1 2 . .  2  3  5 10 10  .  .
    1 2 2 1 2 . .  3  5 10 10  2  .  .
    2 1 2 . . . .  5 10 10  .  .  .  .
    . . . . . . .  .  .  .  .  .  .  .
    2 1 . . . . .  2 10  .  .  .  .  .
    2 1 2 1 2 . .  2  3  4  5 10  .  .
    2 1 . . . . .  2  3  .  .  .  .  .
    1 1 1 2 1 . .  2  3  3  5 10  .  .
    1 2 . . . . .  2 10  .  .  .  .  .
    2 2 . . . . .  2 10  .  .  .  .  .
    1 . . . . . .  2  .  .  .  .  .  .
    2 . . . . . .  2  .  .  .  .  .  .
    1 2 1 1 . . .  2  5  3 10  .  .  .
    2 1 . . . . .  2 10  .  .  .  .  .
    1 1 2 1 2 . .  2  3  5 10 10  .  .
    . . . . . . .  .  .  .  .  .  .  .
    . . . . . . .  .  .  .  .  .  .  .
    2 . . . . . .  2  .  .  .  .  .  .
    2 1 2 . . . .  4  5 10  .  .  .  .
    1 1 2 1 2 2 1  2  3  5  3  5 10 10
    1 1 2 1 1 1 .  2  3  5  3 10 10  .
    2 1 . . . . .  2  3  .  .  .  .  .
    1 2 . . . . .  2  4  .  .  .  .  .
    1 1 2 1 . . .  2  3  5 10  .  .  .
    1 1 . . . . .  2  3  .  .  .  .  .
    2 1 2 1 1 2 .  2  3  4  5 10 10  .
    1 2 1 1 1 1 .  3  5  3  3 10 10  .
    2 1 . . . . .  4  5  .  .  .  .  .
    1 2 1 . . . .  3  5 10  .  .  .  .
    1 1 . . . . .  2  3  .  .  .  .  .
    1 2 1 1 . . .  3  5 10 10  .  .  .
    2 2 . . . . .  2  4  .  .  .  .  .
    2 1 . . . . .  2  3  .  .  .  .  .
    . . . . . . .  .  .  .  .  .  .  .
    2 2 1 2 1 . .  2  7  3  5 10  .  .
    2 1 1 2 1 1 1  2  3  3  5 10 10 10
    1 1 . . . . .  2  3  .  .  .  .  .
    1 1 2 1 2 . .  2  3  5 10 10  .  .
    1 . . . . . .  2  .  .  .  .  .  .
    2 . . . . . .  2  .  .  .  .  .  .
    1 2 2 2 . . .  3  5 10 10  .  .  .
    2 1 2 1 1 1 .  2  3  5  3 10 10  .
    1 2 1 1 . . .  3  5 10 10  .  .  .
    1 1 2 2 2 . .  2  3  5 10 10  .  .
    1 1 . . . . . 10 10  .  .  .  .  .
    1 1 2 1 2 2 1  2  3  5 10 10 10 10
    2 1 2 1 2 1 .  2  3  4  3  4  3  .
    2 2 2 1 . . .  2  5 10 10  .  .  .
    1 2 1 2 2 . .  3  5 10 10 10  .  .
    1 2 1 1 . . .  3  5 10 10  .  .  .
    2 1 2 2 1 . .  4  3  5 10 10  .  .
    . . . . . . .  .  .  .  .  .  .  .
    1 1 2 1 . . .  2  3  5 10  .  .  .
    1 1 2 1 2 2 2  2  3  5  3  5 10 10
    2 1 1 2 2 1 1  4  5  3  5 10 10 10
    2 1 2 . . . .  2  3  4  .  .  .  .
    2 2 1 2 2 1 2  2  4  5 10  4  5 10
    1 1 2 1 2 . .  2  3  4 10 10  .  .
    2 1 2 1 . . .  2  3  5 10  .  .  .
    1 2 . . . . .  2  4  .  .  .  .  .
    1 1 2 1 1 1 2  2  3  5 10 10  3  5
    2 2 2 . . . .  2  4  4  .  .  .  .
    2 1 2 . . . .  2  3  5  .  .  .  .
    1 2 2 . . . .  2  4  4  .  .  .  .
    2 1 . . . . .  2  3  .  .  .  .  .
    2 1 2 . . . .  2  3 10  .  .  .  .
    2 . . . . . .  2  .  .  .  .  .  .
    1 1 2 1 2 . .  2  3  5 10 10  .  .
    1 1 2 1 1 . .  2  3  5 10 10  .  .
    2 1 . . . . .  2  3  .  .  .  .  .
    1 1 . . . . .  2  3  .  .  .  .  .
    1 2 1 . . . .  3  4 10  .  .  .  .
    2 2 1 2 . . .  4  4  3  5  .  .  .
    2 2 1 2 . . .  7 12 12 12  .  .  .
    2 . . . . . .  2  .  .  .  .  .  .
    1 2 1 2 1 . .  3  5 10 10 10  .  .
    1 2 1 . . . .  5  4 10  .  .  .  .
    1 . . . . . .  3  .  .  .  .  .  .
    2 1 . . . . .  2  3  .  .  .  .  .
    1 2 2 2 . . .  3  5 10 10  .  .  .
    2 1 . . . . .  2  3  .  .  .  .  .
    2 1 1 . . . .  2  3  3  .  .  .  .
    1 2 1 2 . . .  3  5 10 10  .  .  .
    2 1 1 . . . .  2 10 10  .  .  .  .
    1 1 . . . . .  2  3  .  .  .  .  .
    2 . . . . . .  4  .  .  .  .  .  .
    1 1 2 2 . . .  2  3  5 10  .  .  .
    2 1 . . . . .  2 11  .  .  .  .  .
    1 . . . . . .  2  .  .  .  .  .  .
    2 2 . . . . .  2  4  .  .  .  .  .
    end
    label values b22s LABEL_B22
    label def LABEL_B22 1 "Male", modify
    label def LABEL_B22 2 "Female", modify
    label values b23s LABEL_B23
    label def LABEL_B23 1 "Male", modify
    label def LABEL_B23 2 "Female", modify
    label values b24s LABEL_B24
    label def LABEL_B24 1 "Male", modify
    label def LABEL_B24 2 "Female", modify
    label values b25s LABEL_B25
    label def LABEL_B25 1 "Male", modify
    label def LABEL_B25 2 "Female", modify
    label values b26s LABEL_B26
    label def LABEL_B26 1 "Male", modify
    label def LABEL_B26 2 "Female", modify
    label values b27s LABEL_B27
    label def LABEL_B27 1 "Male", modify
    label def LABEL_B27 2 "Female", modify
    label values b28s LABEL_B28
    label def LABEL_B28 1 "Male", modify
    label def LABEL_B28 2 "Female", modify
    label values b32r LABEL_B32R
    label def LABEL_B32R 2 "Spouse", modify
    label def LABEL_B32R 3 "Son", modify
    label def LABEL_B32R 4 "Daughter", modify
    label def LABEL_B32R 5 "Son/daughter in law", modify
    label def LABEL_B32R 7 "Parent", modify
    label def LABEL_B32R 10 "Grand children", modify
    label values b33r LABEL_B33R
    label def LABEL_B33R 3 "Son", modify
    label def LABEL_B33R 4 "Daughter", modify
    label def LABEL_B33R 5 "Son/daughter in law", modify
    label def LABEL_B33R 7 "Parent", modify
    label def LABEL_B33R 10 "Grand children", modify
    label def LABEL_B33R 11 "Siblings", modify
    label def LABEL_B33R 12 "Other relatives", modify
    label values b34r LABEL_B34R
    label def LABEL_B34R 3 "Son", modify
    label def LABEL_B34R 4 "Daughter", modify
    label def LABEL_B34R 5 "Son/daughter in law", modify
    label def LABEL_B34R 10 "Grand children", modify
    label def LABEL_B34R 12 "Other relatives", modify
    label values b35r LABEL_B35R
    label def LABEL_B35R 3 "Son", modify
    label def LABEL_B35R 5 "Son/daughter in law", modify
    label def LABEL_B35R 10 "Grand children", modify
    label def LABEL_B35R 12 "Other relatives", modify
    label values b36r LABEL_B36R
    label def LABEL_B36R 2 "Spouse", modify
    label def LABEL_B36R 4 "Daughter", modify
    label def LABEL_B36R 5 "Son/daughter in law", modify
    label def LABEL_B36R 10 "Grand children", modify
    label values b37r LABEL_B37R
    label def LABEL_B37R 3 "Son", modify
    label def LABEL_B37R 5 "Son/daughter in law", modify
    label def LABEL_B37R 10 "Grand children", modify
    label values b38r LABEL_B38R
    label def LABEL_B38R 5 "Son/daughter in law", modify
    label def LABEL_B38R 10 "Grand children", modify
    Code
    Code:
    gen gender=.
    forval i = 2(1)8 {
        replace gender = b2`i's if inrange(b3`i'r, 3, 4)
    }
    *** Odd results
    * Male
    list gender b22s if b32r==3
    
    * Female
    list gender b22s if b32r==4

  • #2
    what should gender be on e.g. line 16 where b33r is son and b34r is daughter?

    Comment


    • #3
      Originally posted by Øyvind Snilsberg View Post
      what should gender be on e.g. line 16 where b33r is son and b34r is daughter?
      Thank you for your response.
      As for your question, b33r should be male because this variable correspond to b22s, which is male. Similarly, b34r is female since it corresponds to b33s. I hope this makes things clearer.

      Comment


      • #4
        Originally posted by Matthew Williams View Post
        b33r should be male because this variable correspond to b22s, which is male.
        I also know that b33r should be male because it says "son."
        I was alluding to the fact that you cannot define a gender variable of children of respondents who have both sons and daughters.

        Comment


        • #5
          Ah, I see your point. You are right that data in a wide layout is impossible to generate a gender variable of respondents. So, I think the easiest way is to transform the data into a long layout.
          Code:
          gen id=_n
          reshape long b2@s b3@r, i(id) j(new)
          * Male
          list b2s if b3r==3
          * Female
           list b2s if b3r==4

          Comment

          Working...
          X