Announcement

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

  • Gen newvar=1 if at least one (of four variables) in one row on absolute basis is greater than 2 (all bytes)

    Dear Statalist,

    I am trying to generate a new number (called rational) to be equal to 1 if at least 1 of four numbers in one row is greater than or equal to 2. Rational should equal 0 if all numbers in one row on an absolute basis are less than 2.

    Code:
    gen rational =99
    replace rational =1 if abs(number1 >= 2) | abs(number2 >= 2) | abs(number3 >= 2) | abs(number4 >= 2)
    replace rational =0 if abs((number1 < 2) & abs(number2 < 2) & (number3 < 2) & abs(number4 < 2))
    Perhaps, to illustrate, the third line counting from the bottom of the list is 0 0 1 -1 - since all numbers on an absolute basis are less than 2, rational should equal 0. It does, but I'm not certain it's because the code is right..
    Likewise, the first row -6 -2 -2 9 should have rational = 1 as all numbers are either equal to or greater than 2 - even though I only specify that at least one of the numbers is equal to or greater than 2.

    I would be most grateful if anyone could improve my code as clearly everything is not rational even though some numbers should be.

    Thank you very much in advance!


    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte(number1 number2 number3 number4) float rational
     -6  -2  -2   9 0
      0  -7   1   7 0
     -1   8  -2  -6 0
     -1   7   0  -6 0
      0  -1   0   1 0
     -4   1  -2   5 0
      0  -9   6   3 0
      0  14  -6  -8 0
     -3   7   0  -4 0
     -2   0   0   2 0
     -1  -2   0   3 0
     -2   3   0  -1 0
    -17  17   0   0 0
     -2   2   0   0 0
     -1   4  -1  -1 0
     -3   6  -1  -2 0
      2 -11   4   5 0
     -4   8  -4   0 0
      4 -16   3   9 0
    -10  -1  -3  14 0
     -1  -1   0   3 0
     -1   2  -3   2 0
     -6   7   0  -1 0
     -3  22  -2 -18 0
      0  15 -11  -3 0
      0  31 -22  -9 0
     -7   7   0   0 0
      0   4   0  -4 0
     -2   1  -3   3 0
      0 -16  10   5 0
     -3   0   3   0 0
      0  -6  -6  12 0
     -5  32  -8 -19 0
      0 -26  11  15 0
      0   4  -4   0 0
      1  -8   3   4 0
     -4   4  -1   1 0
      0   0   0   0 0
    -16  35  -5 -14 0
      5 -10   3   3 0
      6 -23  12   5 0
     -2  13  -7  -4 0
     -3   6   1  -3 0
      0   0  -1   2 0
      0   9  -7  -2 0
     -5  -3  -3  11 0
      0  -8   0   8 0
    -10  13  -5   2 0
     -3   0  -1   3 0
    -12  33 -11  -9 0
      0   7  -5  -2 0
      2 -14   9   3 0
      0   9  -4  -5 0
      0 -18  11   7 0
    -10  34 -10 -14 0
     -5  -3  -1  10 0
      0 -13   0  13 0
     -2  -1   0   4 0
      1 -12   2   8 0
     -2   2  -2   2 0
      0  13 -13   0 0
     -8  17  -6  -3 0
     -1  -4   3   1 0
     -9   9   0   0 0
      0  -3   0   3 0
     -5   8   0  -3 0
     -5   0   1   4 0
      0  10   0 -10 0
     -8  19  -7  -3 0
     -3  20 -12  -5 0
     -2  -1   0   3 0
     -2  14 -12   0 0
      5 -31  13  13 0
     -6  12  -3  -3 0
     -5  -1  -1   7 0
     -1  -1  -1   3 0
     -1   4   0  -3 0
     -2   7  -3  -2 0
     -3   1  -2   4 0
     -1   2   0   0 0
      2  -4   2   1 0
    -11  13  -3   1 0
     -5   9  -2  -2 0
      0  11  -9  -2 0
      0  -2  -1   4 0
     -7   7  -2   3 0
     -1   0   1   0 0
    -14  19  -6   0 0
     -2   0   2   0 0
     -5   1   0   4 0
     -2   3  -2   0 0
     -6   8  -1  -1 0
      3 -13   7   3 0
     -4  12 -10   1 0
      0  -1   0   1 0
      2 -12   5   6 0
     -1   0   0   2 0
      0   0   1  -1 0
     -1   9  -6  -2 0
      0  -2   0   2 0
    end

  • #2
    the problem is your placement of the parentheses; you write, e.g., "abs(number1 >= 2" but this is a logical condition which means that it is equal to 0, if false, or 1, if true; instead, your parens should close after "number1" - here is an example:
    Code:
    abs(number1) >= 2
    be sure to change all 4 on each of your lines

    you could also use the "rowmax" function of the -egen- command; see
    Code:
    h egen

    Comment


    • #3
      As missing counts as greater than 2, I suggest a careful two-step

      Code:
      gen max = max(abs(number1), abs(number2), abs(number3), abs(number4))
      
      gen wanted = max >= 2 if max < .
      The result will be missing if all arguments are missing, 1 otherwise if any is at least 2 in absolute value, and 0 otherwise.

      Note that abs(1) and abs(0) just return 1 and 0 respectively, which is relevant because you are taking absolute values of true or false values 1 and 0.

      It seems that you want e.g. abs(number1) >= 2 as a necessary but not sufficient condition, which is quite different from abs(number1 >= 2).

      Your code shows other inconsistent details which I'll guess are typos.

      Hope that helps.

      Comment


      • #4
        Thank you very much! The code works now!

        Comment

        Working...
        X