| |
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]
st: foreach program
I wrote a foreach loop to calculate incarceration rates that were
specific to race-age-sex-state for 1980, but it appears not to have
worked. I'm unable to see why this is not working. Can anyone shed
some light on why this program would not have calculated
incarceration rates? The dataset is the 1980 US Census (longform
survey, aka "5% sample"). The data is observations on persons, but
uses a sampling method that requires summing up the person weights
("perwt") to find an estimate of the population of interest.
Individuals were asked about housing units, and a person was
classified as living in a "group quarter" if they lived in a dwelling
unit that had at least 5 unrelated persons under the same roof.
Surveys also report the kind of group quarter. In 1980, information
in the variable "gqtyped" told whether the person lived in a
correctional facility - gqtyped=200 means the person lives in a
correctional facility. I attempted to create, in the following loop,
an incarceration rate for the 15-19 year old age cohort, according to
the demographics noted in the grouping fip (Census variable
indicating the United States state), race (black and white only in
this sample) and sex.
egen group=group(fip race sex)
su group, meanonly
foreach i of num 1/`r(max)' {
gen i=perwt if age>=15 & age<=19 & group==`i' & gqtyped==200
gen p=perwt if age>=15 & age<=19 & group==`i'
egen itot=sum(i)
egen ptot=sum(p)
replace ir_level=itot if age==15 & group==`i'
replace pop_level=ptot if age==15 & group==`i'
replace ir=itot/ptot if age==15 & group==`i'
drop i p itot ptot
}
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/