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]

st: nested foreach loops using the values in the respective order


From   "Ariel Linden, DrPH" <ariel.linden@gmail.com>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: nested foreach loops using the values in the respective order
Date   Tue, 16 Jul 2013 17:55:12 -0400

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