Hi all,
For information purposes: I am using STATA/MP version 15.1.
I had a question regarding (nested) loops in Stata to calculate the Blau Index Nationality Diversity within a team.
However, I am not able to figure out how to write my loops to handle the data correctly.
I want to create a nationality diversity variable for each year/month... for the present team members.
In other words if member was present (year`i'== 1) than calculate the nationality diversity.
But, I am not sure how to adapt the following code:
I used forvalues and select a value greater than the total variety of nationalities in my dataset, as I am not able to figure out how to integrate' foreach var' well due to my lack of knowledge.
I based myself mainly on the following discussion post, post #2:
https://www.statalist.org/forums/forum/general-stata-discussion/general/1495309-blau-index
Would someone be able to help me with this issue/ guide me in the right direction? If there is a better/easier solution than this, I am very happy to learn.
If any more info is needed/better explanation, let me know!
Thanks in advance!
Best regards,
Laura
-------------------------------------------------------------------------------------------
Below an excerpt of my data using dataex:
BvdIdNumber = company number
DMUci = employee number
year... = dummy variable that tells me if member was present year 0,1...,16
Nationality = nationality of team member
I used the following code when only accounting for the year the company started/failed (so without looping) and it seems to work, but might not be perfect.
For information purposes: I am using STATA/MP version 15.1.
I had a question regarding (nested) loops in Stata to calculate the Blau Index Nationality Diversity within a team.
However, I am not able to figure out how to write my loops to handle the data correctly.
I want to create a nationality diversity variable for each year/month... for the present team members.
In other words if member was present (year`i'== 1) than calculate the nationality diversity.
But, I am not sure how to adapt the following code:
Code:
.forvalues i= 0/5 {
tabulate Nationality if year`i'== 1, generate(Nationality_Year`i'_)
}
foreach var of varlist Nationality_Year* {
bysort BvdIdNumber: egen sum_`var' =total(`var')
}
drop Nationality_Year*
forvalues i= 0/5 {
egen total_Nationality_Year`i' = rowtotal(sum_Nationality_Year`i'_*)
}
forvalues j = 1/150 {
forvalues i = 0/5 {
gen p_year`i'_`j' = (sum_Nationality_Year`i'_`j'/ total_Nationality_Year`i')^2
*drop sum_Nationality_Year*
*drop total_Nationality_Year*
}
}
forvalues i = 0/5 {
egen sum_squares_year`i'= rowtotal( p_year`i'_`j')
egen sum_squares_year`i'= rowtotal( p_year`i'_`j')
egen sum_squares_year`i'= rowtotal( p_year`i'_`j')
egen sum_squares_year`i'= rowtotal( p_year`i'_`j')
egen sum_squares_year`i'= rowtotal( p_year`i'_`j')
egen sum_squares_year`i'= rowtotal( p_year`i'_`j')
}
*drop p_*
forvalues j = 1/150 {
forvalues i = 0/5 {
gen Nationality_Year`i' = 1 - sum_squares_year`i'
gen Nationality_Year`i'= 1 - sum_squares_year`i'
gen Nationality_Year`i'= 1 - sum_squares_year`i'
gen Nationality_Year`i'= 1 - sum_squares_year`i'
gen Nationality_Year`i'= 1 - sum_squares_year`i'
gen Nationality_Year`i' = 1 - sum_squares_year`i'
*drop sum_squares_*
}
}
I based myself mainly on the following discussion post, post #2:
https://www.statalist.org/forums/forum/general-stata-discussion/general/1495309-blau-index
Would someone be able to help me with this issue/ guide me in the right direction? If there is a better/easier solution than this, I am very happy to learn.
If any more info is needed/better explanation, let me know!
Thanks in advance!
Best regards,
Laura
-------------------------------------------------------------------------------------------
Below an excerpt of my data using dataex:
BvdIdNumber = company number
DMUci = employee number
year... = dummy variable that tells me if member was present year 0,1...,16
Nationality = nationality of team member
Code:
* Example generated by -dataex-. To install: ssc install dataex clear input str16 BvdIdNumber str10 DMUci str27 Nationality float(year0 year1 year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 year12 year13 year14 year15 year16) "AT9070422953" "P300272480" "Austria" 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "AT9070422953" "P301627755" "Austria" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0501515635" "P144431731" "Belgium" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0501515635" "P238808799" "Belgium" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0501562254" "P044740365" "Belgium" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0501562254" "P044740365" "Belgium" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0501562254" "P044740365" "Belgium" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0501562254" "P123993699" "Belgium" 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0501562254" "P123993699" "Belgium" 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0501562254" "P123993699" "Belgium" 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0505804718" "P103923129" "Germany" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0505804718" "P103923129" "Germany" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0505804718" "P238328579" "Japan" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0505804718" "P238328579" "Japan" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0505804718" "P246173452" "Japan" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0505804718" "P246173452" "Japan" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0518917237" "P042037775" "Belgium" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0518917237" "P042037775" "Belgium" 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 "BE0518917237" "P044189380" "Belgium" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0536436625" "P009351640" "Canada" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0536436625" "P009351640" "Canada" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0536436625" "P009351700" "United Kingdom" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0536436625" "P009351700" "United Kingdom" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0536436625" "P014424187" "Canada" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0536436625" "P014424187" "Canada" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0536436625" "P048688796" "United Kingdom" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0536436625" "P048688796" "United Kingdom" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0555980046" "P039659380" "United Kingdom" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0555980046" "P076509994" "France" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0656918246" "P141888237" "Bulgaria" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0656918246" "P141888237" "Bulgaria" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0656918246" "P349820174" "Bulgaria" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0656918246" "P349820174" "Bulgaria" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0657890622" "P077578372" "France" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0657890622" "P350651532" "France" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0664799594" "P006533911" "Greece" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0664799594" "P006533913" "United Kingdom" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0664799594" "P263565204" "Greece" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0808687713" "P043459088" "Denmark" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0808687713" "P043459088" "Denmark" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0808687713" "P043459088" "Denmark" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0808687713" "P043459088" "Denmark" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0808687713" "P043459088" "Denmark" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0808687713" "P068611593" "Denmark" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0808687713" "P068611593" "Denmark" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0808687713" "P068611593" "Denmark" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0808687713" "P068611593" "Denmark" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0808687713" "P068611593" "Denmark" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0810603264" "P123366320" "Belgium" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0810603264" "P123366320" "Belgium" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0810603264" "P123574091" "Belgium" 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0810603264" "P123574091" "Belgium" 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0812211682" "P046290225" "United Kingdom" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0812211682" "P046290228" "United Kingdom" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0818085924" "P001050215" "Germany" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0818085924" "P001050215" "Germany" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0818085924" "P001050215" "Germany" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0818085924" "P001050215" "Germany" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0818085924" "P001068111" "Germany" 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 "BE0818085924" "P001068111" "Germany" 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 "BE0818085924" "P194531680" "Germany" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0818085924" "P194531680" "Germany" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0818085924" "P194531680" "Germany" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0818085924" "P194531680" "Germany" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0822663730" "P010015992" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0822663730" "P010015992" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0822663730" "P194256668" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0822663730" "P194256668" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0822663730" "P210520024" "United States" 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 "BE0822663730" "P210520024" "United States" 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 "BE0822663730" "P300158435" "United Kingdom" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0822663730" "P300158435" "United Kingdom" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0822663730" "P415739933" "United States" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0822663730" "P415739933" "United States" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0822663730" "P469602315" "United States" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0822663730" "P469602315" "United States" 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 "BE0822726086" "P049564102" "Cyprus" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0822726086" "P049564102" "Cyprus" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0822726086" "P049564102" "Cyprus" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0822726086" "P063938116" "Cyprus" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0822726086" "P063938116" "Cyprus" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0822726086" "P063938116" "Cyprus" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0825343207" "P005145231" "United Kingdom" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0825343207" "P005145231" "United Kingdom" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0825343207" "P005145231" "United Kingdom" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0825343207" "P041721435" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0825343207" "P041721435" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0825343207" "P041721435" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0830917737" "P005223488" "France" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0830917737" "P005223488" "France" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0830917737" "P007073447" "France" 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 "BE0830917737" "P007073447" "France" 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 "BE0835023906" "P011838447" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0835023906" "P011838447" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0835023906" "P019045531" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0835023906" "P019045531" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0835023906" "P116393668" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0835023906" "P116393668" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 "BE0835023906" "P116393668" "United States" 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 end
I used the following code when only accounting for the year the company started/failed (so without looping) and it seems to work, but might not be perfect.
Code:
// Year of founding
tabulate Nationality if FoundingTeam == 1, generate(nationality_founding_)
foreach var of varlist nationality_founding_* {
bysort BvdIdNumber: egen sum`var' =total(`var')
}
drop nationality_*
egen totalnationality_founding = rowtotal(sumnationality_founding_*)
forvalues i = 1/177 {
gen p_founding_`i' = (sumnationality_founding_`i'/ totalnationality_founding )^2
}
drop sumnationality*
drop totalnationality*
egen sum_squares_founding = rowtotal(p_founding_*)
drop p_*
gen Nationality_founding = 1 - sum_squares_founding
drop sum_squares_*
// Year of failure
tabulate Nationality if FailureTeam == 1, generate(nationality_failure_)
foreach var of varlist nationality_failure_* {
bysort BvdIdNumber: egen sum`var' =total(`var')
}
drop nationality_*
egen totalnationality_failure = rowtotal(sumnationality_failure_*)
forvalues i = 1/177 {
gen p_failure_`i' = (sumnationality_failure_`i'/ totalnationality_failure )^2
}
drop sumnationality*
drop totalnationality*
egen sum_squares_failure = rowtotal(p_failure_*)
drop p_*
gen Nationality_failure = 1 - sum_squares_failure
drop sum_squares_*

Comment