Statalist


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

st: RE: -forval- with -inequal7-


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: -forval- with -inequal7-
Date   Mon, 14 Jul 2008 18:44:50 +0100

-inequal7- is blameless here. 

Your -forval- syntax, at least as given here, is incorrect. 

forval i = 19901/20071 { 

You must have an equals sign. See the help on -forvalues-, including all
the examples given. 

Similarly, your first example with -foreach- has incorrect syntax. 

You cannot mix and match the two syntaxes. Also, guessing at syntax you
would like or imagine might work is all too likely just to cause
frustration. 

Given the gaps, you might be better advised to check out solutions
documented at 

FAQ     . . . . . . . . . . Making foreach go through all values of a
variable
        8/05    Is there a way to tell Stata to try all values of a
                particular variable in a foreach statement without
                specifying them?
                http://www.stata.com/support/faqs/data/foreach.html

Nick 
[email protected] 

Lola Jackson

I am using -forval- to run -inequal7- by date and export selected
results. The problem is that I get the error message:

'0' invalid obs no
r(198);

I think the reason is that by dates are not 'consecutive' in that I have
19901, 19902, 19903, 19904, then 19911, 19912, etc. (As there are
quarterly observations for each year 1990, 1991 etc). From various tests
I have concluded that the error arises when the loop tries to run for a
date when there are no observations (eg 19905). 

My code is:
qui forval i 19901/20071 {
inequal7 income [fw=wei] if date==`i', returns
replace gini=r(gini) if date==`i'
}

I also tried using -foreach- but got the same error message:
qui foreach i = 19901/20071 {
inequal7 income [fw=wei] if date==`i', returns
replace gini=r(gini) if date==`i'
}

The only way that it works is if I specify each date individually, as
in:
qui foreach i in 19901 19902 19903 **etc** {
inequal7 income [fw=wei] if date==`i', returns
replace gini=r(gini) if date==`i'
}

But this means typing each date individually.

I have run -forval- in this way for the same dataset, without problems,
the problem seems to arise when combined with -inequal7-.

I would welcome any suggestions as to how to correct the problem (or
even a smarter way of doing what I need!)


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