Announcement

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

  • Combining/Concatenating Multiple Binary Variables Into Single Binary Variable

    Hello All.

    I'm working with a number of binary categorical variables and I want to identify all cases that meet the "1" condition on every one of the constituent variables. I've noticed other great advice here for related concatenation questions, but I've not noticed information related to my question. So, to be clear, my constituent variables are something like:

    tall: 1 or 0
    happy: 1 or 0
    lucky 1 or 0
    married 1 or 0

    What I'm hoping to produce from these existing variables is a single variable that in the 1 condition includes all respondents who are tall AND happy AND lucky AND married- who have met all conditions. Those cases for whom all are true would be coded 1 and those cases that did not meet ANY ONE of the constituent conditions would be a 0. To be clear, in this case, someone who is tall AND happy AND lucky BUT NOT married would be a 0.

    I gather that this is a relatively easy problem to solve, but it has as yet eluded me. Many thanks in advance to anyone who can offer some help!

  • #2
    You might want to look into whether egen has something for that.

    But you could also do it quickly from scratch.
    Code:
    generate byte every_one = 1
    foreach var of varlist tall happy lucky married {
        assert inlist(`var', 0, 1)
        quietly replace every_one = 0 if `var' != 1
    }
    Edited to add: I forgot to mention it, but if you have confidence in the integrity of your data, then you can do it in a single line using Stata's Boolean operators.
    Code:
    generate byte every_one = tall & happy & lucky & married
    And in that case you can also just multiply them.
    Code:
    generate byte every_one = tall * happy * lucky * married
    (This latter would accommodate missing values listwise, if that's how you want to handle them.)
    Last edited by Joseph Coveney; 13 Aug 2015, 01:39.

    Comment


    • #3
      As Joseph has pointed out, some egen functions can be useful here. One example:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(id tall happy lucky married)
      1 0 1 1 0 
      2 1 1 1 1 
      2 1 1 . 1 
      end
      
      * Assuming that all variables are either 0, 1, or missing
      egen byte all1 = rowtotal(tall happy lucky married)
      replace all1 = all1 == 4

      Comment


      • #4
        Thanks very much for your collective input. I was able to successfully achieve my goal by using each of these methods. I really appreciate the generosity of the community here- I can't say thanks enough!

        Comment

        Working...
        X