Hello Statalisters,

I have a dataset with each row equaling one team member (see below). These team members enter their team at different times (Member_Entry). Thus, team constellation changes over time (e.g. team A has 2 members until 1.1.2018, then have 5 members until 1.1.2019, then they have 6).
I want to calculate age diversity (using SD/mean) for each group constellation. That means, in the first and second row, the generated variable would be age diversity for team A with two members aged 30 and 50. In the third, forth, fifth row, the output would be age diversity for team A with five members aged 30,50,40,16 and 54. In the sixth row, it would be age diversity for team A with now six member aged 30,50,40,16,54 and 29.

I just could not find a code that enables this calculation, I suspect it should be some kind of loop but I am not sure. Can you help? Thank you already.

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input str1 Team byte Member str10 Member_Entry byte(Member_Age Team_Age_Diversity)
"A" 1 "1.1.2017"   30 .
"A" 2 "1.1.2017"   50 .
"A" 3 "1.1.2018"   40 .
"A" 4 "1.1.2018"   16 .
"A" 5 "1.1.2018"   54 .
"A" 6 "1.1.2019"   29 .
"B" 1 "13.01.2018" 87 .
"B" 2 "20.02.2018" 15 .
"B" 3 "20.02.2018" 11 .
"B" 4 "23.03.2019" 65 .
end