Statalist


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

st: RE: Condition in a nested loop


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Condition in a nested loop
Date   Fri, 21 Aug 2009 12:29:24 +0100

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/



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