Hi Everyone,
I am using Stata 12 and have a dataset with 539,168 observations (8 years * 58 counties * 83 ages (18 to 100) * 2 genders * 7 races).
The years are 2007 to 2014. I would like to copy the 2014 values for the variable Population to the other seven years by county, age, gender and race. The code works below, but takes a very long time because it summarizes every 2014 observation (all 67,396 observations). Is there better code someone could recommend (e.g., bysort Year CountyCode Age sex RaceCode...)?
Thanks,
Brent Fulton
UC Berkeley
*8 years * 58 counties * 83 ages (18 to 100) * 2 genders * 7 races
*539168
di 8*58*83*2*7
des, short
*58 counties * 83 ages (18 to 100) * 2 genders * 7 races
*67396
di 58*83*2*7
clonevar pop14=Population
foreach c of numlist 1/58{
di "c is now `c' *************************************"
di "`c(current_date)' `c(current_time)'"
foreach a of numlist 18/100{
foreach g of numlist 1/2{
foreach r of numlist 1/7{
di "c is `c', a is `a', g is `g', r is `r'"
su Population if CountyCode==`c' & Age==`a' & sex==`g' & RaceCode==`r' & Year==2014
replace pop14=r(mean) if CountyCode==`c' & Age==`a' & sex==`g' & RaceCode==`r' & Year!=2014
}
}
}
}
I am using Stata 12 and have a dataset with 539,168 observations (8 years * 58 counties * 83 ages (18 to 100) * 2 genders * 7 races).
The years are 2007 to 2014. I would like to copy the 2014 values for the variable Population to the other seven years by county, age, gender and race. The code works below, but takes a very long time because it summarizes every 2014 observation (all 67,396 observations). Is there better code someone could recommend (e.g., bysort Year CountyCode Age sex RaceCode...)?
Thanks,
Brent Fulton
UC Berkeley
*8 years * 58 counties * 83 ages (18 to 100) * 2 genders * 7 races
*539168
di 8*58*83*2*7
des, short
*58 counties * 83 ages (18 to 100) * 2 genders * 7 races
*67396
di 58*83*2*7
clonevar pop14=Population
foreach c of numlist 1/58{
di "c is now `c' *************************************"
di "`c(current_date)' `c(current_time)'"
foreach a of numlist 18/100{
foreach g of numlist 1/2{
foreach r of numlist 1/7{
di "c is `c', a is `a', g is `g', r is `r'"
su Population if CountyCode==`c' & Age==`a' & sex==`g' & RaceCode==`r' & Year==2014
replace pop14=r(mean) if CountyCode==`c' & Age==`a' & sex==`g' & RaceCode==`r' & Year!=2014
}
}
}
}
Comment