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: simulataneous foreach loops


From   Nick Cox <[email protected]>
To   "'[email protected]'" <[email protected]>
Subject   RE: st: simulataneous foreach loops
Date   Mon, 15 Nov 2010 18:31:38 +0000

Like Nick Winter, I see only one loop in this problem. 

The situation is also discussed at length in 

SJ-3-2  pr0009  . . . . . . . . . . . . . Speaking Stata:  Problems with lists
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/03   SJ 3(2):185--202                                 (no commands)
        discusses ways of working through lists held in macros

Nick 
[email protected] 

Nick Winter

It sounds like you want something that the old, 
out-of-date-and-no-longer-supported -for- command made easy:

for num 101 102 103 \ num 3 5 3 : di "team X will have Y members"

-for- is no longer documented, so you'll have to guess how to use it if 
you want something more complicated than that.

In modern Stata, you could do something like:

local tnums 101 102 103
local tsizes 3 5 3

forval i=1/3 {
   local team : word `i' of `tnums'
   local size : word `i' of `tsizes'
   di "Team `team' will have `size' members"
}

On 11/12/2010 2:45 PM, Lim, Raymond wrote:

> I preserve before the loop in my actual code. The loop right now creates team 101 with 3 members, team 102 with 5 members, team 103 with 3 members, etc. Yes, there's a one-to-one mapping from tt to NN. I basically need two loop indices to change simultaneously. For example, first round (i=103, j=3), second round (i=139, j=5), third round (i=230, j=3). Of course, the real thing is a couple hundred pairs.
>
> -Raymond
>
> preserve
> local tt=100
> qui foreach NN of numlist 3 5 3 4 {
>         bsample `NN'
>         local tt=`tt'+1
>         replace team=`tt'
>         save simteam_`tt'.dta, replace
>         restore
>         preserve
>         }
>
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/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/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


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