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

From |
Michael Stuetzer <michael.stuetzer@qut.edu.au> |

To |
"statalist@hsphsun2.harvard.edu" <statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: Resetting the local in a nested loop |

Date |
Thu, 20 Aug 2009 15:38:45 +1000 |

Thanks guys. The code is working well. -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: Thursday, 20 August 2009 3:36 AM To: statalist@hsphsun2.harvard.edu Subject: RE: st: Resetting the local in a nested loop Note that we can polish Phil's excellent reply a little further. forv i=1/100 { forv j = 1(2)5 { local k=`j'+1 replace t`i' = 1 /// if BUSINESSPLAN_SP`j'<=`i' & BUSINESSPLAN_SP`k'>=`i' } } Nick n.j.cox@durham.ac.uk Phil Schumm 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' } * * 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/ * * 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/

**References**:**st: Resetting the local in a nested loop***From:*Michael Stuetzer <michael.stuetzer@qut.edu.au>

**Re: st: Resetting the local in a nested loop***From:*Phil Schumm <pschumm@uchicago.edu>

**RE: st: Resetting the local in a nested loop***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**st: Use of Stata on netbooks** - Next by Date:
**RE: st: Stata 11 Random Effects--Std. Errors** - Previous by thread:
**RE: st: Resetting the local in a nested loop** - Next by thread:
**st: How to estimat the parameters of a Tobit model with friction using stata** - Index(es):

© Copyright 1996–2016 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |