Statalist


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

Re: st: Using foreach and two local macros


From   "Joao Ricardo F. Lima" <[email protected]>
To   [email protected]
Subject   Re: st: Using foreach and two local macros
Date   Thu, 7 Aug 2008 02:15:12 -0300

Stas,

really thanks for your answer. It works perfect!

Best,

Joao Ricardo

2008/8/7, Stas Kolenikov <[email protected]>:
> 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
> <[email protected]> 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/
>


-- 
-------------------------------
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–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index