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 at the end of May, and its replacement, statalist.org is already up and running.


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

Re: st: Re-assigning local macro values within loops after each iteration


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Re-assigning local macro values within loops after each iteration
Date   Mon, 20 Aug 2012 19:36:52 +0100

The Catch-22 here is that telling us part of your problem and showing
us part of your code is not enough for anyone to get a complete
understanding of your problem, while telling us all of your problem
and showing us all of your code would not really help, as you would be
unlikely to get anyone to invest time in understanding it all.

That said, there is an important misunderstanding here. The effect of

local N = _N
forvalues i = 1/`N' {

is to set up a loop with a particular upper limit that is fixed when
the -forvalues- loop starts. Changing the number of observations after
the loop has begun makes no difference to that, unless you change the
number in such a way that the loop terminates for other reasons. In
effect, you are "compiling" the number of observations into your code
when the loop starts, so that's fixed code.

My suggestion is that you may find it easier to set this up as a -while- loop.

local notdone = 1
local N = _N

while `notdone' {



}

with code within the loop to change

local notdone = 0

as appropriate. That certainly permits the number of observations to change.

That said, I am curious about the line

expand 2, gen(newvar`i')

which doubles the number of observations, just because of one
childbirth. Sounds like something out of the Alien* films to me, but
nothing like human demography? Shouldn't that -expand- be limited to
expansion of certain observations?

Nick

On Mon, Aug 20, 2012 at 7:02 PM, Stephen Cranney
<cranney.stephen@gmail.com> wrote:

> I'm running a loop that loops over observations and generates new
> observations with expand when one of the observations has a certain
> value for one of the variables. (It's a population simulation that is
> simulating a new person when each childbirth=1, each observation is a
> month/year combination for a specific id). I initially set the macro
> at N= _N, but I want to re-set the macro every time it loops, this way
> the simulation can run past the second generation. As it is, it saves
> the initial _N value and terminates when that value is reached, but I
> would like to re-assign the macro so that it equals the value of _N
> after each expansion, allowing it to run into multiple generations
> until it terminates because of other parameters I've imposed.
>
> I have tried to place the macro within the loop to see if it will
> re-do it, but it does not seem to be working. My syntax so far:
>
> local N = _N
> forvalues i = 1/`N' {
>         local N = _N
>         if childbirth[`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
> ** A lot of code that differentiates the new child's demographic
> characteristics from the parent's
>     }
> }
>
*
*   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/


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