Hey everyone,

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input long idpers byte(jan00 feb00 mar00 apr00)
 4101 1 1 1 1
 4102 4 4 4 4
 4103 . . . .
 4104 . . . .
 4105 . . . .
 5101 2 2 2 2
 5102 . . . .
 5103 . . . .
 5104 . . . .
 6101 2 2 2 5
11101 . . . .
11102 . . . .
11103 . . . .
13101 . . . .
13102 1 1 1 1
13103 . . . .
13104 . . . .
14101 2 2 2 2
14102 . . . .
21101 . . . .
25101 . . . .
26101 4 4 4 4
26102 4 4 4 4
27101 4 4 4 4
27102 1 1 1 1
27103 . . . .
27104 . . . .
27105 . . . .
28101 . 1 1 1
28102 2 2 2 2
28103 . . . .
28104 . . . .
30101 4 4 4 4
34101 1 1 1 1
34102 . . . .
35101 4 4 4 4
35102 . . . .
37101 4 4 4 4
39101 1 1 1 1
39102 4 4 4 4
39103 . . . .
39104 . . . .
42101 1 1 1 1
42102 1 1 1 1
42103 . . . .
42104 . . . .
45101 2 2 2 2
45102 . 1 1 1
45103 . . . .
49101 1 1 1 1
49102 1 1 1 1
55101 1 1 1 1
55102 4 4 4 4
55103 . . . .
56101 2 2 2 2
56102 1 1 1 1
56103 . . . .
56104 . . . .
57101 . . . .
57102 . . . .
57103 4 4 4 4
57104 . . . .
57105 4 4 4 4
57106 . . . .
59101 . . . .
59102 2 2 2 2
60101 4 4 4 4
60102 . . . .
61101 1 1 1 1
61102 2 2 2 2
61103 1 1 1 1
61104 . . . .
64101 4 4 4 4
68101 . . . .
68102 . . . .
70101 2 2 2 2
70102 . . . .
70103 . . . .
70104 1 1 1 1
73101 . . . .
73102 2 2 2 2
73103 . . . .
73104 . . . .
73105 . . . .
73106 . . . .
74101 1 1 1 1
74102 4 4 4 4
74103 4 4 4 4
74104 . . . .
74105 . . . .
74106 . . . .
75101 2 2 2 2
75102 1 1 1 1
75103 . . . .
75104 . . . .
75105 . . . .
80101 1 1 1 1
80102 . . . .
83101 . 1 1 1
85101 . . . .
end
label values idpers IDPERS
label values jan00 LABA
label values feb00 LABA
label values mar00 LABA
label values apr00 LABA
label def LABA 1 "full-time paid job (37 hours or more per week)", modify
label def LABA 2 "part-time paid job (1-36 hours per week)", modify
label def LABA 4 "inactive", modify
label def LABA 5 "unemployed or inactive", modify


I have a dataset on the emplyoment status of individuals until march 2019 (35520 observations and 233 variables). The interview is conducted on a yearly basis for their status in the last 12 months. Not all individuals respond each year ->missing values.
I would like to know how many months the individuals worked in the last 12 months in which they were observed.

My code:

foreach y in 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19{
gen months_worked`y'=0
foreach month in jan feb mar apr may jun jul aug sep oct nov dec{
replace months_worked`y'=months_worked`y'+1 if `month'`y'==1 | `month'`y'==2 if month==
}
}


I know in which year the individual was last observed which is why I just matched the data with the right year. Nevertheless, this did not work because it is possible that the individual has had his/her interview in september which would not be full 12 months (I would also need data from october, november and december fromt the year before).

I just want to use the last 12 months for each individual in which they were observed if they are working or not to create a variable "months_worked" (some were last observed in january 2004, some in march 2012 some in september 2018 =>varies for each individual depending on when the interview was conducted). I do not need the data from the months before that.

I would be glad to hear some ideas how I could do that.
Thank you!

Greetings,
Jenny