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

Re: st: problem with -forvalues-


From   Gary Longton <glongton@fhcrc.org>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: problem with -forvalues-
Date   Fri, 27 Sep 2002 12:19:40 -0700

Estie Hudes wrote:

I have been using -for- for a very long time now, and like it's
capabilities. However, I know it has its limits, and doesn't with a very
complicated sequence of commands.

I recently read Nick Cox' wonderful "Speaking Stata" column, and decided to
try and use -foreach- -forval-, instead of -for-. I have run into problems
with -forval-.

When my numlist is of the form "1 0", "0 1", "1", -forval- complains about
"invalid syntax". It works with "0/1" or "1(-1)0". With "1/0" it doesn't
complain, but displays no results.

-for-, on the other hand, works with all of the above.
the -forvalues- syntax does not really accept a "numlist", rather a "range" which is more narrowly defined in the online help for -forvalues-.

0/1 and 1(-1)0 are acceptable ranges under the definition; "1 0" "0 1" and "1" are not.

1/0 is apparently acceptable range syntax, but translates to "1 to 0 in steps of 1", yielding an empty set.

-foreach-, on the other hand, WILL accept a numlist if the appropriate syntax is used. Eg.

.foreach i of numlist 1 0 -3 { di in yel "i: `i'" }
i: 1
i: 0
i: -3

. foreach i of numlist 1/-3 { di in yel "i: `i'" }
i: 1
i: 0
i: -1
i: -2
i: -3

- Gary

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