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

st: RE: -for- and its fortes


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: -for- and its fortes
Date   Wed, 24 Jul 2002 10:35:42 +0100

Ronan Conroy
> 
> on 23/7/02 4:46 PM, Nick Cox at [email protected] wrote:
> 
> > On the whole, I think nested problems are more common in
> > (my) Stata practice.
> 
> By contrast, -for- problems are commoner in my practice. 
> The ability of
> -for- to execute multiple commands means that I 
> occasionally use it to
> process lists containing a single item!
> 
> Consider these real examples:
> 
> . for var  b101_estwom b104_estboynf b105_estgincest
> b109_estprosecution:encode X, gen(X_) label(b1) \ move X_ X 
> \ drop X \
> rename X_ X 
> 
> This command 
> - encodes a variable, using a predefined mapping contained 
> in the value
> label `b1',
> - generates a new variable with the same name but with an 
> underscore added,
> - moves the new variable to be where the old variable is,
> - drops the old variable,
> - renames the new variable so that it has the original 
> variable name.
> 
> Because you can refer to the variable as X, it means that 
> this bit of code
> is faster to write for a single variable than spelling out 
> the name in full.
> 
> . for var  b101_estwom :encode X, gen(X_) label(b1) \ move 
> X_ X \ drop X \
> rename X_ X
> 
> Is a lot faster and neater than
> 
> . encode b101_estwom , gen(b101_estwom_) lab(b1)
> . move b101_estwom_  b101_estwom
> . drop b101_estwom 
> . rename b101_estwom_ b101_estwom
 
This is not the distinction made in my previous posting, 
nor the main theme of this thread. 

The burden of that was that for _parallel_ processing of 
_two or more_ lists, -for- can remain competitive. 

No disagreement that Ronan is here showing how -for- can 
be used for concise processing of tasks with single lists. 

But the equivalent with -foreach- is about equally easy: 

. for var  b101_estwom b104_estboynf b105_estgincest
b109_estprosecution:encode X, gen(X_) label(b1) \ move X_ X 
\ drop X \
rename X_ X 

becomes 

. foreach v of var b101_estwom b104_estboynf b105_estgincest 
b109_estprosecution { 
	encode `v', gen(`v'_) label(b1) 
	move `v'_ `v' 
	drop `v' 
	rename `v'_ `v' 
} 

Among various points that could be made: 

1. Whatever you know, which works, and which you find 
easy to type out, and to correct whenever you get it 
slightly wrong, is good. 

Beyond that, 

2. Being able to cram multiple commands into one line
with -for- appeals to the minimalist in a hurry, myself included. 
On the other hand, re-creating the logic of what was
done when reading a log file, or correcting it when 
it is not right the first time, are I think more 
difficult with -for- than with the equivalent -foreach-. 
The small discipline of putting separate commands on 
separate lines brings mostly benefit. 

3. The syntax of -for- is idiosyncratic. Much of what you 
learn is not transferable to other Stata commands, and vice
versa. The "arbitrary syntax" of \ as separator is awkward
(and confusing whenever that is needed for other meanings).  
In contrast, -foreach- and -forvalues- are much closer
to the mainstream of Stata thinking and style. This is 
equally important, albeit for slightly different reasons, 
to those learning Stata and/or using it only occasionally 
and to more advanced or more intensive users. 

Nick 
[email protected] 
*
*   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