# Solution

It's tempting to do something like:

by person: egen meanDuration=mean(duration)

But this would be the mean across months, not spells. (For example, person five has two spells, one 18 months long and one 2 months long. The mean duration we want is 10. However, the mean across months is 16.4 since the 18 months with duration 18 count for more than the 2 months with duration 2.)

One easy way to get a mean across spells is to only consider one observation per spell. Since the start variable is one for just the first observation in each spell, you can do this with:

by person: egen meanDuration=mean(duration) if start

Depending on what you needed to do with this variable you might have to do some result spreading.

If you didn't have start defined already, you could create it with something like:

by person spell: gen start=(_n==1)

However, there's a egen function called tag() that does the same thing:

egen start=tag(person spell)

The tag() function gives a one to the first observation in each group defined by the variables it's given and a zero to all others. That first observation is thus "tagged" as the one to be included in calculations.

Last Revised: 1/26/2010