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


From   "Ariel Linden, DrPH" <[email protected]>
To   <[email protected]>
Subject   re: Re: st: nested foreach loops using the values in the respective order
Date   Wed, 17 Jul 2013 07:03:03 -0400

Thank you Richard, Sergiy and Nick! 

These solutions all work very nicely. It's very enlightening to see how the
various ways to solve a given problem!

Ariel

Date: Tue, 16 Jul 2013 18:27:32 -0400
From: Sergiy Radyakin <[email protected]>
Subject: Re: st: nested foreach loops using the values in the respective
order

I'd prefer matrices for this case:

matrix TestPlan = 390, 0.50 \ 121, 0.30 \ 42, 0.20
forval i=1/`=rowsof(TestPlan)' {
  local one = TestPlan[`i',1]
  local two = TestPlan[`i',2]
  display `"`one' `two'"'
}

//eof


390 .5
121 .3
42 .2

Note that it is also quite easy to load and save matrices, so that if
you have different test plans or simulations you can store them
separately from the code.

Best, Sergiy


On Tue, Jul 16, 2013 at 6:09 PM, Nick Cox <[email protected]> wrote:
> 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/


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