Announcement

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

  • Different results from similar non-legal uses of OR operator

    Hello all,

    I got a question about why these incorrect uses of the OR operator would end up with behaviors. I think because this is not a proper use, so I was not able to find any clue in the documentation. Here are the data and the incorrect codes:

    Code:
    clear
    set obs 20
    set seed 776
    gen x = runiform() >= 0.5
    Question: Why does the first code below show only cases where x = 1, but the second code prints everything?

    Code:
    . list if x == 1|0
    
         +---+
         | x |
         |---|
      1. | 1 |
      2. | 1 |
      3. | 1 |
      9. | 1 |
     13. | 1 |
         |---|
     17. | 1 |
     18. | 1 |
         +---+
    
    .
    . list if x == 0|1
    
         +---+
         | x |
         |---|
      1. | 1 |
      2. | 1 |
      3. | 1 |
      4. | 0 |
      5. | 0 |
         |---|
      6. | 0 |
      7. | 0 |
      8. | 0 |
      9. | 1 |
     10. | 0 |
         |---|
     11. | 0 |
     12. | 0 |
     13. | 1 |
     14. | 0 |
     15. | 0 |
         |---|
     16. | 0 |
     17. | 1 |
     18. | 1 |
     19. | 0 |
     20. | 0 |
         +---+
    I fully understand that this is not correct, but am curious why the different outcomes.

    Thanks, and I look forward to your thoughts.
    Last edited by Ken Chui; 07 Oct 2022, 06:58.

  • #2
    Code:
     
     list if x == 0|1
    is parsed as

    Code:
     
     list if (x == 0)|1
    and while x == 0 is true contingently 1 is true (because non-zero) universally.

    The fallacy is that the first statement is not equivalent to

    Code:
    list if x == 0 | x == 1
    For more on true and false in Stata see e.g. https://www.stata.com/support/faqs/d...rue-and-false/ or https://www.stata-journal.com/articl...article=dm0087

    Comment


    • #3
      The correct usage should be

      Code:
      list if x == 1|x==0
      Your current commands state "list if x=0" or "if TRUE" and "list if x= 1" or "if FALSE". All positive numbers and missing evaluate to "TRUE", 0 evaluates to "FALSE".

      Code:
      clear
      set obs 20
      set seed 776
      gen x = runiform() >= 0.5
      
      list if 1
      list if .
      list if 0
      Res.:

      Code:
      . list if 1
      
           +---+
           | x |
           |---|
        1. | 1 |
        2. | 1 |
        3. | 1 |
        4. | 0 |
        5. | 0 |
           |---|
        6. | 0 |
        7. | 0 |
        8. | 0 |
        9. | 1 |
       10. | 0 |
           |---|
       11. | 0 |
       12. | 0 |
       13. | 1 |
       14. | 0 |
       15. | 0 |
           |---|
       16. | 0 |
       17. | 1 |
       18. | 1 |
       19. | 0 |
       20. | 0 |
           +---+
      
      .
      . list if .
      
           +---+
           | x |
           |---|
        1. | 1 |
        2. | 1 |
        3. | 1 |
        4. | 0 |
        5. | 0 |
           |---|
        6. | 0 |
        7. | 0 |
        8. | 0 |
        9. | 1 |
       10. | 0 |
           |---|
       11. | 0 |
       12. | 0 |
       13. | 1 |
       14. | 0 |
       15. | 0 |
           |---|
       16. | 0 |
       17. | 1 |
       18. | 1 |
       19. | 0 |
       20. | 0 |
           +---+
      
      .
      . list if 0
      
      .
      Crossed with #2
      Last edited by Andrew Musau; 07 Oct 2022, 07:20.

      Comment


      • #4
        Let's be clear: In Stata illegal uses are flagged as syntax errors. Code that works without syntax error but does not do what you want is incorrect from your point of view but not illegal.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          Let's be clear: In Stata illegal uses are flagged as syntax errors. Code that works without syntax error but does not do what you want is incorrect from your point of view but not illegal.
          Thank you, Nick and Andrew!

          I think I got it now.

          And Nick, thanks for the advice on the accurate wording regarding "legal".

          Comment


          • #6
            As a further note, it might be guessed from the fact that


            Code:
            list if x == 0 | 1
            is parsed as

            Code:
             list if (x == 0) | 1
            that we just need parentheses to insist on what is wanted, namely
            Code:
            list if x == (0 | 1)


            but that is not going to work either. because

            Code:
            0 | 1 
            is just evaluated as 1. There are alternatives to

            Code:
            x == 0 | x == 1
            such as

            Code:
            inlist(x, 0, 1)
            or

            Code:
            inrange(x, 0, 1)
            -- where the last is correct if and only if there are no values between 0 and 1.
            Last edited by Nick Cox; 08 Oct 2022, 12:10.

            Comment

            Working...
            X