Announcement

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

  • Creating variables from equations with conditional components

    I have a list of 9 variables with numerical data and want to create a syntax that will look at all 9, line by line, and select the 3 greatest values then plug those values into an equation. Since I don't need just the maximum value and some of the largest values are likely to be the same I'm not sure how to define that piece. Could anyone help me figure out how to do this.

    What makes sense to me is to (1) generate 3 new variables from the list to reflect those values (2) generate the final variable using an equation of the previously defined new 3.

    Thanks for any insight!

  • #2
    though what you want is not completely clear to me, my guess is that the user-written -rowsort- is the place to start; this can be found and downloaded using the -search- command

    Comment


    • #3
      You can also do this by reshaping the data and sorting. Here is an example:

      Code:
      set obs 20
      *GENERATE VARIABLES
      set seed 01112024
      foreach var in alpha beta gamma delta epsilon{
          gen `var'= runiformint(1, 100)
      }
      
      *WANTED
      frame put alpha-epsilon, into(rowsort)
      frame rowsort{
          rename * wanted=
          gen long obsno=_n
          reshape long wanted, i(obsno) j(which) string
          gsort obsno -wanted
          by obsno: gen which2=_n
          keep if which2<=3
          drop which
          reshape wide wanted, i(obsno) j(which2)
      }
      gen long obsno=_n
      frlink 1:1 obsno, frame(rowsort)
      frget wanted?, from(rowsort)
      drop rowsort obsno
      frame drop rowsort
      Res.:

      Code:
      . l, sep(0)
      
           +----------------------------------------------------------------------+
           | alpha   beta   gamma   delta   epsilon   wanted1   wanted2   wanted3 |
           |----------------------------------------------------------------------|
        1. |    19     36      79      76         4        79        76        36 |
        2. |    73     21      62      80        45        80        73        62 |
        3. |     2     72      69      29        42        72        69        42 |
        4. |    60     38      41      32        37        60        41        38 |
        5. |    53     89      64      75        95        95        89        75 |
        6. |    70     63      82      37        42        82        70        63 |
        7. |    71     81      22      95        71        95        81        71 |
        8. |    62     65      12      92        52        92        65        62 |
        9. |    34     90      61      58        56        90        61        58 |
       10. |    94     49      12      23         7        94        49        23 |
       11. |    11     73       5      47        38        73        47        38 |
       12. |    45     18      20      59         6        59        45        20 |
       13. |    63     52      67      56        33        67        63        56 |
       14. |    85     36      84      54        77        85        84        77 |
       15. |     3     60       5      87        40        87        60        40 |
       16. |    69     15      82      63        86        86        82        69 |
       17. |    64     67      14      22        28        67        64        28 |
       18. |    72     61      72      27         4        72        72        61 |
       19. |    81     66      96      38        40        96        81        66 |
       20. |     4     52      11      19        55        55        52        19 |
           +----------------------------------------------------------------------+

      Comment

      Working...
      X