Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: -for- versus -for each-


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: -for- versus -for each-
Date   Thu, 8 Apr 2004 10:14:11 +0100

Ian Watson
 
>   My problem is this: how can -for each- be  made to do 
> something like:
> 
>   for var v27-v32 \ num 1/6 : recode X (99=3), gen(item_Y)
> 

> I should add to my earlier posting, that I realise one can have a 
> -foreach- loop nested inside another -for each- loop, but I hope that 
> is not the answer to my query. If so, it means several lines of code
> instead of just one.

> And, if one fully exploits -for- (with a third placeholder Z), then it
> would mean even more lines of code in -for each- to replicate this
> behaviour.

Ian's specific example is akin to a problem posed by Dev Vencappa 
a few days ago and answered by Chris Wallace, Tom Steichen and 
Ernest Berkhout. 

-foreach- [sic] can be used in this problem in several different 
ways. As it happens, all are, I think, longer-winded than the -for- 
statement. However, the problem is not a _nested_ problem at 
all, and so does not call for nested loops. It is a problem 
of going through two lists in parallel. 

Code first, and then comment. 

0. 

local i = 0 
foreach v of var v27-v32 { 
	local i = `i' + 1 
	recode `v' 99=3, gen(item_`i') 
} 

1. You can cut a line from that: 

local i = 0 
foreach v of var v27-v32 { 
	recode `v' 99=3, gen(item_`++i') 
} 

2. 

foreach i of num 1/6 { 
	local j = `i' + 21 
	recode v`j' 99=3, gen(item_`i') 
} 

3. 

foreach i of num 27/32 { 
	recode v`i' 99=3, gen(item_`=`i'-21') 
} 

Instead of cycling through a numlist with -foreach-, 
-forval- offers yet other ways of doing it. Here's one. 

4. 

forval i = 27/32 { 
	recode v`i' 99=3, gen(item_`=`i'-21') 
} 

That's enough variations on a theme. In essence, the 
recipe is to step through one list with -foreach- or -forval-, 
and to step through another parallel list using some 
operation on a local macro. 

What seems to underlie Ian's question is some incredulity 
that -foreach- (introduced in Stata 7) can be so long-winded 
when -for- (with a much longer history, but now undocumented
as of Stata 8) offers -- for this kind of problem -- a compact 
solution. 

He is right to be puzzled. -for- could be used very concisely 
for problems like this, and was designed with precisely that 
aim in mind. In fact, I'd call this -for-'s biggest single advantage. 

-foreach- and -forvalues- in contrast offer only the means of 
stepping through one list at a time. They score much much better 
for nested problems, and in most other ways. 

The merits and demerits of -for- featured frequently on this 
list up to about two years ago. Some of those threads are echoed
directly or indirectly in a discussion in 
"Speaking Stata:  Problems with lists" Stata Journal
3(2): 185--202 (2003).           

As it also happens, a single call to -recode- can be used 
for the problem, but I take that to be incidental. 
            

Nick 
n.j.cox@durham.ac.uk 

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index