Announcement

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

  • Morgan Law in Stata

    Hi!

    I have three binary variables hh_d_floor, hh_d_wall and hh_d_roof. The three of them take values of 0 and 1.

    I am trying to run the following instruction:

    gen housing = 1 if (hh_d_floor == 1| hh_d_wall == 1| hh_d_roof == 1)**The household has inadequate (1) housing materials in any of the three components: floor, roof, or walls.
    replace housing=0 if (hh_d_floor==0) & (hh_d_wall==0) & (hh_d_roof==0) **The household has adequate (0) housing materials if it has all of the three components: floor, roof, or walls.

    The first line is giving me an error....How could I express the union of threeevents in stata?
    and... is the intersection defined with &?

    Thanks!
    Last edited by Silvana Builes; 07 Jul 2023, 20:48.

  • #2
    I solved it


    gen housing = 1 if hh_d_floor == 1 | hh_d_wall == 1 | hh_d_roof == 1
    replace housing=0 if hh_d_floor==0 & hh_d_wall==0 & hh_d_roof==0

    Comment


    • #3
      By the way, Boolean expressions can be directly used without the if conditional (not to be confused with the -if- command). A more efficient version of your code would be:
      Code:
      gen housing = (hh_d_floor == 1| hh_d_wall == 1| hh_d_roof == 1)
      Also, there is nothing wrong with your original code. The source of the error isn't because of the parentheses.
      Last edited by William Liu; 07 Jul 2023, 21:26.

      Comment


      • #4
        Code:
        gen housing = inlist(1, hh_d_floor, hh_d_roof, hh_d_wall)
        is another way to do it.

        Comment


        • #5
          Side note: in all these methods, be mindful of missing values and their consequences for the boolean evaluations.

          Comment

          Working...
          X