[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 |
st: RE: RE: Condition in a nested loop |

Date |
Mon, 24 Aug 2009 17:56:08 +1000 |

Dear Nick, Thanks again for your help. Especially the difference between if qualifier and the if command was new for me, but now it makes of course sense. Best regards Michael -----Original Message----- From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-statalist@hsphsun2.harvard.edu] On Behalf Of Nick Cox Sent: Friday, 21 August 2009 9:29 PM To: statalist@hsphsun2.harvard.edu Subject: st: RE: Condition in a nested loop Note that, as in another problem you recently posted, the inner -while- can be simplified to another -forval- loop. More importantly, see the FAQ on -if- to see that you are not understanding the way that the -if- command differs from the -if- qualifier: http://www.stata.com/support/faqs/lang/ifqualifier.html Many Stata users have travelled down that tricky path before you. The code you need sounds like forvalues j=1/32 { forval i=1/146 { replace timing_activities=`i' if activity==`j' & t`i' == 1 } } However, do you really need the loop over -activity-? Would this be enough? forval i=1/146 { replace timing_activities=`i' if t`i' == 1 } It also sounds as if you could do with a -reshape long- of your data. Nick n.j.cox@durham.ac.uk Michael Stuetzer I have again a looping problem, and would be very thankful for a hint. I have a time grid t1, t2 ... till t146, representing the number of months since the start of the venture creation process of a company. In this time grid t has the value of 1 if the company worked on a specific activity in the venture creation process. If the company did not work on this activity t equals zero. There are 32 (j) such activities, and therefore for each company there are 32 rows in the dataset. What I intend to do is to create a variable (timing_activities) which takes the value of the month in which for the first time the company has worked on this activity (e.g. 5 if the company worked in the 5th month for the first time on that activity). Obviously I need a more advanced loop for that job. It must loop over all 32 activities and over all months, while evaluating if the company for the first time worked on that activity j. Below is a first draft of such a loop, but unfortunately it produces only missings in the variable timing_activities. forvalues j=1/32 { local i=1 while `i'<=146 { if t`i'==1 { replace timing_activities=`i' if activity==`j' local i=`i'+1 } else { local i=`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: Condition in a nested loop***From:*Michael Stuetzer <michael.stuetzer@qut.edu.au>

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

- Prev by Date:
**Re: st: GPUs and statistical computing** - Next by Date:
**st: Query on how to re-link variables to their original name tag after mkmat and svmat manipulations** - Previous by thread:
**st: RE: Condition in a nested loop** - Next by thread:
**st: Randomized Quantile Residuals** - Index(es):

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