Announcement

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

  • How to find maximum and minimum on a panel data

    Hello. I have a dataset for several countries and years. I would like to know the code to find the year that corresponds to the maximum value for a variable?
    For example, I have the maximum of pol_risk=80. How can I find the year that corresponds to this value?
    Can someone help me with this issue?
    Thank you in advance.

  • #2
    Ana:
    you might be searching for something along the lines of the following toy-example:
    Code:
    . use "http://www.stata-press.com/data/r14/nlswork.dta", clear
    (National Longitudinal Survey.  Young Women 14-26 years of age in 1968)
    
    . xtsum hours
    
    Variable         |      Mean   Std. Dev.       Min        Max |    Observations
    -----------------+--------------------------------------------+----------------
    hours    overall |  36.55956   9.869623          1        168 |     N =   28467
             between |             7.846585          1       83.5 |     n =    4710
             within  |             7.520712  -2.154726   130.0596 | T-bar = 6.04395
    
    
    . tab year if hours==r(max)
    
      interview |
           year |      Freq.     Percent        Cum.
    ------------+-----------------------------------
             82 |          1      100.00      100.00
    ------------+-----------------------------------
          Total |          1      100.00
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Code:
      gen byte miss = missing(pol_risk)
      bys country miss (pol_risk) : gen byte max_pol_risk = _n == _N if miss == 0
      bys country pol_risk mis (max_pol_risk): replace max_pol_risk = max_pol_risk[_N] if miss == 0
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Hi Ana,

        As I understand your question there is exactly one year in which pol_risk has the maximum value (of 80 in this case), and you want to know what year that is. However, since you’re working with panel data, it might be that different countries have the maximum value in different years and there might not be just one answer. You could generate a new variable, say max_year, which have the year value whenever pol_risk is at the global maximum. The following example does just that:
        Code:
        clear
        input str9 country year pol_risk
        "Country 1" 2010 30
        "Country 1" 2011 46
        "Country 1" 2012 58
        "Country 1" 2013 80
        "Country 2" 2010 60
        "Country 2" 2011 67
        "Country 2" 2012 80
        "Country 2" 2013 75
        end
        
        qui sum pol_risk
        gen max_year = year if pol_risk == r(max)
        The result is then
        Code:
        . list, noobs
        
          +----------------------------------------+
          |   country   year   pol_risk   max_year |
          |----------------------------------------|
          | Country 1   2010         30          . |
          | Country 1   2011         46          . |
          | Country 1   2012         58          . |
          | Country 1   2013         80       2013 |
          | Country 2   2010         60          . |
          |----------------------------------------|
          | Country 2   2011         67          . |
          | Country 2   2012         80       2012 |
          | Country 2   2013         75          . |
          +----------------------------------------+

        Comment

        Working...
        X