Announcement

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

  • Find second highest (max) in a range with asrol

    I am using asrol (downloaded from the ssc). Is there a way to find second highest value or the second maximum in a rolling window.

  • #2
    I have added max2, max3, max4, and max5 to the list of asrol statistics. These statistics will find the second, third, fourth, and the fifth largest values in a given range. As with other asrol's statistics, these new options will work in a rolling window or by groups.
    The new statistics have the following rules:
    1. Missing values are ignored
    2. For stat(max2), the minimum number of non-missing observations are 2, otherwise the result will be missing.
    3. The same rule is followed in other max functions, e.g. for max3, the minimum number of non-missing values are 3, and so on.

    Code:
    * Install the beta version from my site
    net install asrol, from(http://fintechprofessor.com) replace
    Examples:
    Code:
    *1. By groups
    webuse grunfeld
    bys company: asrol invest, stat(max2)
    
    *2. Rolling window in groups
    bys company: asrol invest, stat(max2) window(year 10)
    
    list company year invest invest_max210 in 1/20
         +-------------------------------------+
         | company   year   invest   inves~210 |
         |-------------------------------------|
      1. |       1   1935    317.6           . |
      2. |       1   1936    391.8   317.60001 |
      3. |       1   1937    410.6   391.79999 |
      4. |       1   1938    257.7   410.60001 |
      5. |       1   1939    330.8   257.70001 |
         |-------------------------------------|
      6. |       1   1940    461.2   330.79999 |
      7. |       1   1941      512   461.20001 |
      8. |       1   1942      448         512 |
      9. |       1   1943    499.6         448 |
     10. |       1   1944    547.5   499.60001 |
         |-------------------------------------|
     11. |       1   1945    561.2       547.5 |
     12. |       1   1946    688.1   561.20001 |
     13. |       1   1947    568.9   688.09998 |
     14. |       1   1948    529.2   568.90002 |
     15. |       1   1949    555.1   529.20001 |
         |-------------------------------------|
     16. |       1   1950    642.9   555.09998 |
     17. |       1   1951    755.9   642.90002 |
     18. |       1   1952    891.2   755.90002 |
     19. |       1   1953   1304.4   891.20001 |
     20. |       1   1954   1486.7      1304.4 |
         +-------------------------------------+
    Acknowledgement
    The max2 function largely follows the code suggested by Nick Cox (reference).
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

    Comment


    • #3
      Thanks. Works like a charm.

      Comment

      Working...
      X