Announcement

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

  • Invalid syntax in a loop (r198)

    Dear all,

    I had a problem when doing a loop, the commands are as follows:

    local max_ad 2

    forvalues andate=1/`max_ad'{
    replace event_time=0 if excel_date==ad`andate'
    replace identifier=secid+`andate'/10 if excel_date==ad`andate'
    replace final=1 if excel_date==ad`andate' & ad`andate'_fin==1
    }


    If always returns invalid syntax. But when I changed `max_ad' to 2, it can be processed.

    Can somboby please tell me why this happens? And how can this be solved?

    Thank you in advance!
    Tanya

  • #2
    My best guess is that you are running this code in blocks, That would account for the definition of the local being invisible to the code that uses it -- and for why you could fix the code.

    A perhaps cryptic aphorism is that local means what it says. Less cryptically, a local macro is visible within the same block of code, which in turn means

    1. The same interactive session

    2. The same program

    3. The same do-file

    4. The same do-file editor contents, noting that if you are running code from that in chunks, that narrows visibility down to the same chunk.

    Otherwise note that it's hard for anyone to test your code, which depends on variables in your dataset that we don't have.

    Comment

    Working...
    X