Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: Using foreach and two local macros


From   "Joao Ricardo F. Lima" <jricardofl@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   st: Using foreach and two local macros
Date   Thu, 7 Aug 2008 01:38:53 -0300

Dear Statalisters,

I'm trying to use the foreach command, I see the FAQ, etc, but it's
not clear to me.

One first simple example, I can do

local test1 "1 2"
foreach z of local test1 {
              di `z'+2*`z'
}

The result is

. local test1 "1 2"

. foreach z of local test1 {
  2.               di `z'+2*`z'
  3. }
3
6

Stata is doing 1+2*1=3; 2+2*2=6;

However, if I have two local macros, I can do

local test1 "1 2"
local test2 "3 4"
foreach z of local test1 {
      foreach h of local test2 {
              di `z'+2*`z'+`h'
}
}

and see these results

...
. foreach z of local test1 {
  2.       foreach h of local test2 {
  3.               di `z'+2*`z'+`h'
  4. }
  5. }
6
7
9
10

or

1+2*1+3=6;
1+2*1+4=7;
2+2*2+3=9;
2+2*2+4=10... the 4 possible combinations.

My question is how can I do a loop to work with two local macros and
get, for example, only these two combinations:

local test1 "1 2"
local test2 "3 4"

foreach...???

di 1+2*1+3
di 2+2*2+4

Sorry this question, thanks for the help.

Best regards,

Joao Ricardo

-- 
-------------------------------
Joao Ricardo Lima
Professor
UFPB-CCA-DCFS
+553138923914
-------------------------------
*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index