Announcement

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

  • Setting a condition when a variable must be larger than the maximum of other two

    Dear All,

    I would like to set a conditional stattements in my gen command. Something like:

    Code:
    gen y=1 if x>max{k,z}
    It might be trivial, but I cannot figure out how I can get something like that. Could you please offer me some suggestions?

    Thanks in advance

    Dario

  • #2
    max(k, z) is fine as Stata syntax (parentheses not braces).

    Comment


    • #3
      Almost exactly as you put it, except you need parentheses (so that the last term is the -max()- function). Missing values are evaluated greater than everything else, so you need care in formulating the expression.

      Code:
      sysuse auto, clear
      keep in 1/10
      keep rep78 headroom gear_ratio
      gen wanted= rep78 > max(headroom, gear_ratio) & !missing(rep78) & !(missing(headroom) & missing(gear_ratio))
      Res.:

      Code:
      . l, sep(0)
      
           +--------------------------------------+
           | rep78   headroom   gear_r~o   wanted |
           |--------------------------------------|
        1. |     3        2.5       3.58        0 |
        2. |     3        3.0       2.53        0 |
        3. |     .        3.0       3.08        0 |
        4. |     3        4.5       2.93        0 |
        5. |     4        4.0       2.41        0 |
        6. |     3        4.0       2.73        0 |
        7. |     .        3.0       2.87        0 |
        8. |     3        2.0       2.93        1 |
        9. |     3        3.5       2.93        0 |
       10. |     3        3.5       3.08        0 |
           +--------------------------------------+
      Last edited by Andrew Musau; 24 Mar 2023, 07:05.

      Comment


      • #4
        Thanks Nick Cox and Andrew Musau I just realized that it was a problem with brackets. THanks for confirming this.

        Comment


        • #5
          The max function does not obey the usual Stata logic that missing are bigger than all non-missings. E.g.,

          Code:
          . dis max(1,.)
          1
          
          . dis max(.,.)
          .
          
          . dis max(1,2,3,.)
          3

          Comment


          • #6
            Joro Kolev thanks for pointing this. I read something about. I did not care about this issue in this case, as i do not have any missing value in my dataset. But i will keep this information in mind for the future

            Comment


            • #7
              It's also Stata logic to ignore missings whenever possible. You would not think much of summarize if on being presented with 1,2,3,. it refused to answer on the grounds that a missing value makes the problem insoluble. Of course, there is software which behaves in precisely that manner.

              Comment

              Working...
              X