Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: foreach program


From   Scott Cunningham <[email protected]>
To   [email protected]
Subject   Re: st: foreach program
Date   Fri, 8 Sep 2006 11:21:32 -0400

Nick,

I'm going to work with your program and see if I can learn something about my problem.

The "not working" - I'm getting 0's for incarceration rates and incarceration levels for all the cells. There are people who are incarcerated in the data (that is, there are person weights for people with group quarter values equalling 200, meaning living in a correctional facility), meeting the criteria I specify as far as I can tell.

I will try your syntax, though. Between the two of them (my code and yours), I hope I can see where the logic in my code is breaking down.

thanks,

scott

On Sep 8, 2006, at 11:16 AM, n j cox wrote:


The report "not working" really is not at all transparent. From your posting, I infer anything from "Stata complained" through "results are all missing" to "results look implausible".

This code would be better as a

forval i = 1/`r(max)' {

loop. I can't see a bug, but creating variables to hold constants
and then -drop-ping them is not at all necessary.

Here's a second try:

egen group=group(fip race sex)
su group, meanonly

tempname itot ptot
forval i = 1/`r(max)' {
su perwt if inrange(age,15,19) & group==`i' & gqtyped==200, /// meanonly
scalar `itot' = r(sum)
su perwt if inrange(age,15,19) & group==`i', meanonly
scalar `ptot' = r(sum)
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'
}

This assumes that your variables being -replace-d exist before
the loop is entered. This probably misses your question, which may hinge
on the substantive logic, but you might pick up some Stata tips anyway.

Nick
[email protected]

Scott Cunningham

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/
*
*   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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index