Announcement

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

  • Find a Max value and return the values of other variabels in the same row.

    Dear Statalist Users

    My problem is probably a basic one for you to solve, but I hope you will take a little time to help me - I have used all day trying to solve what I believed would be really easy to do.

    Below is a very simplified version of my dataset.
    I would like STATA to find the maximum value of "LKF" and then return the values of "Frame" and "LKVarus" in the same row as the max value of LKF (e.g. the value of Frame that STATA should return should be 3 in my simplified dataset).

    I am going to use these specific values of "Frame" and "LKVarus" in a later analysis, so I try to create local values for this later use.

    Here is the simplified dataset (please note that both LKF and LKVarus contains a few missing values as this gave me some problems in my early attempts).
    Frame LKF LKVarus
    0 1 2
    1 2 2
    2 3 2
    3 4 1
    4 . .
    5 . .
    I have tried to solve it in several different ways, but my most promising try is probably this one:

    Code:
    summarize LKF
    local LKFMax = r(max)
    local Frame_at_LFKMax Frame if LKF=`LFKMax'
    display `Frame_at_LFKMax'
    When I run the code I get the following message:
    0 If not found
    r(111)

    Can you help get the "local Frame_at_LFKMax" right or maybe suggest another way for me to solve this issue?

    Thanks in advance
    Last edited by Steen Harsted; 14 Jun 2016, 08:04.

  • #2
    Your problem is ill-posed. If there are multiple observations ("rows") which tie for the maximum value of LKF, and if those have different values of Frame, then there is no answer to your question. You need to come up with a rule to resolve any ties that occur. Or you need to verify in advance that no such ties exist. The code below operates by verifying the absence of such ties:

    Code:
    summ LKF, meanonly
    local LKF_Max = r(max)
    summ Frame if LKF == `LKF_Max', meanonly
    assert r(N) == 1 // VERIFY NO TIES
    local Frame_at_LKF_Max = r(mean)
    Notes:
    1. In your example, the variables are all integers. The above code will work fine for integers. However, if you have non-integer values, you may run into precision problems in the -summ- statement. It is always risky to condition on the exact equality of two floating point numbers.
    2. The -assert- statement will complain and halt execution if there is more than one observation with the maximum value of LKF. It will also do that if, due to precision issues, it can't find any exact match.

    Comment


    • #3
      Another way to approach the problem (provided that there are no missings on LKF) :

      Code:
       
      sort LKF 
      list Frame if LKF == LKF[_N]

      Comment

      Working...
      X