Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"{ required" in nested loop in stata

Tags:

stata

I hate to annoy you guys with this question, but I am getting the error "{ required" even though all my loops appear to be open (and closed) properly and unfortunately Stata doesn't tell you where the error is, so I can't figure out why this is happening. By the way if I take out the append_replace section with the if statements, I am still getting the same error, so I don't think it is from that section. Here is my code:

local vars = "any_rate resp_rate circ_rate weight_rate diabetes_rate gallstones_rate   mental_rate cancer_rate std_rate died_rate"
local dates = "1947 1974"
foreach var of local `vars' {
    foreach i of local `dates' {
        forvalues j = 500(100)2500 {
            local append_replace = "append"
            if "`var'"=="any_rate" {
                if "`i'" == "1947" {
                    if `j' == 500 {
                        local append_replace = "replace"
                    }
                }
            }
            reg `var' post`i' dobdistfrom`i'change dobdistfrom`i'changesq post`i'_dist`i' post`i'_dist`i'sq if dobdistfrom`i'change < `j' & dobdistfrom`i'change > -`j', cluster(dobdistfrom`i'change)
                outreg2 using Prelim_RD_Estimates.xls, `append_replace' excel dec(3)
        }
    }
}

Thanks so much for your help!

like image 664
Brandon Heck Avatar asked Aug 13 '26 19:08

Brandon Heck


1 Answers

I believe the problem is with the local that prevents the { from being read.

Original problematic version:

local dates = "1947 1974"
foreach i of local `dates' {
  di `i'
  }

Corrected version:

local dates = "1947 1974"     
foreach i in `dates' {
   di `i'
   }

You could also just omit the quotes in "foreach i of local dates" in your original construction.

like image 107
RedOwl Avatar answered Aug 16 '26 14:08

RedOwl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!