Announcement

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

  • Question on Order of Operations

    Hello I am new to STATA and noticed a difference in the following lines of code:

    replace var = 1 if dup_counter[_n] != 0 & dup_counter[_n+1] != 0 & (var[_n+1]>var[_n] | var[_n]>var[_n+1])

    replace var = 1 if dup_counter[_n] != 0 & dup_counter[_n+1] != 0 & var[_n+1]>var[_n] | var[_n]>var[_n+1]

    Why do the parentheses matter? The 1st line of code only modified 40 results while the 2nd modified several hundred.

    Some duplicates had missing value (.) and other had values of 1 within a group of duplicates. The goal was to change all the missing within a duplicate group to 1.
    Last edited by Roy Wang; 23 May 2018, 21:55.

  • #2
    The parentheses matter for the same reason that they matter in 2*(3+4) vs 2*3+4. Just as multiplication takes precedence over addition, & takes precedence over |.

    Comment


    • #3
      From

      Code:
      help operator
      The order of evaluation (from first to last) of all operators is ! (or ~), ^, - (negation), /, *, - (subtraction), +, != (or ~=), >, <, <=, >=, ==, &, and |.
      As for the problem, Roy probably wants to sort before the replace commands; details of code depend on the details of the problem, of which we only see a presumably last step.

      Best
      Daniel
      Last edited by daniel klein; 24 May 2018, 01:21.

      Comment

      Working...
      X