Dear Statalist users,
We are using TIMSS panel data, where each student is one observation, with one StudentID, and each student is assigned to a classroom with a number of students and a common ClassID. Using foreach, we aim to compute the share of girls in the classroom for each student observation while excluding the student her-/himself. Hence, we have computed these shares separately for each class (ClassID) and for the two sexes (StudentSex == 1 for girls and StudentSex == 0 for boys). However, we are trying different configurations, we always end up with the same shares for all boys and all girls respectively, with no difference between classes. Either the first classroom composition or the last classroom composition is used by Stata throughout, depending on our configuration. How do we compute these values (shares) separately for each class?
This is our code:
Thank you in advance!
We are using TIMSS panel data, where each student is one observation, with one StudentID, and each student is assigned to a classroom with a number of students and a common ClassID. Using foreach, we aim to compute the share of girls in the classroom for each student observation while excluding the student her-/himself. Hence, we have computed these shares separately for each class (ClassID) and for the two sexes (StudentSex == 1 for girls and StudentSex == 0 for boys). However, we are trying different configurations, we always end up with the same shares for all boys and all girls respectively, with no difference between classes. Either the first classroom composition or the last classroom composition is used by Stata throughout, depending on our configuration. How do we compute these values (shares) separately for each class?
This is our code:
HTML Code:
gen GirlShare = . levelsof ClassID foreach class in `r(levels)' { count if (StudentSex == 1 & ClassID == `class') gen femalesInClass = `r(N)' count if ClassID == `class' gen classSize = `r(N)' if ClassID == `class' { foreach student in StudentID { gen femalesInClassExcl = femalesInClass - StudentSex replace GirlShare = ( femalesInClassExcl / ( classSize - 1) ) drop femalesInClassExcl } } drop femalesInClass drop classSize }
Thank you in advance!
Comment