Announcement

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

  • For Loop Syntax Error

    Hello,

    I am trying to use a basic for loop but keep getting the error 'Invalid Syntax' which I cannot figure out. The variable id has been defined.

    Code:
    forval i=1/`id'{
    preserve
    use if (inrange(Census2001_Lon,`minlon',`maxlon') & inrange(Census2001_Lat,`minlat',`maxlat')) using MainData
    save data_`id', replace
    restore
    }
    Could anyone please tell me if they understand the reason for the syntax error. I ran the code without the for loop and using individual values and it worked.

    Thanks!

  • #2
    Welcome to Statalist.

    What you show us is confusing and apparently incomplete.

    You make no use of the loop local macro i within the loop, so if the loop were to execute, it would execute exactly the same commands each time through the loop.

    As you have shown it, though, you will get a syntax error because the local macros minlon, maxlon, minlat, and maxlat have not been defined.

    Perhaps if you were to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post, noting especially sections 9-12 on how to best pose your question, and then give more details, other members would better understand what you are trying to accomplish and could advise you on how to do that.

    Comment


    • #3
      Please refer to this thread now: https://www.statalist.org/forums/for...ubsetting-data

      Comment

      Working...
      X