Dear all,

I am working with a matched employer-employee dataset from Brazil in which each observation represents a pair worker-firm for some period and I would like to know for how many different firms a given worker has worked.

In particular, the dataset is like

year week id_worker id_firm
2017 1 17 25
2017 2 17 41
2017 3 17 19
2017 3 17 25
2017 4 17 53
2017 5 17 19

I would like to create a variable like 'number_of_firms_week'

year week id_worker id_firm number_of_firms_week
2017 1 17 25 4
2017 2 17 41 4
2017 3 17 19 4
2017 3 17 25 4
2017 4 17 53 4
2017 5 17 19 4
where 'number_of_firms_week' is the variable for the number of different firms for which a given worker has worked (considering all periods).

Can you help me to find a solution for that?

Thank you very much!


Below I provide the code for importing the example dataset into Stata :

clear
input year week id_worker id_firm
2017 1 17 25
2017 2 17 41
2017 3 17 19
2017 3 17 25
2017 4 17 53
2017 5 17 19
end

Obs: I tried to use 'dataex' but I found it easier, in this case, to provide the 'importing code'.