Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: Double loop nested within another double loop
From
Stephen Cranney <[email protected]>
To
[email protected]
Subject
st: Double loop nested within another double loop
Date
Wed, 29 Aug 2012 17:11:24 -0400
Hi Statalisters,
Forgive me if my description is long. It's a complicated setup.
I'm making a population microsimulation program where each observation
is an id/year/month combination, with the data ordered by year and
month. I recently constructed an initial loop to simulate the first
person's reproductive history. After the first person's reproductive
history is created, I use the expand command to create "offspring"
observations; these observations are then given characteristics based
on the parents'. My problem is that I have been unable to insert the
code I used to simulate postpartum infertility in the offspring, even
though I have been able to make it work for the parent.
This is the syntax for the first parent that does do what I want it to
(after a back-and-forth on Statalist), which is make a certain number
of subsequent months after a birth (simulated by an earlier rbinom
command) or miscarriage infertile (monthsofpostpartum= months of
postpartum infertility, etc.):
local N = _N
local monthsofpostpartum = 12
local monthsofpostmiscarriage= 7
forvalues i = 1/`N' {
forvalues j= 1/`monthsofpostpartum' {
local k = `i' + `j'
local s= (`monthsofpostmiscarriage'-`j') + `i' + 1
if conception[`i']==1 & miscarriage[`i']==0 replace conception= .p in `k'
if conception[`i']==1 & miscarriage[`i']==1 & `s'>1 replace
conception= .m in `s'
}
}
*Then I create offspring:
local N = _N
forvalues i = 1/`N' {
if conception[`i'] == 1 {
expand 2, gen(newvar`i')
**Here is long code that deletes replicated observations arising from
the expand 2 command. It also assigns characteristics such as age as
based on parents' age, etc.
*This is the code that simulates the conception of a child
replace conception= rbinomial(1, probabilityconceive) if conception==
.f & newvar`i'==1
*Finally, I want to simply make a loop-within-a-loop (within another
double loop), and stick the syntax below right below the syntax *above
in order to replicate infertile post-partum and post-miscarriage
months. However, when I stick it in it doesn't work (although *it
doesn't stop the code either--it just doesn't change anything), even
though it worked for the first generation before the loops got
*started.
local N = _N
local monthsofpostpartum = 12
local monthsofpostmiscarriage= 7
forvalues z = 1/`N' {
forvalues j= 1/`monthsofpostpartum' {
local k = `z' + `j'
local s= (`monthsofpostmiscarriage'-`j') + `z' + 1
if conception[`i']==1 & miscarriage[`i']==0 & newvar`i'==1 replace
conception= .p in `k'
if conception[`i']==1 & miscarriage[`i']==1 & `s'>1 & newvar`i'==1
replace conception= .m in `s'
}
}
*And then the code continues on for a while until it ends.
Apologies again for the length of this problem. Thanks in advance.
Stephen
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/