Announcement

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

  • Correctly Create a Binary Variable within ID that Satisfy some Conditions in Stata.

    I have a dataset like this,
    clear
    input byte (id state)
    1 0
    1 0
    1 0
    2 0
    2 1
    2 0
    3 0
    3 .
    3 0
    4 0
    4 .
    4 1
    4 .
    4 0
    end

    I want to create a new variable "fail", which should satisfy the rule below,
    1) when all values of the variable state=0 within id, then all values of the variable fail==0;
    2) as long as there is a value of state=1 within id, then all values of the variable fail==1;
    3) when the state variable takes on the value of "0" and "missing value" within id, then all values of the variable fail==missing value

    In sum, the resulting data should look like this,
    clear
    input byte (id state fail)
    1 0 0
    1 0 0
    1 0 0
    2 0 1
    2 1 1
    2 0 1
    3 0 .
    3 . .
    3 0 .
    4 0 1
    4 . 1
    4 1 1
    4 . 1
    4 0 1
    end

    Can someone help me with Stata Code?
    Thank you!
    Last edited by smith Jason; 24 Jul 2022, 09:48.

  • #2
    id 1 does not fit your rules (it appears 4 times and has 3 0's and 1 4) but you have given it a "1" - does that mean that your rule 2 should be something like. "if there is a non-missing value greater than 0 for an id then fail=1 for all occurrences of that id" - if not please clarify

    Comment


    • #3
      Thank you. It is typo and the problematic data is removed just now.

      Comment


      • #4
        If none of your conditions are satisfied, then the result is coded ".a".

        Code:
        clear
        input byte (id state fail)
        1 0 0
        1 0 0
        1 0 0
        2 0 1
        2 1 1
        2 0 1
        3 0 .
        3 . .
        3 0 .
        4 0 1
        4 . 1
        4 1 1
        4 . 1
        4 0 1
        end
        bys id: egen wanted= max(state==1)
        bys id (state): replace wanted= cond(!state[1] & !state[_N], 0, cond(!state[1] & missing(state[_N]), ., .a)) if !wanted
        Res.:

        Code:
        . l, sepby(id)
        
             +----------------------------+
             | id   state   fail   wanted |
             |----------------------------|
          1. |  1       0      0        0 |
          2. |  1       0      0        0 |
          3. |  1       0      0        0 |
             |----------------------------|
          4. |  2       0      1        1 |
          5. |  2       0      1        1 |
          6. |  2       1      1        1 |
             |----------------------------|
          7. |  3       0      .        . |
          8. |  3       0      .        . |
          9. |  3       .      .        . |
             |----------------------------|
         10. |  4       0      1        1 |
         11. |  4       0      1        1 |
         12. |  4       1      1        1 |
         13. |  4       .      1        1 |
         14. |  4       .      1        1 |
             +----------------------------+
        Last edited by Andrew Musau; 24 Jul 2022, 10:02.

        Comment


        • #5
          Thank you!

          Comment

          Working...
          X