Announcement

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

  • Panel Data: max( l(0/t).x )

    Hi,
    thanks for the super helpful forum. It seems, today for the first time, I cannot find a solution here. It would be great if you could please point me to one, or help.

    I have a panel data set and would like to create a new variable that contains the maximum value over t previous values.
    Consider t = 3:

    Works:
    qui gen x_3 = max(L0.x,L1.x,L2.x)

    Does not work:
    qui gen x_3b= max( L(0/3).x)
    returns: unknown function L()

    Background:
    Data is per-second measurements with one file per day, each day is about 2.5G
    Unfortunately I will need to do this for many values of t for every day over five years.
    Eventually the particular values of t will be provided by the user in a numlist,
    so I need to generate the input for max() automatically.

    Thanks for your help!

    Christian


  • #2
    It's not clear if you panel data (-xtset-) or survival time data (-stset-). I assume panel data, and I assume that you have no gaps in the timing variable for your panels.

    In that case, if you really only care about a small, fixed window size (like 3 seconds in your example) you can hand code the varlist yourself. The other option doesn't require data to be -xtset- (though it's a good idea for some error checking), and uses -rangestat- (available from SSC).

    Code:
    input id time x
    1 1 1
    1 2 3
    1 3 2
    1 4 5
    1 5 4
    end
    
    xtset id time
    
    * option 1 - smal and fixed rolling interval (easily adaptable to a loop to build the varlist)
    gen want1 = max(x, L1.x, L2.x)
    
    * option 2 - using rangestat
    sort id time
    rangestat (max) want2=x, interval(time -2 0) by(id)
    Result

    Code:
         +-------------------------------+
         | id   time   x   want1   want2 |
         |-------------------------------|
      1. |  1      1   1       1       1 |
      2. |  1      2   3       3       3 |
      3. |  1      3   2       3       3 |
      4. |  1      4   5       5       5 |
      5. |  1      5   4       5       5 |
         +-------------------------------+

    Comment


    • #3
      the rangestat command solves my problem. I learned a lot on the way.
      Thanks Leonardo,

      Comment

      Working...
      X