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 14:18:33 -0300

Nick,

thanks for the information. I'm with the two articles and will read.

Best regards!

Joao Lima

2008/8/7, Nick Cox <[email protected]>:
> Stefan's (non-)recommendation of -for- deserves some further explanation
> and yet more  warnings.
>
> Note that this -for- in Stata is absolutely nothing to do with -for- in
> Mata.
>
> -for- was a prominent command in Stata, but has not been documented for
> some time.
> That is, help is not even accessible via -help undocumented-. So -for-
> is of no use to you unless you can remember the syntax or somehow have
> kept on-line or printed documentation from Stata 7.
>
> It is true that -for- supported parallel lists and that nothing quite
> equivalent is
> available via -foreach- or -forvalues-. That was, however, its only real
> advantage in my view. In every other respect, -for- is a command that
> can get you into real messes. Longer-standing members of this list may
> remember a genre of anguished questions from people bitten by one or
> more aspects of -for-.
>
> Echoes of that genre can be seen in
>
> http://www.stata.com/support/faqs/lang/macros.html
> http://www.stata.com/support/faqs/lang/nestfor.html
>
> Among other things, -for- entailed a considerable interpretative
> overhead. So far from code being shorter, it was really much, much
> longer than with -foreach- or -forvalues-, even for parallel lists.
>
> In terms of the original question, Jose evidently did not look at
>
> 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-2-2  pr0005  . . . . . .  Speaking Stata:  How to face lists with
> fortitude
>         . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N.
> J. Cox
>         Q2/02   SJ 2(2):202--222                                 (no
> commands)
>         demonstrates the usefulness of for, foreach, forvalues, and
>         local macros for interactive (non programming) tasks
>
> Under the Stata Journal's 3-year moving window, these papers are visible
> to all via the Stata Journal website, regardless of whether you or your
> institution subscribe.
>
> The more recent paper discusses parallel lists (and the demise of -for-)
> in considerable detail.
>
> Nick
> [email protected]
>
> Gawrich Stefan
>
> Looping over parallel lists is indeed a very powerful feature.
> To keep it flexible you'll often use a second extended function (word
> count)
> with the forval command.
>
> local x "1 2"
> local y "3 4"
> local n : word count `x'
> forvalues i = 1/`n' {
> local x1 : word `i' of `x'
> local y1 : word `i' of `y'
> di `x1'+2*`x1'+`y1'
> }
>
> But in my view the syntax has two drawbacks:
> 1) It needs a lot of lines. So it bloats your do-files expecially when
> you
> use it with more than two locals.
> (I often use it for batch graph creation where variables, selections,
> titles
> and settings can make up to ten locals)
> 2) You have to create new local names inside the loop, which makes it
> confusing and error prone
>
> So sometimes I find it convenient to use that good old (not longer
> documented) "for" command again.
>
> for num 1 2 \ num 3 4 :  di X+2*X+Y  // Yes, you can do it in one line!
>
> or
>
> local x "1 2"
> local y "3 4"
> for num `x' \ num `y' :  di X+2*X+Y
>
> But foreach and forvalues are now state of the art, so I don't want to
> advertise "for".
> But I think it could be very helpful to save the user from writing all
> these
> extended function lines and add this
> parallel lists functionality to "foreach".
>
>
> *
> *   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