Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • syntax for loops

    Good evening,

    I am still struggling with syntax for loops. How do I correct the following:

    Code:
    . tokenize retaustria retbelgium retdenmark retfinland retfrance retgermany retireland retitaly retnethe
    > rlands retnorway retportugal retspain retsweden retswitzerland retuk
    
    . forval i = 1/15 {
      2.
    .    arch `i', arch(1) tarch(1) garch(1)
      3.
    .    predict H_`i'_tarchGJR, variance
      4.
    . }
    1 invalid name
    r(198);
    Also, how do I decide whether the appropriate loop type is "forval" or "foreach"?

    Thank you,
    Stan

  • #2
    Your forval portion seems right. When you tokenize, you have to refer to the tokenized items as ``i'' and not single quotes `i'.

    See this for example to see how tokenize works.

    Code:
    tokenize retaustria retbelgium retdenmark retfinland retfrance retgermany retireland retitaly retnetherlands retnorway retportugal retspain retsweden retswitzerland retuk
    di "`1'"
    forval i = 1/15 {
      di "``i''"
      di "The `i' number is ``i''"
      }


    Code:
    . di "`1'"
    retaustria
    
    . forval i = 1/15 {
      2.   di "``i''"
      3.   di "The `i' number is ``i''"
      4.   }
    retaustria
    The 1 number is retaustria
    retbelgium
    The 2 number is retbelgium
    retdenmark
    The 3 number is retdenmark
    retfinland
    The 4 number is retfinland
    retfrance
    The 5 number is retfrance
    retgermany
    The 6 number is retgermany
    retireland
    The 7 number is retireland
    retitaly
    The 8 number is retitaly
    retnetherlands
    The 9 number is retnetherlands
    retnorway
    The 10 number is retnorway
    retportugal
    The 11 number is retportugal
    retspain
    The 12 number is retspain
    retsweden
    The 13 number is retsweden
    retswitzerland
    The 14 number is retswitzerland
    retuk
    The 15 number is retuk

    Comment


    • #3
      You could also use a -foreach- loop. But you should define all those 15 as a local macro and then call the local macro in a foreach loop.

      Code:
      local list15 retaustria retbelgium retdenmark retfinland retfrance retgermany retireland retitaly retnetherlands retnorway retportugal retspain retsweden retswitzerland retuk
      
      foreach l of local list15 {
      arch `l',....
      ....
      }

      Comment


      • #4
        Thank you very much! It worked!

        Comment


        • #5
          To add some notes to Girish Venkataraman's very helpful replies:

          You don't have to define a local macro. Odds are that looping over a varlist ret* or retaustria-retuk will get that job done.

          forval is for looping over (most simply) integer sequences that could be defined by #start(#step)#stop. There are various different syntaxes, most simply that #start(1)#stop can be just #start/#stop.

          The help shows that the sequences don't have to be integers, but watch out if you try that. A common gotcha is that using increments of 0.1 often yields puzzling results because of precision problems. You're better off looping over integers and dividing by 10 inside the loop.

          As in earlier posts, if you have defined local macros with names say 1 up, then you can use forval to loop over those. Sometimes that's a good idea and sometimes it's easier to use other devices such as looping over varlists.

          I would be most likely to define a local macro when (a) the contents could be quite complicated (b) I want to use that local macro more than once.

          In code like

          Code:
          local myvars frog toad newt  
          
          foreach v of local myvars {
          
          }
          the use of a local macro is on the face of it needless. This device has been called using a middle macro (an allusion to the middle man, who proverbially or idiomatically is someone you should want to cut out!).

          Don't go from Boston to New York via Chicago (substitute a local equivalent). Go straight there:

          Code:
          foreach v in frog toad newt
          Running search loops, sj finds some resources, most recently

          Code:
          . search loop, sj
          
          Search of official help files, FAQs, Examples, and Stata Journals
          
          SJ-21-4 pr0075  . . . . . . . . . . . . . .  Speaking Stata: Loops in parallel
                  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                  Q4/21   SJ 21(4):1047--1064                              (no commands)
                  tutorial discussing looping in parallel using foreach
                  and forvalues
          
          SJ-21-2 pr0074_1  . . . . . .  Erratum: Speaking Stata: Loops, again and again
                  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                  Q2/21   SJ 21(2):555                                     (no commands)
                  erratum for a line of code
          
          SJ-20-4 pr0074  . . . . . . . . . . . . Speaking Stata: Loops, again and again
                  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                  Q4/20   SJ 20(4):999--1015                               (no commands)
                  provides updated guidance on using foreach and forvalues for
                  looping through lists of values and repeating commands using
                  members of those lists in turn
          Last edited by Nick Cox; 15 Dec 2023, 04:05.

          Comment

          Working...
          X