 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: RE: Looping over observations

 From Nick Cox To "'statalist@hsphsun2.harvard.edu'" Subject st: RE: Looping over observations Date Thu, 17 Nov 2011 11:47:06 +0000

```That is correct.

-while- works on a single true-or-false flag, and if variables are specified then their values in the first observation are used to compute the flag. This is an FAQ and documented at

http://www.stata.com/support/faqs/lang/ifqualifier.html

If your problem really is a loop over observations, then loop from first to last. Don't use a -while- loop at all, but -forval-.

Nick
n.j.cox@durham.ac.uk

Albert Navarro

We have simulated an arbitrary number of observations of survival data, where each individual can have several episodes and each episode has its own generating process, from the first one until the fourth. From the fourth episode and until the time when a new episode occurs is before the follow-up time (which is a fixed parameter in our simulation) we want to generate new episodes for each individual. We have tried to do this with a ‘while’ loop, but it only takes into account the value for the first individual, and this is no option for us as we want this loop running for each individual. Any ideas on how to do this?

Our current code (part of the loop):

while (suma < max_time) {
replace ui = uniform() if (suma < 120 & it4 == 1)
gen t`i'c = ((log(1/ui)/0.009961534))^(1/ 1.42213732720679) if (suma < 120 & it4 == 1)
replace uj = uniform() if (suma < 120 & it4 == 1)
gen t`i'b = ((log(1/uj)/0.1717102))^(1/0. 85817155198598) if (suma < 120 & it4 == 1)
replace suma = `suma' + t`i'b
gen it`i' = 1 if (suma < 120 & it4 == 1)
replace it`i' = 0 if (t`i'c < t`i'b | suma > `time')
local i = `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/
```