You can get evaluation on the fly with `=exp'
where "exp" is an expression to be evaluated.
That expression can itself include local macros.
So
forval i = 1(1)`=`t'+2' {
...
}
or
forval i = 1/`=`t'+2' {
...
}
Note that this is nothing to do with -forvalues-
as such. It is a general device for handling
macro evaluation, added in Stata 7 (but not documented)
and documented in Stata 8. For example, start at the
on-line help for -macro-.
There was also related discussion within SJ 3(2):185--202
(2003).
Nick
n.j.cox@durham.ac.uk
Richard Upward
> I'm trying to write some code that repeats a varying number
> of times, based on an argument `t'. I'd like to be able to
> write, for example
>
> forvalues i = 1(1)`t'+2 {
> <some code>
> }
>
> but -forvalues- does not seem to allow this because it
> evaluates `t' but does not add it to the 2. So, if `t' was
> 10 I end up with
>
> forvalues i=1(1)10+2
>
> which gives a syntax error. Is there a way to force
> -forvalues- to evaluate an expression like this, or do I
> need to set another macro equal to `t'+2 before the loop?
> That seems a bit clunky. I noticed that I can use an
> expression like this in a -while- loop.
*
* 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/