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-assigning local macro values within loops after each iteration

From   Stephen Cranney <>
Subject   st: Re-assigning local macro values within loops after each iteration
Date   Mon, 20 Aug 2012 12:02:35 -0600

Hi statalisters:

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

Thanks in advance,

*   For searches and help try:

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