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

st: RE: Evaluating an expression in -forvalues-

From   "Nick Cox" <>
To   <>
Subject   st: RE: Evaluating an expression in -forvalues-
Date   Tue, 16 Dec 2003 10:14:16 -0000

You can get evaluation on the fly with `=exp' 
where "exp" is an expression to be evaluated. 
That expression can itself include local macros.

forval i = 1(1)`=`t'+2' { 


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


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:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index