Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: Foreach nested in Foreach


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: Foreach nested in Foreach
Date   Wed, 18 Oct 2006 17:18:27 +0100

Philipp has identified the main initial problem, 
but his code still won't work. 

The first problem is that 

forvalues i of num 1/`r(max)' { 

is illegal. You probably want 

forvalues i = 1/`r(max)' { 

The second problem is with 

local k=1
while `k'<=`year_count' if bvd_id_number==`l' {
	replace gamo=gamo + (tax[_`l']-tax[_`k'])*ratio_assets[_`k']
	local k=`k'+1
}

The -if- condition is in the wrong place, so
this is more likely. I assume `year_count' is 
defined upstream. 

local k=1
while `k'<=`year_count' {
	replace gamo=gamo + (tax[_`l']-tax[_`k'])*ratio_assets[_`k'] if bvd_id_number==`l' 
	local ++k
}

Nick 
[email protected] 

Philipp Rehm
 
> I think you get the delimiters wrong, i.e. they are missing 
> in some of 
> the lines. You have to either - delimit cr - an remove all of them or 
> add them to the end of each line.
> 
> Also, the first -foreach- should probably be a -forvalues-.
> 
> So, maybe something along these lines (untested):
> 
> #delimit cr
> su group, meanonly
> forvalues i of num 1/`r(max)' {
> levelsof bvd_id_number, local(levels)
> foreach l of local levels {
> local k=1
> while `k'<=`year_count' if bvd_id_number==`l' {
> replace gamo=gamo + (tax[_`l']-tax[_`k'])*ratio_assets[_`k']
> local k=`k'+1
> }
> } 	
> }

> Socrates Mokkas wrote:
> > Hi All,
> > 
> > I have the do file below and apart from other problems tah 
> one I have is that Stata says that "option foreach is not 
> allowed". Any suggestions? Can you identify other problems 
> with the code?
> > 
> > #delimit;
> > su group, meanonly
> > foreach i of num 1/`r(max)' {;
> > levelsof bvd_id_number, local(levels)
> > foreach l of local levels {;
> > local k=1
> > while `k'<=`year_count' if bvd_id_number==`l' {;
> > replace gamo=gamo + (tax[_`l']-tax[_`k'])*ratio_assets[_`k']
> > local k=`k'+1
> > };
> > };	
> > };

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



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