Announcement

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

  • One more question about time-series: Finding a maximum over a subset

    Sorry to bug you guys again, but I have one more question about time-series.

    Suppose each of my observations corresponds to a person, and I have three variables:
    b = date of birth
    d = date of death
    h = height in cm

    For each person, I want to define a variable m, whose value is the height of the tallest person alive at the same time.

    So for example, suppose I have these data:

    b d h
    1 3 180
    2 4 170
    4 5 160
    6 7 150

    Then I want to define m such that:

    b d h m
    1 3 180 180
    2 4 170 180
    4 5 160 170
    6 7 150 150

    Does anyone know how to do this?

  • #2
    You can loop over observations:

    Code:
    clear
    set more off
    
    *----- example data -----
    
    input ///
    id birth death height mheight
    1 4 5 170 170
    2 1 3 180 180
    3 6 7 150 150
    4 2 4 170 180
    5 10 15 190 .
    6 12 14 200 .
    end
    
    list
    
    *----- what you want -----
    
    gen flag = .
    gen mheight2 = .
    forvalues i = 1/`=_N' {
        
        quietly {
        
        // flag cohort fellows of observation `i'
        replace flag = inrange(birth[`i'], birth, death) |  ///
                       inrange(death[`i'], birth, death) | ///
                       (birth[`i'] <= birth & death[`i'] >= death)
                      
        
        // compute the max height
        summarize height if flag, meanonly
        
        // save the max for observation `i'
        replace mheight2 = r(max) in `i'
        
        }
    
    }
    
    list, sep(0)
    I added a case (two observations) that was not present in your example data. Namely, when observation i is born before another person and dies after that same person. That entails a third condition to be checked for.
    Last edited by Roberto Ferrer; 22 Sep 2014, 23:54.
    You should:

    1. Read the FAQ carefully.

    2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

    3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

    4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

    Comment


    • #3
      Thanks, Roberto!

      Comment

      Working...
      X