Announcement

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

  • if statement in loop

    Hi,

    I'm constructing plots for different variables using a loop. There is one observation per country. For each variable I want to plot there are certain, different, outliers which I would like to drop before I plot. E.g. if I plot GDP I would like to drop the observation for Malta. How can I implement this in the following loop?

    foreach var2 in GDP LABOR WAGE {

    graph twoway scatter `var2' h2m
    }

  • #2
    This seems backwards to me. If by "drop" you mean "omit an observation from the graph" but the observation differs according to variable, then the implication is that you are using an inappropriate scale for graphics. GDP is a classic case, as its magnitude varies by several orders over the range from small countries to large countries (and because GDP per head is hardly constant). Hence logarithmic scale for GDP is almost always not just convenient but indeed natural.

    In your example I doubt that there is a clearer or faster way to do what you are asking than writing out three statements with different if qualifiers,

    Comment


    • #3
      If you must do it, you could use something like the following code:

      Code:
      local ifcond_GDP country != "Malta"
      local ifcond_LABOR !inlist(country, "Country1", "Country2", Country3")
      local ifcond_WAGE !inlist(country, "Country4", "Country5")
      
      foreach var2 in GDP LABOR WAGE {
          graph twoway scatter `var2' h2m if `ifcond_`var2''
      }

      Comment

      Working...
      X