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

st: RE: forvalues - irregular step sizes


From   "Nick Winter" <[email protected]>
To   <[email protected]>
Subject   st: RE: forvalues - irregular step sizes
Date   Thu, 20 Feb 2003 10:50:11 -0500

> -----Original Message-----
> From: Danielle H. Ferry [mailto:[email protected]] 
> Sent: Thursday, February 20, 2003 10:39 AM
> To: StataList
> Subject: st: forvalues - irregular step sizes
> 
> 
> Hi,
> 
> 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)
> 
> }
> 
> Any help would be greatly appreciated!

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.

Nick WInter
*
*   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