Announcement

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

  • Encountering error r(198) while running a loop in stata

    HI,

    I am encountering error while running a loop in stata. The following is my code

    HTML Code:
    forvalues i = 1/`MaxLPLags' {
    
      foreach var in state_gdp state_exp TE_shock {
    
        gen bad`var'`i' = L`i'.`var'*`state'
        gen good`var'`i' = L`i'.`var'*(1-`state')
     
      }
    }
    
    I have declared state, MaxPLags, as locals. When I run this loop, I come across

    HTML Code:
    invalid syntax
    r(198);
    I am not sure why I am getting this error. Some help is appreciated.
    Thank you.

    Regards
    Indrani
    Last edited by Indrani Manna; 26 Feb 2021, 06:12. Reason: loop; r(198)

  • #2
    My guess is that you are running the code line by line, so that the definition of macro i is invisible when you try to use it.

    Comment


    • #3
      Thank You Nick! I am running the loop as a whole not line by line. But the loop is only a part of the larger code. When I run the entire code, there is no such error. Isnt it true that once we declare a variable as local, it stays in the memory until the end of the code. So even if I run a part of the code, it should just remember that state was declared as a local or MaxPlags is local.

      Comment


      • #4
        If you are running the loop as a whole, but it is just one piece of larger code that you are running a piece at a time, then my guess is that the code that set MaxLPLags is not part of the piece with the loop, so it is undefined when you run the piece with the loop.

        once we declare a [macro] as local, it stays in the memory until the end of the code
        It stays in memory until the piece of code being run ends.

        Consider the following example. In the do-file editor window, I have a two-line program that I run in its entirety.
        Code:
        . do "/Users/lisowskiw/Downloads/example.do"
        
        . local message Hello, world.
        
        . display "The message is `message'"
        The message is Hello, world.
        
        . 
        end of do-file
        Now I run the same two lines by selecting the first line and running it, then selecting the second line and running it.
        Code:
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
        
        . local message Hello, world.
        
        . 
        end of do-file
        
        . do "/var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//SD17616.000000"
        
        . display "The message is `message'"
        The message is 
        
        . 
        end of do-file
        The important thing to keep in mind is that local macros vanish when the do-file within which they were created ends. If you look carefully at the results above, you'll see that when I selected a single line to run, it was copied into a temporary do-file and run, so even though both lines are in the same window in the do-file editor, they are run as separate do-files, and local macro defined in the first line vanishes at the end of that do-file, and is undefined when the second line is run.

        Comment

        Working...
        X