Announcement

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

  • loop to select different samples not working

    Hi everyone, I have been looking at this for a while now and still cant figure out what's wrong. I have a few loops here to grab different datasets, run some regressions and then make some graphs. Bellow is the full code. I also want to select different samples in the dataset, so I am using the a loop to keep that sample.
    Code:
     foreach conditional in r_wh retail {
      keep if `conditional'==1
    . It runs the first interaction just right, but when it comes to the second one, it says that the second variable, in this case retail, doesn't exist. I know that because of the collapse and the reshape, the variable no longer exist, but isn't it just using a "clean dataset" and re running, just changing samples? Any tips to improve this. I guess, I could make different datasets but different samples, but I want to avoid that. I would appreciate if you can give me any feedback on what I am missing and how to improve it.
    Thanks in advance.

    Code:
    foreach date in "2010-2012" "2011-2012" {
    forvalues i = 1(1)2 {
      use  "$datadir\noncedereform_t`i'_`date'.dta", clear
      
      
      foreach conditional in r_wh retail {
      keep if `conditional'==1
      
      xtset ide period_day, daily
      
        * Winsorize when needed:
      
      foreach var in rtaxsale rnetliab {
      winsor2 `var', cuts(0 99.5) by(treatment month year) replace
      }
       
      foreach var of varlist rtaxsale rnetliab {
      reg `var' c.treatment##c.post
      foreach x in se b {
      gen double p`x'_`var'=_`x'[c.treatment#c.post]
      replace  p`x'_`var'=round(p`x'_`var',0.001)
      local p`x'_`var'=p`x'_`var'
      local p`x'_`var': di %10.3f  `p`x'_`var''
     }
     }
    
      collapse (mean) rtaxsale rnetliab (sd) sdtaxsale=rtaxsale sdnetliab=rnetliab (count) count_firms=rtaxsale, by(period_day treatment month year)
      replace count_firms=count_firms^0.5
      
      foreach name in taxsale netliab {
      gen v_`name'=(sd`name'/count_firms)*1.96
      gen lower_`name'=r`name'-v_`name'
      gen upper_`name'=r`name'+v_`name'
      gen mean_`name'=r`name'
      foreach type in lower upper mean {
      bysort treatment: egen double pre`type'_`name'=mean(`type'_`name') if year<2012
      bysort treatment: egen double pre`type'_`name'd=mean(pre`type'_`name')
      gen double r`type'`name'_p50_sc=`type'_`name'/pre`type'_`name'd
      }
      }
      
      keep r*_p50_sc period_day treatment month year 
    
      reshape wide r*_p50_sc month year, i(period_day) j(treatment)
      
      drop year1
      drop month1
      sort period_day
      gen n=_n
      gen n2=n if year0==2012 & month0==1
      egen reforma=mean(n2)
      gen dis=n-reforma
      
      egen minxaxis=min(dis)
      replace minxaxis=minxaxis+2
      local textx=minxaxis
      
      egen taxrowmax=rowmax(ruppertaxsale_p50_sc0 ruppertaxsale_p50_sc1)
      egen taxcolmax=max(taxrowmax)
      replace taxcolmax=(0.8*taxcolmax+0.2)
      local taxaxis=taxcolmax
      
      egen liabrowmax=rowmax(ruppernetliab_p50_sc0  ruppernetliab_p50_sc1)
      egen liabcolmax=max(liabrowmax)
      replace liabcolmax=(0.8*liabcolmax+0.2)
      local liabaxis=liabcolmax
      
      twoway (line rmeantaxsale_p50_sc0 dis, lcolor(navy) lp(solid)) (line rmeantaxsale_p50_sc1 dis, lp(dash)), ///
      tlabel(, labsize(vsmall) angle(vertical)) tline(0) ///
      graphregion(color(white)) ylabel(, labsize(vsmall)) legend(order(1 "Control Group" 2 "Treatment Group") rows(1) size(vsmall)) ///
      ytitle(Standard Tax Sale, size(vsmall)) xtitle(year, size(vsmall)) text(`taxaxis' `textx'  "OLS: `pb_rtaxsale' (`pse_rtaxsale')", place(e) orient(horizontal) size(vsmall))
      graph export "`conditional' output VAT noncede did std treatment`i'_`date'.png", replace
      }
      }
      }

  • #2
    Change this
    Code:
    foreach date in "2010-2012" "2011-2012" {
    forvalues i = 1(1)2 {
      use  "$datadir\noncedereform_t`i'_`date'.dta", clear
      
      foreach conditional in r_wh retail {
      keep if `conditional'==1
      
      xtset ide period_day, daily
    to this
    Code:
    foreach date in "2010-2012" "2011-2012" {
    forvalues i = 1(1)2 {
      
      foreach conditional in r_wh retail {
      use  "$datadir\noncedereform_t`i'_`date'.dta", clear
      keep if `conditional'==1
      
      xtset ide period_day, daily
    or this
    Code:
    foreach date in "2010-2012" "2011-2012" {
    forvalues i = 1(1)2 {
      
      foreach conditional in r_wh retail {
      use  "$datadir\noncedereform_t`i'_`date'.dta" if `conditional'==1, clear
      
      xtset ide period_day, daily

    Comment


    • #3
      it works perfectly, thanks!

      Comment

      Working...
      X