Announcement

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

  • Summarize and ameans return no result within a loop

    Hello Statalisters!

    I need help to understand what I am doing wrong in my loop. If I run from the command line:

    Code:
    summarize epl_reg if year==2018 & country =="IE"
    or

    Code:
    ameans epl_reg if year==2018 & country =="IE"
    I am able to retrieve the scalar stored in r(mean).

    However, if I run those commands within a loop, I eventually obtain no results (i.e. it reports 0 observations) from both the summary statistics or the ameans statistics. I say "eventually" because the first 5 or 6 times `l' takes the value "IE" and year takes the value 2018 both summary and ameans commands calculate the mean statistic correctly. The issue appears later. I do not understand why for a few times it does the proper calculation but then it does not.

    The whole loop is the following:

    Code:
            foreach y in $MacroList {
                foreach f in $EUFundsList {
                    foreach x in $EUPaymentMethodList {
                        forvalues h = 1/$ForeHorizon {
                        
                        
                            display "fund=`f' y=`y' horizon=`h' instrumented_method=`x' instrument=`Inter_var'"
                            
                            qui sleep 250
                            preserve
                                
                            qui keep if fund == "`f'"                     
                            qui encode geo, gen(id_g)
                            qui gen geo_time=country+"_"+string(year)
                            qui encode geo_time, gen(id_ct)
                            qui xtset id_g year
                
                            
                            eststo clear
                                
                                local i=0
                                qui levelsof country, local(levels) 
                                foreach l of local levels {
                                    
                                    local i=`i'+1
                                
                                    tempfile ft`i'
                                    qui gen temp_var = `Inter_var'*`x'_cum_`h'
                                    qui ivreghdfe     `y'_cum_`h' (c.`x'_cum_`h' c.temp_var = c.u_`x'_1 c.`Inter_var'#c.u_`x'_1 ) ///
                                    l.`y'_cum_`h' c.`Inter_var' c.abs7, ///
                                    absorb(id_g year) robust bw(`h') level(90) ///
                                    partial(l.`y'_cum_`h' c.`Inter_var' c.abs7)
                                    
                                    display "country=`l'"
                                    *summarize `Inter_var' if year==2018 & country =="`l'"        
                                    ameans `Inter_var' if year==2018 & country =="`l'"
                                    local Quant = r(mean)
                                    
                                    
                                    qui parmby "lincomest `x'_cum_`h' +`Quant'*temp_var", label idstr("`l'") saving(`ft`i'',replace)
                                    drop temp_var
    
                                }
                                
                                dsconcat `ft1' `ft2' `ft3' `ft4' `ft5' `ft6' `ft7' `ft8' `ft9' `ft10' `ft11' `ft12' `ft13' `ft14' `ft15' `ft16' `ft17' `ft18'
                                qui gen min90 = estimate - 1*stderr
                                qui gen max90 = estimate + 1*stderr  
                                
                                
                                qui gen macro = "`y'"
                                qui gen horizon = `h'
                                qui gen paymenttype= "`x'"
                                qui gen fund = "`f'"
                                
                                qui append using ResultsDBPanel_Marg_Int_`Inter_var'_Chart
                                qui save ResultsDBPanel_Marg_Int_`Inter_var'_Chart, replace                        
    
                                
                            restore
                            
                            */
                        
                        }    
                    }
                }    
            }

    My data looks like this for the year 2018 (790 observations, 3 for Ireland).



    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input int year str3 country double epl_reg
    2018 "AT" 2.2857143878936768
    2018 "AT" 2.2857143878936768
    2018 "AT" 2.2857143878936768
    2018 "AT" 2.2857143878936768
    2018 "AT" 2.2857143878936768
    2018 "AT" 2.2857143878936768
    ....
    
    2018 "HU" 1.5873016119003296
    2018 "HU" 1.5873016119003296
    2018 "IE" 1.2301586866378784
    2018 "IE" 1.2301586866378784
    2018 "IE" 1.2301586866378784
    2018 "IT" 2.4742064476013184
    2018 "IT" 2.4742064476013184
    2018 "IT" 2.4742064476013184
    ....
    end
    Thank you very much!

    Federico

  • #2
    Perhaps the 0 observation results are for a country which does does not have any data in 2018
    Code:
    tab country year
    will tell you if there are countries in your data which are not present in every year.

    Comment


    • #3
      It is not clear to me what you are trying to achieve with this loop. The loop works as intended, but inside the loop you set a local

      Code:
       
                                       *summarize `Inter_var' if year==2018 & country =="`l'"                                          ameans `Inter_var' if year==2018 & country =="`l'"                                  local Quant = r(mean)
      and this local is overwritten each next round of the loop, and probably disappears anyways because it is local to the loop.

      So what are you trying to achieve with your code in relation to your question?

      You can also check out my egen function _gwmean, which is byable and can provide all types of weighted means.

      Comment

      Working...
      X