Announcement

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

  • How to use if statement in foreach loop?

    I'm attempting to plot gas concentrations over time, but want to format the graphs differently for some of the gasses. My actually dataset is much larger, and I took a subset for the purposes of this question. I also have other variables in my dataset that are used in the plotting as well. My goal is to have the CO2 and SO2 plots have a different plot title then the rest. The current problem is that my plots for all variables are being exported as if they came from else loop later in my code, which I do not want. Here is what my current code looks like:

    Code:
    local shockvars "rt_inc rt_dec level_inc level_dec"
    
    local gaslist  "co2 so4 n2o ch4 bod"
    local co2_lab "Carbon Dioxide"
    local so4_lab "Sulfur Dioxide"
    ...... 
    ......
    .....
    .....
    
    local shockvars "rt_inc rt_dec level_inc level_dec"
    local rt_inc_lab "..."
    ...
    ...
    ...
    
    foreach lhsvars of local gaslist{
    foreach shockvar of local shockvars {
    [INDENT=2]if inlist("`lhsvar'", "co2", "so2") == 1 {[/INDENT][INDENT=3]twoway (var1 `lhsvar' date) /// title(``lhsvar'_lab, `shockvar'_lab', color(black) size(medium))[/INDENT][INDENT=2]} else {[/INDENT][INDENT=3]twoway (var1 `lhsvar' date) /// title(``lhsvar'_lab', color(black) size(medium))[/INDENT][INDENT=2]}[/INDENT]
    }
    }
    Any suggestions on how to fix this? I can't break the loops up because there are too many variables.

  • #2
    Reading through the minor mess created by your accidental[INDENT] intrusions, I notice:
    1) You refer to "lhsvars" in your -foreach- statement, but "lhsvar" in your code. Abbreviation of locals doesn't work.
    2) Your use of backquotes to dereference your locals looks funny to me at
    title(``lhsvar'_lab, `shockvar'_lab'
    Stata will be trying to dereference a local with a "," in the middle, I think, and the confusion is compounded by the spelling error on lhsvar.

    Setting -trace on- would help reveal the problems here.

    Comment


    • #3
      This is hard to read without copying and pasting and cleaning up.

      I think the first bug here is the opening of the outer loop. which should start foreach lhsvar. I've deleted == 1 as unnecessary.

      Points to Mike Lacy for spotting a problem with local macro names. I've guessed at a fix.


      Code:
      foreach lhsvar of local gaslist {
          foreach shockvar of local shockvars {
              if inlist("`lhsvar'", "co2", "so2") {
                  twoway (var1 `lhsvar'  date) ///
                  title(``lhsvar'_lab', ``shockvar'_lab', color(black) size(medium))
              }
              else {
                  twoway (var1 `lhsvar'  date) ///
                  title(``lhsvar'_lab', color(black) size(medium))
              }
          }
      }
      Last edited by Nick Cox; 01 Nov 2019, 12:06.

      Comment


      • #4
        My apologies with the formatting! I haven't used statalist much, but will be careful about that in the future. Thank you Mike Lacy for the typo catch. In my actual code I use "lhsvar", but typed it wrong here. Your hypothesis about the "," was right, and that's what was breaking.

        Nick Cox using the following for the title of the plot seemed to do the trick! Thank you again.
        Code:
         
         title("``lhsvar'_lab', ``shockvar'_lab'", color(black) size(medium))

        Comment

        Working...
        X