# st: Characteristics of median observation

 From John Hund To statalist@hsphsun2.harvard.edu Subject st: Characteristics of median observation Date Mon, 17 Aug 2009 18:32:16 -0500

I am trying to calculate some characteristics of the median observation within groups defined by several variables. As an example, I have data (sales and ages) on firms by year in different industries. I would like to find the age of the firm with the median value on sales for each year and industry. That is:
```
AGE     SALES   IND    YEAR ID
2     1.04   3339    1991 1
3     1.75   3339    1991 2
3     3.08   3339    1991 3
31     .496   3339    1991 4
42     .546   3339    1991 5
42      1.5   3339    1991 6
5        .   3411    1991 7
8     .584   3411    1991 8
30     .491   3411    1991 9
19     .944   3411    1991 10
20     .692   3411    1991 11
28     1.81   3411    1991 12
29     .601   3411    1991 13
32     .509   3411    1991 14
42     .938   3411    1991 15
42     .886   3411    1991 16

```
The median sales for industry 3411 in 1991 is .692; I'd like a new variable, say medAge that contains 20. The problem comes from groups with an even number of members like industry 3339...here I'm trying to get the average of the two observations that make up the median calculation. That is, I need medAge to contain (42+2)/2 = 22 from the observations with sales of 1.04 and 1.5. If groups only contained odd numbers of observations, I could merge back into on the dataset using the median by group, but this obviously won't work for groups with even numbers of observations. Is this something where I should use forvalues within each group...or is there a faster way?
```
John
=================================
John Hund
Visiting Assistant Professor