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

RE: st: foreach command


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   RE: st: foreach command
Date   Mon, 16 Jun 2003 18:27:28 +0100

Fred Wolfe
 
> >Note that you lose any sense of what the dependent 
> variable is. Instead,
> >the log simply lists t-test after t-test. Is there any way 
> we can bring
> >back some marker of the command Stata repeats?
> 
> I have always thought Stata ought to have an -echo- option 
> to deal with 
> problem. But the best you can do is:
> 
> foreach var of varlist a b c {
> di"`var'"
> test `var',by(catvar)
> }
> 
> It is kind of a pain in the neck.

Fred is right: forget this when you need it, 
and you pretty much have to repeat what you 
did, in many circumstances. 

There is another side to this, however, namely those 
occasions in which you don't want a header, 
or you want something other than the raw syntax. 

That is, -for- provided a header, which 
optionally you can switch off. What 
it did was to echo the command. 

-foreach- gives you no header by 
default, but allows you to insert 
your own header. As that gives you, 
ultimately, more flexibility in terms of what you 
get Stata to say and how it is presented, I 
judge it on balance an improvement. 

Suppose, for example, you want a header which 
is in bold and shows not the variable name
but an informative variable label: 

foreach v of var <varlist> { 
	di as res "{bf:`: variable label `v''}" 
	...
} 

Fancier than you want for much interactive 
work, no doubt, but a pointer to things very 
difficult with -for-. 

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