Announcement

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

  • Change options based on iteration of a loop

    If I'm trying to run some regression models with this general format and save the main coefficients using regsave (from ssc):

    Code:
    foreach var of varlist score behave attend teach {
        xtlogit outcome `var' i.year x2 x3, fe 
        regsave using "Model output\outcomel.dta", addlabel(model, `var') ci append
    }
    In order to use regsave properly, I'd need the first time through this loop (with 'score' as the main predictor var) to be -ci replace-, and then every ensuing loop would use -ci append-. Is there some way to do this within the loop itself?

  • #2

    Code:
    local first 1
    
    foreach var of varlist score behave attend teach {
        xtlogit outcome `var' i.year x2 x3, fe
        if `first' {
            regsave using "Model output\outcomel.dta", addlabel(model, `var') ci replace
            local first 0
        }
        else regsave using "Model output\outcomel.dta", addlabel(model, `var') ci append
    }
    or

    Code:
    local which replace
    
    foreach var of varlist score behave attend teach {
    xtlogit outcome `var' i.year x2 x3, fe
    
    regsave using "Model output\outcomel.dta", addlabel(model, `var') ci `which'
    
    local which append
    }
    Last edited by Nick Cox; 19 Oct 2024, 17:42.

    Comment

    Working...
    X