Announcement

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

  • Combination of if and & operators for new variable

    Dear Stata Forum,

    My query relates to combining the if and the and (&) operator in creating a new variable. So far, I have gen newvar =99 and replace newvar = 6 if original_a != 0 & original_a == changed1_a == changed2_a

    In essence, this variable should equal 6 if original_a is not equal to 0 and the original, changed1, and changed2 variables are all the same. For example, newvar should equal 6 if the original, changed1, and changed2 all equal to 75.

    I have other replaces in place, which all work fine, for example: replace newvar =1 if original_a < changed1_a < changed2_a

    I feel the issue lies in the combination of the two operators. Following https://www.statalist.org/forums/for...=1683535816935 and Mr. Schechter's comment about the order, I have also tried to implement if original_a == == changed1_a == changed2_a & original_a != 0. Likewise, parentheses also did not help.

    I would be most grateful for any advice!

    Kind regards,
    Mary Burckhette

  • #2
    original_a == changed1_a == changed2_a
    This is the issue. You need pairwise assertions:

    Code:
    & (original_a==changed1_a)& (original_a==changed2_a) & (changed1_a==changed2_a)

    Comment


    • #3
      For the point expanded in excruciating detail, see also https://journals.sagepub.com/doi/pdf...6867X231162009

      I would not be sure that original_a < changed1_a < changed2_a worked without checking very carefully. It's the same issue: If it did work, that's numerical coincidence.

      Code:
      original_a < changed1_a
      is evaluated first and will thus become 0 or 1; and then the comparison is whichever of

      Code:
      0 < changed1_a
      
      1 < changed1_a
      applies, observation by observation.

      Otherwise put, you're hoping that Stata understanding mathematics the way you do, but that is only true here if you parenthesise aggressively. (I don't think Stata is exceptional here either.)

      Code:
       original_a < changed1_a < changed2_a  
      needs to be re-cast as

      Code:
       
      (original_a < changed1_a) & (changed1_a < changed2_a) 
      (DIfferent issue: is exact equality impossible?)
      Last edited by Nick Cox; 08 May 2023, 05:11.

      Comment


      • #4
        Dear Andrew! Thank you so much! All works!

        Comment


        • #5
          Originally posted by Nick Cox View Post
          For the point expanded in excruciating detail, see also https://journals.sagepub.com/doi/pdf...6867X231162009

          I would not be sure that original_a < changed1_a < changed2_a worked without checking very carefully. It's the same issue: If it did work, that's numerical coincidence.

          Code:
          original_a < changed1_a
          is evaluated first and will thus become 0 or 1; and then the comparison is whichever of

          Code:
          0 < changed1_a
          
          1 < changed1_a
          applies, observation by observation.

          Otherwise put, you're hoping that Stata understanding mathematics the way you do, but that is only true here if you parenthesise aggressively. (I don't think Stata is exceptional here either.)

          Code:
           original_a < changed1_a < changed2_a 
          needs to be re-cast as

          (original_a < changed1_a) & (changed1_a < changed2_a)
          [/CODE]

          (DIfferent issue: is exact equality impossible?)
          Dear Nick,
          Thank you very much for the paper and your reply! Stata has made several real changes because of the parentheses!
          Regarding your question, I assume you mean exact equality between the numbers? All numbers are coded as bytes and represent integers from 0 to 100 (inclusive). It's possible that the three numbers (original, changed, and changed1) are equal to each other.

          May I kindly ask what a possible underlying problem could be?

          Many thanks in advance!

          Comment


          • #6
            Well, if two or even three of your variables are exactly equal to each other, then they fail one or even both comparisons and the net result will be false (zero). For example,

            (2 < 2) & (2 < 3)

            yields false, as the first comparison is false.

            Perhaps that is what you want, i.e. you want exactly what you asked for. If so, no problem.

            Comment


            • #7
              I see, thank you! Yes, that's not a problem. I'm specifically looking at deviations, i.e., when the numbers are unequal.

              Comment

              Working...
              X