Bookmark and Share

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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Looping over observations

From   Nick Cox <>
To   "''" <>
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

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-. 


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:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index