Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: RE: Overlapping Loops


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   st: RE: Overlapping Loops
Date   Wed, 18 Jan 2012 19:45:44 +0000

That's not a problem so far as I can see. Consider first time round the outer loop, so `i' = 1. 

The inner statements become 

gen ceo_photo1 = 0 if photo1==1

forvalues j=1/numberofpeople1 {
	replace ceo_photo1=1 if ceo == name`j'_photo1 & ceo_photo1==0
}

The thing that looks odd now is -numberofpeople1-. Tell us where you define it and what it is. 

Similarly, what are -numberofpeople2- ... -numberofpeople17-? 

Basically, it doesn't matter what they are, variables or scalars, as -forval- expects to see an actual number at that place. You could evaluate something on the fly, but that's a different technique. 

Nick 
[email protected] 


-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Bader Alhashel
Sent: 18 January 2012 19:27
To: statalist
Subject: st: Overlapping Loops

I am trying to write two over lapping "foreach" loops however I keep
getting an invalid syntax error. I don't know why but I am guessing it
is because of the local macro in the first loop is not recognized in
the second loop. This is the code I am trying to run.

forvalues i = 1/17 {
gen ceo_photo`i'=0 if photo`i'==1
forvalues j=1/numberofpeople`i' {
replace ceo_photo`i'=1 if ceo == name`j'_photo`i' & ceo_photo`i'==0
}
}

*
*   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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index