Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.

# Re: st: Loop-within-loop error while looping over observations for a microsimulation

 From Nick Cox To statalist@hsphsun2.harvard.edu Subject Re: st: Loop-within-loop error while looping over observations for a microsimulation Date Mon, 6 Aug 2012 15:35:55 +0100

```What is -monthsofpostpartum-? -foreach- and -forvalues- won't evaluate
for you what is not evaluated otherwise. Here -monthsofpostpartum-
needs to be replaced by some constant.

Nick

On Mon, Aug 6, 2012 at 3:29 PM, Stephen Cranney
<cranney.stephen@gmail.com> wrote:

> I'm trying to write a program that involves population microsimulation.
> Each individual has a column for year and month, and the data is sorted
> accordingly. I'm trying to loop through the observations with a stochastic
> binomial function to simulate a birth occurring, but am then trying to
> simulate the months of postpartum infertility so that it doesn't have the
> individual getting pregnant twice in two months. Postpartum infertility is
> indicated by .i and all of the fertile-time data is automatically indicated
> by .f My syntax is below, but I keep getting an invalid syntax error and
> can't figure out what I'm doing wrong.
>
>
>
> local N = _N
> forvalues i = 1/`N' {
>     forvalues j= 0/monthsofpostpartum {
>         replace childbirth= rbinomial(1, probabilityconceive) if childbirth== .f
>         replace childbirth[_n+j]== .i if childbirth [`i'] == 1
>         }
>     }
>
*
*   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/
```