Dear Stata Users,
I am trying to use a loop so that i create four different figures for four different treatment variables. I am using the code below:
But it is generating the first two figures perfectly (for city1 and city2). But the third and fourth figures are generated the same. So it is replacing figure City3 with Figure City 4. I am getting 3 figures at the end: City1, City2, and City3=City4.
Is there something wrong with the code?
Thanks
JL
I am trying to use a loop so that i create four different figures for four different treatment variables. I am using the code below:
Code:
foreach var in treat_city1 treat_city2 treat_city3 treat_city4 { cd "$mydata_home" use Pollution_finaldata_citylevel, clear * Interpolate technology variable ("piecewise constant using the nearest neighbor from the past") bysort ordinal (year): mipolate `var' year, generate(`var'_comp) forward *--------------------------------------*---------------------------------------* * Treatment: 1 *--------------------------------------*---------------------------------------* * Create a dummy for cities A bysort ordinal: gen treated = `var'_comp-L.`var'_comp gen participated = 0 bysort ordinal: replace participated = 1 if treated == 1 | treated < 0 bysort ordinal: egen withingroup = max(participated) gen treatment = 1 if withingroup == 1 replace treatment = 0 if withingroup==0 replace treatment = . if withingroup==. *City 1 ******** preserve if `var' == treat_city1 { collapse (mean) polutionN2 , by( time treatment) #delimit; twoway (connected polutionN2 time if treatment==1, mc(red) msize(small) lpattern(solid) ms(D) lc(red) lw(medthick)) /// (connected polutionN2 time if treatment==0, mc(gray) msize(blue) lpattern(dot) lc(blue) lw(medthick)), /// graphregion(color(white)) /// legend (off) xtitle("Year") ysc(r(50 150)) /// ytitle (City polution)) xlabel(2004(1)2019) xlabel(, angle(vertical)) /// title("") ; #delimit cr } graph export "$Myfig_home\treat_city1.png", as(png) replace restore *City 2 ******** preserve if `var' == treat_city2 { collapse (mean) polutionN2 , by(time treatment) #delimit; twoway (connected polutionN2 time if treatment==1, mc(red) msize(small) lpattern(solid) ms(D) lc(red) lw(medthick)) /// (connected polutionN2 time if treatment==0, mc(gray) msize(blue) lpattern(dot) lc(blue) lw(medthick)), /// graphregion(color(white)) /// legend (off) xtitle("Year") ysc(r(50 150)) /// ytitle (City polution)) xlabel(2004(1)2019) xlabel(, angle(vertical)) /// title("") ; #delimit cr } graph export "$Myfig_home\treat_city2.png", as(png) replace restore *City 3 ******** preserve if `var' == treat_city3 { collapse (mean) polutionN2 , by(time treatment) #delimit; twoway (connected polutionN2 time if treatment==1, mc(red) msize(small) lpattern(solid) ms(D) lc(red) lw(medthick)) /// (connected polutionN2 time if treatment==0, mc(gray) msize(blue) lpattern(dot) lc(blue) lw(medthick)), /// graphregion(color(white)) /// legend (off) xtitle("Year") ysc(r(50 150)) /// ytitle (City polution)) xlabel(2004(1)2019) xlabel(, angle(vertical)) /// title("") ; #delimit cr } graph export "$Myfig_home\treat_city3.png", as(png) replace restore *City 4 ******** preserve if `var' == treat_city4 { collapse (mean) polutionN2 , by(time treatment) #delimit; twoway (connected polutionN2 time if treatment==1, mc(red) msize(small) lpattern(solid) ms(D) lc(red) lw(medthick)) /// (connected polutionN2 time if treatment==0, mc(gray) msize(blue) lpattern(dot) lc(blue) lw(medthick)), /// graphregion(color(white)) /// legend (off) xtitle("Year") ysc(r(50 150)) /// ytitle (City polution)) xlabel(2004(1)2019) xlabel(, angle(vertical)) /// title("") ; #delimit cr } graph export "$Myfig_home\treat_city4.png", as(png) replace restore }
But it is generating the first two figures perfectly (for city1 and city2). But the third and fourth figures are generated the same. So it is replacing figure City3 with Figure City 4. I am getting 3 figures at the end: City1, City2, and City3=City4.
Is there something wrong with the code?
Thanks
JL
Comment