Announcement

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

  • Running Maximum by group

    Hello Everyone

    I have a dataset that has observations on an X variable for different individuals over time. I would like to calculate new variables that equal the maximum and minimum of X for each individual when considering all the values up to time t.

    So for example

    Individual time X max min
    1 1 1 . .
    1 2 -1 1 -1
    1 3 3 3 -1

    Thank you.

    Costas

  • #2
    https://www.stata.com/support/faqs/d...m-of-sequence/

    The FAQ explains how to do it with official commands only, but my bias is to plugging rangestat as getting both variables in one line:


    Code:
    clear
    input id time X
    1 1 1
    1 2 -1
    1 3 3
    end
    
    ssc install rangestat
    
    rangestat (max) max=X (min) min=X, int(time . 0) by(id)
    
    list
    
         +----------------------------+
         | id   time    X   max   min |
         |----------------------------|
      1. |  1      1    1     1     1 |
      2. |  1      2   -1     1    -1 |
      3. |  1      3    3     3    -1 |
         +----------------------------+

    Comment


    • #3
      Thank you Nick, this worked perfectly. My date variable was an actual date, and so to simplify things and avoid using interval() I sorted by id and date and created a counter (by id: g time=_n).

      Comment


      • #4
        That shouldn't be needed with any numeric date variable. It's exactly the same logic for interval(date . 0)

        Comment

        Working...
        X