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

From |
"Stas Kolenikov" <skolenik@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: Using foreach and two local macros |

Date |
Wed, 6 Aug 2008 23:48:31 -0500 |

forvalues k = 1/2 { local t1 : word `k' of `test1' local t2 : word `k' of `test2' di `t1' + 2*`t1' + `t2'*`t2' } // or whatever it is that you wanted to compute Those things are called extended functions in Stata jargon, and are extremely powerful at times. They are gems whose functionality is described technically in the manuals, but it takes a few examples to really start appreciating them. On Wed, Aug 6, 2008 at 11:38 PM, Joao Ricardo F. Lima <jricardofl@gmail.com> wrote: > 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/ > -- Stas Kolenikov, also found at http://stas.kolenikov.name Small print: I use this email account for mailing lists only. * * 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/

**Follow-Ups**:**Re: st: Using foreach and two local macros***From:*"Joao Ricardo F. Lima" <jricardofl@gmail.com>

**References**:**st: Using foreach and two local macros***From:*"Joao Ricardo F. Lima" <jricardofl@gmail.com>

- Prev by Date:
**st: Using foreach and two local macros** - Next by Date:
**Re: st: RE: IV and quantile regression** - Previous by thread:
**st: Using foreach and two local macros** - Next by thread:
**Re: st: Using foreach and two local macros** - Index(es):

© Copyright 1996–2016 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |