Announcement

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

  • Invalid Syntax Error while Looping

    Hi,

    when I enter the following prompt:

    * Define periods and variables
    local periods "2005 2008 2009 2012 2013 2016 2017 2020 2021 2024"
    local vars "debt_maturity_1 size market_to_book leverage rd capex ppe roa dividend_dummy cash age taxes"

    * Loop over periods
    foreach period in `periods' {
    local start = word("`period'", 1)
    local end = word("`period'", 2)

    * Filter for the time period
    preserve
    keep if year >= `start' & year <= `end'

    * Loop over variables and compute stats for each size group
    foreach var in `vars' {
    bysort size_group: summarize `var', detail
    display "Period: `start'-`end', Variable: `var', Mean: " %9.2f r(mean) ", Median: " %9.2f r(p50)
    }

    restore
    }

    I get the Invalid Syntax Error. Any Ideas what I did wrong?

    Thanks!

  • #2
    What do you want to capture in "start" and "end" local macros?
    I ran the first five lines only, and it seems "start" macro captures the year in "periods" macro, while "end" captures nothing.

    Code:
    foreach period in `periods' {
            
        local start = word("`period'", 1)
        local end = word("`period'", 2)
        
        di    "start is `start'"
        di    "end is `end'"
        
    }   
    
    
    start is 2005
    end is
    start is 2008
    end is
    start is 2009
    end is
    start is 2012
    end is
    start is 2013
    end is
    start is 2016
    end is
    start is 2017
    end is
    start is 2020
    end is
    start is 2021
    end is
    start is 2024
    end is
    I assume this is why you are getting an syntax error in "keep if year >= `start' & year <= `end' line, since you are conditioning on "end" value which is null value.

    Comment


    • #3
      I can tell you what went wrong, but as I'm not sure what you're trying to do, I can't advise you how to fix it.

      The error is coming from the command -keep if year >= `start' & year <= `end'-. This is happening because you have defined -local end = word("`period'", 2)-, but local macro period, an iterator in your -foreach- loop is just a single word (number, in your case), so word("`period'", 2) does not exist.

      I'll go out on a limb and guess that what you mean to do is have `start' = 2005 with `end' = 2008, and then on the next iteration have `start' = 2008 and `end' = 2009, and so on. If that's the case your code needs start like this:

      Code:
      local n_periods: word count `periods'
      local n_periods = `n_periods' - 1
      forvalues i = 1/`n_periods' {
          local start = word("`periods'", `i')
          local end = word("`periods'", `=`i'+1')
      
      // ETC.
      Note: I haven't really investigated the rest of the code. There may be additional errors in there--at first glance it looks confusing to me, but I haven't checked it out carefully.

      Comment

      Working...
      X