Announcement

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

  • Comparing Variables

    Hello, I have not found a command to compare the values of different variables. For example, let's say I have 3 variables: var1, var2 and var3. Each variable contains a constant across all observations, var1=1, var2=2 and var3=3. I am looking for a command that keeps the variable that takes the maximum value across all variables (which in this case is var3), or at least identifies that variable (for example, the result of the command may be "var3", like the result of a "display" command).

    My dataset contains 1000 variables of this kind (constant values across all observations), so doing so manually is not feasible (like in the example).

    Thanks in advance

  • #2
    -help egen- and scroll down to the -rowmax()- function.

    Comment


    • #3
      David:
      welcome to this forum.
      You may want to consider the following toy-example:
      Code:
      . set obs 1
      Number of observations (_N) was 0, now 1.
      
      . g A=1
      
      . g B=2
      
      . g C=3
      
      . egen wanted=rowmax(A B C)
      
      . list
      
           +--------------------+
           | A   B   C   wanted |
           |--------------------|
        1. | 1   2   3        3 |
           +--------------------+
      
      .
      PS: crossed in the cyberspace with Clyde's helpful replies.
      Last edited by Carlo Lazzaro; 22 Oct 2023, 11:27.
      Kind regards,
      Carlo
      (Stata 19.0)

      Comment


      • #4
        #1 included the question of which variable holds the maximum -- which is a different question from what is the maximum.

        Note that this is tricky if two or more variables hold the same maximum. So, there are various solutions if life is that complicated, including recording the first, the last, or even all of them.

        But https://journals.sagepub.com/doi/pdf...36867X20931007 goes into some detail on what may be needed.

        Comment

        Working...
        X