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   Richard Herron <[email protected]>
To   [email protected]
Subject   Re: st: nested foreach loops using the values in the respective order
Date   Tue, 16 Jul 2013 18:08:54 -0400

I would use -extended_fcn-s here. There are -extended_fcn-s to count
words in a string and to select word n in a string.

***
local first 390 121 42
local second 0.50 0.30 0.20
local n: word count of `first'
forvalues i = 1/`n' {
    local one: word `i' of `first'
    local two: word `i' of `second'
    display "`one', `two'"
}
***

This yields:

390, 0.50
121, 0.30
42, 0.20

On Tue, Jul 16, 2013 at 5:55 PM, 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