Announcement

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

  • Common value in dummies

    Hi statalist community,

    I need help in the following problem:


    /*-------------------------------------------------------------------------------------------------------------*
    |
    |
    I | have three dummies, D1, D2, and D3.
    |
    I | need D4
    |
    | Condition-a of D4: takes value 1 if either of D1 D2 D3 is 1
    | Condition-b of D4: takes value 0 for all rows, in which D1 D2 D3 are not 1, with another condition that at least one of D1 D2 D3, is zero
    | Condition-c of D4: takes value '.' missing value for all rows, in which D1 D2 D3, are all '.' missing values
    |
    *-----------------------------------------------------------------------------------------------------------------------------------------*/
    Last edited by ajay pasi; 27 Nov 2022, 08:09.

  • #2
    ajay pasi I'm not able to entirely figure out what you want D4 to do for each possible value of D1, D2 and D3. Perhaps you can show us a logic table?

    Comment


    • #3
      If Column_D1 takes value 0 or 1
      If Column_D2 takes value 0 or 1
      If Column_D3 takes value 0 or 1


      Logic using three columns (i.e., Column_D1, Column_D2, Column_D3) D4
      If at least one of the three columns is 1 1
      If all (three) columns are missing values missing value
      between three columns, at least column has 0 and other columns have missing value 0
      Last edited by ajay pasi; 27 Nov 2022, 08:36.

      Comment


      • #4
        I think any of these serves your purpose:

        Code:
        gen byte D4 = (D1 == 1 | D2 == 1 | D3 == 1)
        replace D4 = . if D1 == . & D2 == . & D3 == .
        or

        Code:
        egen byte D5 = anymatch(D1 D2 D3), value(1)
        replace D5 = . if D1 == . & D2 == . & D3 == .
        or even just

        Code:
        gen byte D6 = max(D1, D2, D3)
        Here are the results of all possible combinations of values of the three variables:

        Code:
        . li, noobs sep(0)
          +-----------------------------+
          | D1   D2   D3   D4   D5   D6 |
          |-----------------------------|
          |  0    0    0    0    0    0 |
          |  0    0    1    1    1    1 |
          |  0    0    .    0    0    0 |
          |  0    1    0    1    1    1 |
          |  0    1    1    1    1    1 |
          |  0    1    .    1    1    1 |
          |  0    .    0    0    0    0 |
          |  0    .    1    1    1    1 |
          |  0    .    .    0    0    0 |
          |  1    0    0    1    1    1 |
          |  1    0    1    1    1    1 |
          |  1    0    .    1    1    1 |
          |  1    1    0    1    1    1 |
          |  1    1    1    1    1    1 |
          |  1    1    .    1    1    1 |
          |  1    .    0    1    1    1 |
          |  1    .    1    1    1    1 |
          |  1    .    .    1    1    1 |
          |  .    0    0    0    0    0 |
          |  .    0    1    1    1    1 |
          |  .    0    .    0    0    0 |
          |  .    1    0    1    1    1 |
          |  .    1    1    1    1    1 |
          |  .    1    .    1    1    1 |
          |  .    .    0    0    0    0 |
          |  .    .    1    1    1    1 |
          |  .    .    .    .    .    . |
          +-----------------------------+
        Last edited by Hemanshu Kumar; 27 Nov 2022, 08:51.

        Comment


        • #5

          From egen byte D4 = anymatch(D1 D2 D3), value(1)


          say, if D1 is 0 and D2 is missing and D3 is missing. In this case, will the code produce 0 in D4?

          Comment


          • #6
            Yes. See the edited post in #4, which includes the results of all possible combinations of values.

            Comment


            • #7
              thanks Hemanshu.

              Comment

              Working...
              X