Bookmark and Share

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.


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

Re: st: Terminating a loop when a certain value is reached.


From   Stephen Cranney <scranney@sas.upenn.edu>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Terminating a loop when a certain value is reached.
Date   Mon, 1 Apr 2013 14:26:49 -0400

Sorry, I should have responded to that back then. Basically, I'm
trying to make a microsimulation program in Stata because the other
microsimulation programs are very technical and require sophisticated
programming knowledge if you want to customize them to suit your
needs, whereas a microsimulation program in Stata would be much more
accessible and customizable. Hence my interest in keeping the code in
Stata, even though alternative coding languages would be more
efficient overall.

Thanks for the suggestion, I'll work on it and see if I can figure this out.

Stephen



On Mon, Apr 1, 2013 at 1:13 PM,  <njcoxstata@gmail.com> wrote:
> As I wrote some while ago, this is a rather surprising way to implement a demographic simulation. It would seem much simpler to program using matrices and vectors using Mata, rather than this way. But that's just a personal view.
>
> I'd suggest trying a very small number of iterations, less than 10, and -list-ing and -display-ing aggressively to see what's what.
>
> Nick
>
> On 1 Apr 2013, at 15:03, Stephen Cranney <scranney@sas.upenn.edu> wrote:
>
>> Apologies for the lack of detail. I forgot to add the `i'  but it
>> still isn't working (it's running, it just isn't doing what I want it
>> to, which is terminate the loop at a certain point). Below is the
>> relevant portion of my code. As you can see, I have a large number so
>> that it just keeps running indefinitely (which is why I want to be
>> able to make a break point trigger). Once a "birth" is created a
>> series of mirror observations is created for the offspring, and its
>> generationnum value is one greater than its parent. What I'm trying to
>> do is run this until generationnum equals a certain number (decided at
>> the beginning of the ado file), but I don't see why the continue,
>> break code at the end performs this function.
>>
>> forvalues i = 1/999999999999999999999999 {
>>        if births[`i'] == 1 {
>>        expand 2, gen(newvar`i
>>        egen Xnewvar= rowtotal (newvar*)
>>        drop if Xnewvar>1
>>        drop if Xnewvar== 0 & generationnum!= 1
>>        drop Xnewvar
>>        replace generationnum=generationnum[`i']+1 if newvar`i'==1
>>         if generationnum==2 {
>>        continue, break
>>          }
>> ....
>>
>> Thanks again,
>>
>> Stephen
>>
>>
>>
>> On Mon, Apr 1, 2013 at 8:30 AM, Nick Cox <njcoxstata@gmail.com> wrote:
>>> Not much detail here.
>>>
>>> One possibility is that you are forgetting that
>>>
>>> if generationnum == 2
>>>
>>> can only mean
>>>
>>> if generationnum[1] == 2
>>>
>>> Nick
>>>
>>>
>>> On 1 Apr 2013, at 13:03, Stephen Cranney <scranney@sas.upenn.edu> wrote:
>>>
>>>> Hi all, I'm trying to figure out how to break out of a loop once a
>>>> variable is changed to a certain value. In each iteration, the value
>>>> is modified according to the inputs within the loop. I've tried to use
>>>> an "if" command:
>>>>
>>>> if generationnum==2 {
>>>>       continue, break
>>>>       }
>>>>
>>>> but it's still not terminating the loop. Does anybody know of a
>>>> command to terminate a loop once a specified calculated number is
>>>> reached?
>>> *
>>> *   For searches and help try:
>>> *   http://www.stata.com/help.cgi?search
>>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>>> *   http://www.ats.ucla.edu/stat/stata/
>>
>>
>>
>> --
>> Stephen Cranney
>>
>> PhD Candidate
>> Graduate Group in Demography
>> University of Pennsylvania
>> *
>> *   For searches and help try:
>> *   http://www.stata.com/help.cgi?search
>> *   http://www.stata.com/support/faqs/resources/statalist-faq/
>> *   http://www.ats.ucla.edu/stat/stata/
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/



-- 
Stephen Cranney

PhD Candidate
Graduate Group in Demography
University of Pennsylvania
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index