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

st: RE: RE: forvalues - irregular step sizes


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: forvalues - irregular step sizes
Date   Thu, 20 Feb 2003 16:24:51 -0000

Danielle H. Ferry 

> > I would like to run the following loop, where I have a series 
> > of variables
> > produced by a regression containing an interaction of 
> > state*popnLn*ttrend1.
> > "popnLn" & "ttrend1" are continuous variables, but "state" is 
> > a categorical
> > variable. Since the coding for "state" has irregular spacings 
> > (1, 5, 16, 20,
> > 23, etc.) the loop fails. Is there a way to make it continue 
> > onto the next
> > loop if, for example, a value of i=2 is not found?
> > 
> > forvalues i = 5(1)54{
> > 
> >     test _Ist`i'XpoXtt
> > 
> >     scalar f`i' = r(F)
> >     scalar pr`i' = r(p)
> > 
> > }

Nick Winter 

> Two options.  First, only loop over the values you want:
> 
> 	foreach i in 1 5 16 20 23 {
> 		<<do something>>
> 	}
> 
> Second, -capture- the attempt to test:
> 
> 	forvalues i = 5(1)54 {
> 	  capture test _Ist`i'XpoXtt
> 	  if !_rc {
> 	    scalar f`i' = r(F)
> 	    scalar pr`i' = r(p)
> 	    local list "`list' `i'"
> 	  }
> 	}
> 
> This code also leaves behind the macro list, which includes 
> a list of
> the valid codes, in case you need them for subsequent processing.

Two footnotes to Nick's posting: 

1. -levels- on SSC picks up the levels of a categorical 
variable. 

levels state, local(state) 
foreach s of local state { 
	test _Ist`s'XpoXtt
	... 
} 

2. There is an FAQ on this general problem 
at http://www.stata.com/support/faqs/data/for.html. 
It is showing its age -- it is phrased in terms of -for- 
and the -vallist- mentioned there is superseded by 
-levels- -- but the discussion may be of interest. 
	

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