Announcement

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

  • I can never remember the foreach syntax

    here is my latest attempt:

    foreach `var' of varlist freqcsi1_1-freqreport1_15 {
    replace `var' = `var' - 1
    }

    invalid syntax
    r(198);

    I get it right sometimes, but not consistently. What is wrong in the above?

    `var' in the first line should be simply var


    aargh
    Last edited by Larry Hanser; 26 Jan 2024, 09:47. Reason: I stumbled across the solution.

  • #2
    It happens to us all. Fortunately, it's a simple command to reference the help file and get the correct syntax when we forget or stumble like this. See -help foreach- and the related -help forvalues-.

    Comment


    • #3
      One way of remembering this may be that references to `var' don't make sense before you have explained that var is the name you will use. However, as that doesn't work for other languages I know about, it may not help.

      Comment

      Working...
      X