Announcement

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

  • Loop for many figures

    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:

    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

  • #2
    Statements of the form

    Code:
    if `var' == treat_city2  
    are always going to be interpreted as

    Code:
      
     if `var'[1]  == treat_city2[1] 


    which will be true for
    `var' being treat_city2 and not necessarily otherwise.

    See https://www.stata.com/support/faqs/p...-if-qualifier/

    You want to check the variable names, not their contents.

    I guess what you are reaching for is
    Code:
    if "`var'" == "treat_city2"
    and so on.

    But I am not clear that you need that at all. Does this help? [


    Code:
    cd "$mydata_home"
    use Pollution_finaldata_citylevel,  clear
    
    foreach var in treat_city1 treat_city2 treat_city3 treat_city4    {
    
        preserve 
        bysort ordinal (year): mipolate `var' year, generate(`var'_comp) forward
        bysort ordinal: gen treated = `var'_comp-L.`var'_comp
        bysort ordinal: egen treatment = max(treated == 1 | treated < 0)
        collapse (mean) polutionN2 , by(time treatment)
        
        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("")
    
        graph export "$Myfig_home/`var'", as(png)  replace
    
        restore
    }

    Last edited by Nick Cox; 13 Mar 2022, 08:37.

    Comment


    • #3
      Dear Nick,


      When I used your code:
      But I am not clear that you need that at all. Does this help?

      Code:
        
       cd "$mydata_home" use Pollution_finaldata_citylevel,  clear  foreach var in treat_city1 treat_city2 treat_city3 treat_city4    {      preserve      bysort ordinal (year): mipolate `var' year, generate(`var'_comp) forward     bysort ordinal: gen treated = `var'_comp-L.`var'_comp     bysort ordinal: egen treatment = max(treated == 1 | treated < 0)     collapse (mean) polutionN2 , by(time treatment)          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("")      graph export "$Myfig_home/`var'", as(png)  replace      restore }


      It worked and stata did not show any errors, but I could not see if the figures were created in a good shape as intended. The generated files were saved as "Data" and not "PNG-Data", although I added "as (png) at the end of the code.

      When I used my first code and only replaced
      Code:
        
       if `var' == treat_city2 {
      with
      Code:
        
       if "`var'" == "treat_city2"
      the first figure for city1 was created but then stata showed an error:


      Code:
      file C:\Users\Dropbox\Uni\Research\Myfig_home
          could not be opened
      r(603);
      
      end of do-file
      
      r(603);
      Thank you in advance for any further help.
      Best
      JL

      Comment


      • #4
        There are various points here but I am not sure that I can help with any.

        You should see a graph every time Stata draws one and you're exporting to $My_fig_home (wherever that is). I necessarily can't advise on what you can and can't do on your own C: drive, or on the C: drive of the computer you're using.

        If you stick closer to your original code then it seems that you need four lines like


        Code:
        if "`var'" == "treat_city1"
        
        if "`var'" == "treat_city2"
        
        if "`var'" == "treat_city3"
        
        if "`var'" == "treat_city4"
        which I don't advise but which seem to match your original intention.

        As pointed out many times here on Statalist it can be hard to help if posters don't give a data example that everyone reading can use.

        Comment


        • #5
          Dear Nick,

          You were right. I do not have to repeat
          Code:
           
           if "`var'" == "treat_cityN"
          for every variable. I have just used your code in the first message and it worked well. The thing is that I just changed this
          Code:
            
            graph export "$Myfig_home/`var'", as(png)  replace
          to this
          Code:
          cd "$Myfig_home"
          graph export `var'.png , replace
          ***Sorry, I did not pay attention to adding data examples. I will certainly do it the next time...***

          Comment

          Working...
          X