Announcement

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

  • How to make a loop continue working if a variable it is not found?

    Hello.

    I am trying to generate graphs from some variables. I do not have all possible combinations of v iv p s. I want Stata to generate graphs with all combinations I have but without stopping and returning error.

    This is the code I am using:

    foreach v in bi y fi w ti {
    foreach s in qe nqe {
    foreach pi in t3m t6m t1y t2y t3y t5y t7y t10y {
    foreach iv in ed1 ed2 ed3 ed4 ff1 ff2 ff3 ff4 mp1 mp2 mp3 mp4 pc1 {
    qui: twoway (rarea min_`v'_`pi'_`iv'_`s'_68 max_`v'_`pi'_`iv'_`s'_68 horizon , pstyle(ci)) ///
    (line max_`v'_`pi'_`iv'_`s'_90 horizon, lcolor(black) lpattern(dash) lstyle(ci) ) ///
    (line min_`v'_`pi'_`iv'_`s'_90 horizon, lcolor(black) lpattern(dash) lstyle(ci) ) ///
    (line b_`v'_`pi'_`iv'_`s'_90 horizon, lcolor(red) lpattern(solid)), ///
    title("`v'", color(black) size(medium)) ///
    ytitle("Percent", size(medsmall)) xtitle("Horizon", size(medsmall)) ///
    graphregion(color(white)) plotregion(color(white)) ///
    legend(off) ///
    title("GK shocks: `v'_`pi'_`iv'_`s'")
    graph save "C:\****\irf_`pi'_`iv'_`v'_`s'.gph", replace
    }
    }
    }
    }


    Thank you for your help.

  • #2
    Code:
    help capture 
    help confirm

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Code:
      help capture
      help confirm
      Thank you for helping,

      I do not unterstand how to do it, could you be more specific please?

      Comment


      • #4
        The help and manual entries really are quite detailed.

        You can (e.g.) confirm that the variables you need exist and ask for the graph if and only if they do. Suppose you need frog and toad to draw a graph:

        Code:
        capture confirm variable frog toad 
        if _rc == 0 scatter frog toad
        In your case you're inside a loop but you just feed the names to confirm that you would feed to twoway immediately afterwards.

        Comment


        • #5
          Originally posted by Nick Cox View Post
          The help and manual entries really are quite detailed.

          You can (e.g.) confirm that the variables you need exist and ask for the graph if and only if they do. Suppose you need frog and toad to draw a graph:

          Code:
          capture confirm variable frog toad
          if _rc == 0 scatter frog toad
          In your case you're inside a loop but you just feed the names to confirm that you would feed to twoway immediately afterwards.
          Hello! I tried what you told me using the code below, and it still says "variable not found" for the variables I dont have. What fo you thing is wrong?

          Code:
          foreach v in vix  {
          foreach s in qe nqe{
          foreach pi in t3m t6m t1y t2y t3y t5y t7y t10y { 
          foreach iv in ed1 ed2 ed3 ed4 ff1 ff2 ff3 ff4 mp1 mp2 mp3 mp4 pc1 { 
          capture confirm variable min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 min_`v'_`pi'_`iv'_`s'_90  max_`v'_`pi'_`iv'_`s'_90 b_`v'_`pi'_`iv'_`s'_90
          if _rc == 0 scatter min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 min_`v'_`pi'_`iv'_`s'_90  max_`v'_`pi'_`iv'_`s'_90 b_`v'_`pi'_`iv'_`s'_90
          qui: twoway (rarea min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 horizon , pstyle(ci)) ///
            (line max_`v'_`pi'_`iv'_`s'_90  horizon, lcolor(black) lpattern(dash) lstyle(ci) ) ///
            (line min_`v'_`pi'_`iv'_`s'_90  horizon, lcolor(black) lpattern(dash) lstyle(ci)  ) /// 
          (line b_`v'_`pi'_`iv'_`s'_90 horizon, lcolor(red) lpattern(solid)),  ///
          title("`v'", color(black) size(medium)) ///
              ytitle("Percent", size(medsmall)) xtitle("Horizon", size(medsmall)) ///
              graphregion(color(white)) plotregion(color(white)) ///
              legend(off) ///
              note("Shaded area IC 90% and dashed line IC 68%") ///
          title("Business Income (`pi',`iv',`s')")
          graph save  "C:\Users\q63909\Desktop\hola\irf_`pi'_`iv'_`v'_`s'.gph", replace 
          }
          }
          }
          }
          Thank you!!

          Comment


          • #6
            I'm not 100% sure this does what you want. But the brackets for the if statement are needed in any case. Also much mroe legible if you use indentation per loop.

            Code:
            *some data
            clear
            set obs 100
            foreach v in vix  {
                foreach s in qe nqe{
                    foreach pi in t3m t6m {
                        foreach iv in ed1 ed2 {
                        gen min_`v'_`pi'_`iv'_`s'_68 = runiform()
                        gen max_`v'_`pi'_`iv'_`s'_68 = runiform()
                        gen min_`v'_`pi'_`iv'_`s'_90 = runiform()
                        gen max_`v'_`pi'_`iv'_`s'_90 = runiform()
                        gen b_`v'_`pi'_`iv'_`s'_90 = runiform()
                        }
                    }
                }
            }
            gen horizon = runiform()
            
            foreach v in vix  {
                foreach s in qe nqe{
                    foreach pi in t3m t6m t1y t2y t3y t5y t7y t10y {
                        foreach iv in ed1 ed2 ed3 ed4 ff1 ff2 ff3 ff4 mp1 mp2 mp3 mp4 pc1 {
                        capture confirm variable min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 min_`v'_`pi'_`iv'_`s'_90  max_`v'_`pi'_`iv'_`s'_90 b_`v'_`pi'_`iv'_`s'_90
                        if _rc == 0 {
                            scatter min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 min_`v'_`pi'_`iv'_`s'_90  max_`v'_`pi'_`iv'_`s'_90 b_`v'_`pi'_`iv'_`s'_90
                            qui: twoway (rarea min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 horizon , pstyle(ci)) ///
                              (line max_`v'_`pi'_`iv'_`s'_90  horizon, lcolor(black) lpattern(dash) lstyle(ci) ) ///
                              (line min_`v'_`pi'_`iv'_`s'_90  horizon, lcolor(black) lpattern(dash) lstyle(ci)  ) ///
                              (line b_`v'_`pi'_`iv'_`s'_90 horizon, lcolor(red) lpattern(solid)),  ///
                            title("`v'", color(black) size(medium)) ///
                                ytitle("Percent", size(medsmall)) xtitle("Horizon", size(medsmall)) ///
                                graphregion(color(white)) plotregion(color(white)) ///
                                legend(off) ///
                                note("Shaded area IC 90% and dashed line IC 68%") ///
                            title("Business Income (`pi',`iv',`s')")
                            graph save  "C:\Users\q63909\Desktop\hola\irf_`pi'_`iv'_`v'_`s'.gph", replace
                            }
                        }
                    }
                }
            }
            Last edited by Jorrit Gosens; 20 Feb 2019, 03:29.

            Comment


            • #7
              Hello. I added the bracket, but it still does not work.

              I explain better what I want. I only have some combinations (e.g. min_vix_t3m_ed1_qe_68 ) but I do not have all possible iterations and when the loop arrives at a variable that does not exist in my dataset it stops. What I want is to make graphs with the combinations I have and just skip the other ones (but not having a return error).

              Code:
              foreach v in vix  {
              foreach s in qe nqe{
              foreach pi in t3m t6m t1y t2y t3y t5y t7y t10y { 
              foreach iv in ed1 ed2 ed3 ed4 ff1 ff2 ff3 ff4 mp1 mp2 mp3 mp4 pc1 { 
               capture confirm variable min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 min_`v'_`pi'_`iv'_`s'_90  max_`v'_`pi'_`iv'_`s'_90 b_`v'_`pi'_`iv'_`s'_90
              if _rc == 0 scatter min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 min_`v'_`pi'_`iv'_`s'_90  max_`v'_`pi'_`iv'_`s'_90 b_`v'_`pi'_`iv'_`s'_90 {
              qui: twoway (rarea min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 horizon , pstyle(ci)) ///
                (line max_`v'_`pi'_`iv'_`s'_90  horizon, lcolor(black) lpattern(dash) lstyle(ci) ) ///
                (line min_`v'_`pi'_`iv'_`s'_90  horizon, lcolor(black) lpattern(dash) lstyle(ci)  ) /// 
              (line b_`v'_`pi'_`iv'_`s'_90 horizon, lcolor(red) lpattern(solid)),  ///
              title("`v'", color(black) size(medium)) ///
                  ytitle("Percent", size(medsmall)) xtitle("Horizon", size(medsmall)) ///
                  graphregion(color(white)) plotregion(color(white)) ///
                  legend(off) ///
                  note("Shaded area IC 90% and dashed line IC 68%") ///
              title("Business Income (`pi',`iv',`s')")
              graph save  "C:\Users\q63909\Desktop\hola\irf_`pi'_`iv'_`v'_`s'.gph", replace 
              }
              }
              }
              }
              }
              Thank you for helping!!!

              Comment


              • #8
                Have another look at where the opening bracket goes in post #6
                Code:
                 if _rc == 0 {
                scatter min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 min_`v'_`pi'_`iv'_`s'_90  max_`v'_`pi'_`iv'_`s'_90 b_`v'_`pi'_`iv'_`s'_90

                Comment


                • #9
                  I changed the opening bracket, now, it does not return error but does nothing, it does not generate any graph.
                  Code:
                  foreach v in vix  {
                  foreach s in qe nqe{
                  foreach pi in t3m t6m t1y t2y t3y t5y t7y t10y { 
                  foreach iv in ed1 ed2 ed3 ed4 ff1 ff2 ff3 ff4 mp1 mp2 mp3 mp4 pc1 { 
                   capture confirm variable min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 min_`v'_`pi'_`iv'_`s'_90  max_`v'_`pi'_`iv'_`s'_90 b_`v'_`pi'_`iv'_`s'_90
                  if _rc == 0 {
                   scatter min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 min_`v'_`pi'_`iv'_`s'_90  max_`v'_`pi'_`iv'_`s'_90 b_`v'_`pi'_`iv'_`s'_90 
                  qui: twoway (rarea min_`v'_`pi'_`iv'_`s'_68  max_`v'_`pi'_`iv'_`s'_68 horizon , pstyle(ci)) ///
                    (line max_`v'_`pi'_`iv'_`s'_90  horizon, lcolor(black) lpattern(dash) lstyle(ci) ) ///
                    (line min_`v'_`pi'_`iv'_`s'_90  horizon, lcolor(black) lpattern(dash) lstyle(ci)  ) /// 
                  (line b_`v'_`pi'_`iv'_`s'_90 horizon, lcolor(red) lpattern(solid)),  ///
                  title("`v'", color(black) size(medium)) ///
                      ytitle("Percent", size(medsmall)) xtitle("Horizon", size(medsmall)) ///
                      graphregion(color(white)) plotregion(color(white)) ///
                      legend(off) ///
                      note("Shaded area IC 90% and dashed line IC 68%") ///
                  title("Business Income (`pi',`iv',`s')")
                  graph save  "C:\Users\q63909\Desktop\falsa\irf_`pi'_`iv'_`v'_`s'.gph", replace 
                  }
                  }
                  }
                  }
                  }
                  Thanks!!

                  Comment


                  • #10
                    Does the folder "C:\Users\q63909\Desktop\falsa" exist?
                    Don't see an issue otherwise. Works fine on my machine as long as I point it to a folder that exists.

                    Comment


                    • #11
                      Yes, this folder exists. First time I ran it, it worked, but later it did not run any more. I don`t understand why. I tried to add something with no sense in the loop (abcd123) to see if it returns error and it does not. It just do nothing
                      Thanks
                      Last edited by Alberto Lopez; 20 Feb 2019, 04:19.

                      Comment


                      • #12
                        If I run the exact code provided in post #6, it runs. First time around, and again if I run it again.
                        You'll have to be more specific to diagnose this. Are you sure you are not simply seeing your graphs overwritten?
                        If 'it doesn't work', then what is the error code returned? Current code would not run without any error code being given, with the exception of a situation where none of the variables specified is found.

                        Comment

                        Working...
                        X