Announcement

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

  • select middle number with egn

    Hello everyone. I have 5 variables as below:

    Id d1 d2 d3 d4
    1 2 2 3 4


    i used egen=rowmin(d1-d4) rowmax(d1-d4) to find the min and max across d1-d4. Now how can I select the variable in between? in other words how can I select the second minimum value across d1-d4? in this case value 3.
    I looked through other topics as of using rowranks and ranks but it did not help me.
    I appreciate your help and suggestion.

  • #2
    If you found rowranks then you should have found rowsort (Stata Journal):


    ---------------------------------------------------------------------------------------
    search for rowranks (manual: [R] search)
    ---------------------------------------------------------------------------------------

    Search of official help files, FAQs, Examples, SJs, and STBs

    SJ-9-1 pr0046 . . . . . . . . . . . . . . . . . . . Speaking Stata: Rowwise
    (help rowsort, rowranks if installed) . . . . . . . . . . . N. J. Cox
    Q1/09 SJ 9(1):137--157
    shows how to exploit functions, egen functions, and Mata
    for working rowwise; rowsort and rowranks are introduced


    Code:
    clear
    input Id d1 d2 d3 d4
    1 2 2 3 4
    2 3 1 666 7 
    end
    
    rowsort d*, gen(D1-D4)
    
    list
    
         +----------------------------------------------+
         | Id   d1   d2    d3   d4   D1   D2   D3    D4 |
         |----------------------------------------------|
      1. |  1    2    2     3    4    2    2    3     4 |
      2. |  2    3    1   666    7    1    3    7   666 |
         +----------------------------------------------+
    Last edited by Nick Cox; 13 Apr 2018, 12:22. Reason: Typo fix. Stray text (which didn't produce any code errors)

    Comment


    • #3
      Dear Nick
      thanks for the response. yes as you also mentioned above, it made the value of 666 which does not sens to me. and it does not extract number 3 as the second high value. so did i make mistake somewhere?

      Comment


      • #4
        I just doubled your example which isn't instructive as (surprise) 1 2 2 3 will just get sorted to 1 2 2 3.

        3 1 666 7 gets sorted to 1 3 7 666 as it should. (I have removed the 42 from #1 which is just a typo.)

        That is, rowsort always produces new variables.

        Comment


        • #5
          I used rowsort and yes it made variables and put the value of d1:d4 in new variable but in order. but again how can i choose the second highest value? rowsort just ordered my value in different variable

          Comment


          • #6
            d3 contains the second highest value. If you want to call it something else, rename it or use the descending option.

            Comment


            • #7
              Thanks a lot Nick. It really helped

              Comment

              Working...
              X