On Aug 19, 2009, at 4:08 AM, Michael Stuetzer wrote:
I have a small problem with a nested loop and maybe you can give me
an advice. For a specific business starting activity I have data for
the beginning (BUSINESSPLAN_SP[odd]) and the end
(BUSINESSPLAN_SP[even]) of this activity. For instance the
entrepreneur worked on the business plan in the months 4 till 7 as
well as 16 till 25. I want to transfer the information into a time
grid. In this grid t1, t2, etc. stands for the respective month.
They should take the value of 1 if the entrepreneur worked on the
business plan on the point of time. I have here a first draft of the
loop.
set more off
local r=5
forvalues i=1/100 {
local j=1
local k=`j'+1
while `j'<=`r' {
replace t`i' = 1 BUSINESSPLAN_SP`j'<=`i' & BUSINESSPLAN_SP`k'>=`i'
local j=`j'+1
}
}
The problem is that this loop recognizes the first sequence (4 to
7), but not the second sequence (16 to 25). I suspect something is
wrong with the resetting of the local j (local j=`j'+1) and k.
I presume from your code that you have the variables BUSINESSPLAN_SP1-
BUSINESSPLAN_SP6, that you've verified that no one worked on their
business plan beyond month 100, and that you've already generated the
variables t1-t100 set to 0. Your code should read:
local r=5
forv i=1/100 {
local j=1
while `j'<=`r' {
local k=`j'+1 // moved from above
replace t`i' = 1 if BUSINESSPLAN_SP`j'<=`i' &
BUSINESSPLAN_SP`k'>=`i'
local j=`j'+2 // changed
}
}
Note that you need to increment k within your -while- loop, and you
need to increment j by 2 (instead of 1). If you haven't already, you
might also want to verify the following:
forv i = 1/5 {
ass BUSINESSPLAN_SP`i' <= BUSINESSPLAN_SP`=`i'+1'
}
-- Phil
*
* 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/