Announcement

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

  • How to extract _n values in month.

    hello researchers and expert good evening,
    i want to calculate 5 maximum returns in a month. for different firms. i am using code for single max value in month but unable to extract 5 max observation,
    code:
    egen max_ret = max(r), by(id date)
    bys dscd date: egen max2=max(r)

    Thanks !!!!

    Wahab Ahmed

  • #2
    Assuming you have no missing values in your variable "r"

    Code:
    bys id date (r): gen order= sum(_n>=_N-4)
    forval i=1/5{
         bys id date: egen max`i'= max((order==`i')*r)
    }
    If you have missing values or the above does not solve your problem, please read https://www.statalist.org/forums/help, especially Advice #12, and provide a data example using dataex.

    Comment

    Working...
    X