Hello everyone,

I have trouble working with duration data.

I am working with spell-type data. The data is a panel that contains the employment status in each month. I grouped (concat) the 12 variables that correspond to the status observed each month to obtain the variable Empl_status which allows me to see the trajectory of the individual for each year. So, for each month individual can be either employed (=1), unemployed(=2) or nonparticipating(=3). For example the individual personid==1 is employed from January to August 2010 (8 months) and exit to employment and become unemployed at September 2008 to May 2011 (9 months). For each individual, I just want the first two durations.
Code:
clear all
input double personid year begdat str12 Empl_status spellE_1 spellE_2 spellU_1 spellU_2 
      1 2010  "111111112222"  8 0 9 0
      1 2011 "222221111111" . . . .
      1 2012 "111111111111" . . . .
      1 2013 "111122222222" . . . .
      2 2010 "222222222222" 0 17 0 14
      2 2011 "221111111111" . . . . 
      2 2012 "111111122222" . . . .
      2 2013 "111111333333" . . . .
end
Where Empl_status denotes employment status each month.

I want to calculate spellE_1 spellE_2 spellU_1 spellU_2.

spellE_1 = total employment duration if the spell begin in employment. The beginning correspond to the first time the individual was observed (i.e 2010). This variable measures duration on employment until the first unemployment after employment.

spellE_2= measures total duration on employment after duration on unemployment (i.e the individual begins in unemployment status)

spellU_1= measures total duration on unemployment after duration on employment (i.e the individual begins in employment status)

spellU_2 =measures total duration on employment until the first duration on employment after unemployment(i.e the individual begins in unemployment status)

Could you please help me to write a code for such durations?

Thanks for your time.