Announcement

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

  • Combining two variables

    Hello everyone
    I apologize in advance if this question has already been asked.

    I have a variable var1 "births in the past year" coded as 1=yes (the respondent has given birth in the past year), 0=no (the respondent has not given birth in the past year);
    and a variable var2 "currently pregnant", where 1=yes (the respondent was pregnant at the time of the interview), 0=no (the respondent was not pregnant).

    I need to create a third variable, var3, coded as
    1=respondents who gave birth in the past year and respondents who are pregnant (category=1 of var1+var2), 0=respondents who have not given birth in the past year (as =0 in var 1).

    Or, to put it in another way, I would like to add to the category =1 of var1, the category=1 of var2.

    I tried by using the -stack- command, but I need something that does not delete the rest of my dataset.

    I hope I made myself clear, thank you!

    Virginia

  • #2
    Code:
    gen wanted = 1 if var1==1 | var2==1
    replace wanted =0 if wanted==. & !missing(var1, var2)
    ​​​
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Thank you Marteen!
      I think i did not explained myself, var3 should be coded as
      1=respondents who gave birth in the past year and/or were pregnant at the time of the interview (category=1 of var1+ 1 of var2),
      0=respondents who have not given birth in the past year (as =0 in var 1)

      Comment


      • #4

        Code:
        gen wanted  = inlist(1, var1, var2)
        Code:
        gen wanted = var1 == 1 | var2 == 1
        -- although on the face of it this is what Maarten Buis suggested too.

        Comment


        • #5
          Not quite as Maarten Buis handles missing values differently.

          Comment


          • #6
            Be clear though that Maarten and Nick provide solutions where:

            1=respondents who gave birth in the past year and/or were pregnant at the time of the interview (category=1 of var1+ 1 of var2),
            0=respondents who have not given birth in the past year AND are not pregnant at the time of interview

            Which seems like your likely intention, but not what you describe at any point.

            Comment


            • #7
              Originally posted by Virginia Salaorni View Post
              Thank you Marteen!
              I think i did not explained myself, var3 should be coded as
              1=respondents who gave birth in the past year and/or were pregnant at the time of the interview (category=1 of var1+ 1 of var2),
              0=respondents who have not given birth in the past year (as =0 in var 1)
              You were clear, and that is what I tried to do. You did not say what you wanted to do with missing values, so Nick and I made assumptions. In the example below you can see what they are:

              Code:
              . clear
              
              . input var1 var2
              
                        var1       var2
                1. 1 1
                2. 0 1
                3. 1 0
                4. 0 0
                5. . 1
                6. 1 .
                7. . 0
                8. 0 .
                9. . .
               10. end
              
              .
              . label var var1 "gave birth last year"
              
              . label var var2 "currently pregnant"
              
              . label define yesno 0 "no" 1 "yes"
              
              . label value var1 var2 yesno
              
              .
              . list
              
                   +-------------+
                   | var1   var2 |
                   |-------------|
                1. |  yes    yes |
                2. |   no    yes |
                3. |  yes     no |
                4. |   no     no |
                5. |    .    yes |
                   |-------------|
                6. |  yes      . |
                7. |    .     no |
                8. |   no      . |
                9. |    .      . |
                   +-------------+
              
              .
              .
              . // my solution:
              . gen byte wanted:yesno = 1 if var1==1 | var2==1
              (4 missing values generated)
              
              . replace wanted        = 0 if wanted==. & !missing(var1, var2)
              (1 real change made)
              
              .
              .
              . // Nick's solutions
              . gen byte wanted2:yesno = inlist(1, var1, var2)
              
              . gen byte wanted3:yesno = var1 == 1 | var2 == 1
              
              .
              . // Yet another solution
              . gen byte wanted4:yesno = inlist(1, var1, var2) if !missing(var1,var2)
              (5 missing values generated)
              
              . list
              
                   +----------------------------------------------------+
                   | var1   var2   wanted   wanted2   wanted3   wanted4 |
                   |----------------------------------------------------|
                1. |  yes    yes      yes       yes       yes       yes |
                2. |   no    yes      yes       yes       yes       yes |
                3. |  yes     no      yes       yes       yes       yes |
                4. |   no     no       no        no        no        no |
                5. |    .    yes      yes       yes       yes         . |
                   |----------------------------------------------------|
                6. |  yes      .      yes       yes       yes         . |
                7. |    .     no        .        no        no         . |
                8. |   no      .        .        no        no         . |
                9. |    .      .        .        no        no         . |
                   +----------------------------------------------------+
              ---------------------------------
              Maarten L. Buis
              University of Konstanz
              Department of history and sociology
              box 40
              78457 Konstanz
              Germany
              http://www.maartenbuis.nl
              ---------------------------------

              Comment

              Working...
              X