Announcement

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

  • Creating new variable which takes the largest value from two pre existing variables

    I am new to stata, and I am trying to create a new variable (z) which takes the largest value from two pre-existing variables, i.e. if x = 6 and y = 5 z = 6.
    The current code I am using is not working properly, as if the score in variable y is lower than x, z is being populated with y instead of x.

    Please see the code below:

    gen w1ei1 = w1eimotherrecode if w1eimotherrecode >= w1eifatherrecode ! =.a
    replace w1ei1 = w1eifatherrecode if w1eifatherrecode >= w1eimotherrecode ! =.a

    If anyone can help with it would be greatly appreciated!

  • #2
    you probably want the -egen- command with the "max" function; however, it is impossible to be sure without a data example - the FAQ gives much more on this so please read the FAQ; also, see
    Code:
    help egen
    Last edited by Rich Goldstein; 02 Dec 2024, 12:45.

    Comment


    • #3
      @Rich Goldstein's advice is excellent, as always.

      I just want to point out an additional problem in the original post, involving legal syntax that has unintuitive semantics.

      Code:
      gen w1ei1 = w1eimotherrecode if w1eimotherrecode >= w1eifatherrecode ! =.a
      is legal syntax. But it's hard to know what O.P. intends it to mean; there is a good chance it actually means something different from what she has in mind.

      While it is legal in Stata syntax to chain equations and inequalities as one would in ordinary algebraic notation, e.g. a <= b <= c, the meaning is different from what it means in algebra. An expression like a >= b != c is interpreted by Stata as follows. First, there is association to the left: (a >= b) != c. Now, a >=b is a logical expression that is either true or false, and Stata represents true and false by 1 and 0 respectively. So a >= b will be evaluated as either 1 or 0 depending on whether or not a is greater than or equal to b. Stata next compares .a to that 0 or 1, and find them unequal. So the result of 1 (or 0) != .a is always true.

      Perhaps this is what O.P. wants, but I am pretty sure it is not.
      Last edited by Clyde Schechter; 02 Dec 2024, 13:00.

      Comment


      • #4

        Code:
        gen w1ei1 =  max(w1eimotherrecode, w1eifatherrecode)
        is I think the main idea you want. You need not specify code for equal values, because if codes are the same you will get the right answer.

        Your code was wrong because you can't chain inequalities like that -- or at least the result will usually not be what you want -- and in any case ! = .a should be != .a. But if you got results you may not have typed exactly what you are showing.

        Note that max() will ignore .a in favour of any numeric value. If you need to exclude it even with the code above than add an if qualifier, possibly

        Code:
        if !inlist(.a, w1eimotherrecode, w1eifatherrecode)
        I agree with Rich Goldstein that a data example would help, including detail on what you specifically want to happen given .a.

        Comment

        Working...
        X