Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Defining upper limit of loop


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: Defining upper limit of loop
Date   Wed, 11 Dec 2013 22:17:42 +0000

Yes indeed. Suppose that you want to cycle over 1 to the maximum of
some variable:

su myvar, meanonly

forval i = 1/`r(max)'  {

The more general syntax is

forval i = `=exp1'/`=exp2' {

where exp1 and exp2 are expressions to be evaluated on the fly.

So

(1)

forval i = `=2+2'/`=6*7' {

would be

forval i = 4/42 {

(2)

su myvar, meanonly

forval j = `r(min)'/`r(max)' {

would cycle from the minimum to the maximum (in steps of 1).

Little note. `r(min)' or `r(max)' and `=r(min)' or `=r(max)'  work
equally well. In principle they don't mean exactly the same, but the
result is identical.

Big note. This is nothing to with the syntax of -forval- as such. It
is just general Stata syntax. See -help macro-.
Nick
[email protected]


On 11 December 2013 22:08, Nabin Kafle <[email protected]> wrote:
> Is there a way that I can use the forvalues command in stata in a way
> that I upper value of the loop can be defined from the varibale list.
>
> forvalues i = 1/"var name" {
> code for loop
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/faqs/resources/statalist-faq/
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index