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: nested foreach loops using the values in the respective order


From   Nick Cox <[email protected]>
To   "[email protected]" <[email protected]>
Subject   Re: st: nested foreach loops using the values in the respective order
Date   Tue, 16 Jul 2013 23:09:39 +0100

This is one loop over three pairs, if I understand you correctly:

forval k = 1/3 {
             local i : word `k' of 390 121 42
             local j : word `k' of 0.5 0.3 0.2

}

Some discussion 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

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]


On 16 July 2013 22:55, Ariel Linden, DrPH <[email protected]> wrote:
> Hi fellow listers,
>
> This is likely to be a simple problem, but I can't seem to figure this one
> out..
>
> I have a nested loop where I'd like the code to loop over each pair of
> values in the first and second foreach loop respectively. So in the example
> below, I'd like there to be only three total loops, with the first paired
> values = 390, 0.50, the second paired values = 121, 0.30, and the third
> paired values = 42, 0.20. This should terminate the looping.
>
> However, I get the array of values as displayed below...
>
> Thanks in advance!
>
> Ariel
>
> **** code****
>
> foreach i of numlist 390 121 42 {
>    foreach j of numlist 0.50 0.30 0.20 {
>          display "`i',`j'"
>   }
> }
> **** end code
>
>
> ***output***
> 390,.5
> 390,.3
> 390,.2
> 121,.5
> 121,.3
> 121,.2
> 42,.5
> 42,.3
> 42,.2
>
> *
> *   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