Announcement

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

  • Activate and deactivate commands in a loop

    Hi all,

    I have the following for loop:

    Code:
    set more off
    local recall "dummy_1 dummy_2 dummy_3 dummy_4"
    
    foreach rec of local recall {
        local aggr "product firm atc2"
        local expl "sales stdunits price"
        foreach agg of local aggr {
            foreach k of local expl {
                use "/Users/federiconutarelli/Dropbox/Federico/store_DB_analyses/DB_`agg'_`rec'_`k'_nomiss.dta", clear
            
                estimates restore `agg'_`rec'_`k'  // serve per attivare le regressioni
                predict av_t, res
                replace av_t = round(av_t, .111)
                predict stdres, stdr
                predict stdyhat, stdp
                predict stdyj, stdf
                gen var_avt = stdyj - stdyhat
                replace var_avt = stdyhat
                
                su ritiri, meanonly
                forval j = 1/`r(max)'{
                    gen av_t`j' = av_t
                    gen var_avt`j' = var_avt
                    }
            
            
            
                save "/Users/federiconutarelli/Dropbox/Federico/store_DB_analyses/grafici_e_DB/demeaned_`agg'_`rec'_`k'.dta", replace
                merge 1:1 _n using "/Users/federiconutarelli/Dropbox/Federico/store_DB_analyses/DB_`agg'_`rec'_`k'_nomiss.dta"
                save "/Users/federiconutarelli/Dropbox/Federico/store_DB_analyses/grafici_e_DB/demeaned_`agg'_`rec'_`k'.dta", replace
            
                local avt
                su ritiri, meanonly
                forval j = 1/`r(max)'{
                    local avt `avt' av_t`j' var_avt`j' rescaled`j'
                
                }
                if "`agg'"~="product" & "`rec'"~= "dummy_1"{
                stack `avt', into(mean_avt var_avt rescaled) clear
                }
            
            
                save "/Users/federiconutarelli/Dropbox/Federico/store_DB_analyses/grafici_e_DB/stacked_mean_`agg'_`rec'_`k'.dta", replace
    
                clear
            }
        }
    }
    What I would like to do actually is avoid to perform the operations from

    Code:
    local avt
    su ritiri, meanonly
    to the end when `agg' == product and `rec'==dummy_1 (hence I would like to perform it just for all the other cases: product-dummy_2, product-dummy_3, product-dummy_4, firm-dummy_1, firm-dummy_2...atc2-dummy_4). Actually the if that I put does not seem too work. In particular it avoids the operations for all the aggregations with "product" (so for all product-dummy_1, product-dummy_2, product-dummy_3 and product-dummy_4).
    The real problem is actually that stack accepts only earliest and the Database product-dummy_1 has only one av_t.
    Is there a way to do so? I was thinking about defining "stack" as a local...

    Thank you,


    Federico

  • Federico Nutarelli
    replied
    Oh thank you a lot! I was trying actually with a while loop redefining the aggr local as "firm atrc2" only, but then I did not know how to continue the loop ignoring the modification in the while.

    Thank you a lot!

    Leave a comment:


  • William Lisowski
    replied
    This if command from your code
    Code:
    if "`agg'"~="product" & "`rec'"~= "dummy_1"{
    should be
    Code:
    if ! ("`agg'"=="product" & "`rec'"== "dummy_1") {
    or equivalently
    Code:
    if "`agg'"~="product" | "`rec'"~= "dummy_1"{

    Leave a comment:

Working...
X