Statalist The Stata Listserver

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

Re: st: Re: foreach syntax

From   Maarten buis <>
Subject   Re: st: Re: foreach syntax
Date   Wed, 21 Feb 2007 13:43:56 +0000 (GMT)

--- Paul Fenner [] wrote:
> The following works in Stata9:
> local x = "lr1-ul6"
> foreach i of local x{
> 	su `i'
> }
> but
> local x = "lr1-ul6"
> foreach i of local x{
> 	ttest `i',by(gender)
> }
> with error r(103) too many variables specified.
In this case Stata will loop over the different 
elements in the local x. The local x contains one 
element: lr1-ul6, so the loop will go through only 
one iteration and the local i will in that 
iteration contain lr1-ul6. So in the first loop the 
first and only iteration will read -su lr1-ul6-, 
which is legal Stata syntax, and in the second loop 
the first and only iteration will read 
-ttest lr1-ul6, by(gender)-, which is illegal Stata 
syntax since you specified too many variables as 
the error message showed. What you probably want is 
the following:

local x = "lr1-ul6"
su `i'

foreach i of varlist `x'{
ttest `i',by(gender)

Hope this helps,

Maarten L. Buis
Department of Social Research Methodology 
Vrije Universiteit Amsterdam 
Boelelaan 1081 
1081 HV Amsterdam 
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434 

+31 20 5986715

The all-new Yahoo! Mail goes wherever you go - free your email address from your Internet provider.
*   For searches and help try:

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