Announcement

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

  • How can I identify the historical maximum value of a variable?

    How can I identify the historical maximum value of a variable in time-series data?

    I know the command egen MaxValue = max(Variable), but I don't know how to restrict Stata's search to historical values only (or prior values after sorting the data w.r.t. time). For example, I have data on the variable of interest from 2000 to 2010 and in 2005 I want to identify the maximum value of this variable up to the year 2005

  • #2
    So to get the maximum value of the variable up to the year 2005 (not including, I assume):
    Code:
    egen max_before_2005 = max(cond(year < 2005), Variable, .)
    That puts the result in a "variable" which is really a constant. A better approach, which just captures the single number in a local macro you can refer to later is:

    Code:
    summ Variable if year < 2005
    local max_before_2005 = r(max)

    Comment


    • #3
      This is an FAQ:

      FAQ . . . . . . . . . . Calculating the maximum and minimum of a sequence
      . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . N. J. Cox
      10/06 How do I calculate the maximum or minimum seen
      so far in a sequence?
      http://www.stata.com/support/faqs/data-management/
      maximum-and-minimum-of-sequence/

      egenmore (SSC), which is a rag-bag of user-written extras for egen, includes a record() function

      record(exp) [ , by(byvarlist) min order(varlist) ] produces the maximum (with min
      the minimum) value observed "to date" of the specified exp. Thus record(wage),
      by(id) order(year) produces the maximum wage so far in a worker's career,
      calculations being separate for each id and records being determined within
      each id in year order. Although explanation and example here refer to dates,
      nothing in record() restricts its use to data ordered in time. If not otherwise
      specified with by() and/or order(), records are determined with respect to the
      current order of observations. No special action is required for missing
      values, as internally record() uses either the max() or the min() function,
      both of which return results of missing only if all values are missing. (Stata
      6 required.)

      . egen hiwage = record(exp(lwage)), by(id) order(year)
      . egen lowage = record(exp(lwage)), by(id) order(year) min


      Install egenmore with

      Code:
       
      ssc inst egenmore

      Comment


      • #4
        Thanks a lot to both of you!

        Comment

        Working...
        X