title: How to use `if` with local macro in Stata?
body: <p>I am trying to assign values of <code>income</code> in 2012 to locals. So that local <code>val_AK</code> would have value of <code>income</code> in state "AK", <code>val_AL</code> would have value of <code>income</code> in state "AL".... Here is the <a href="http://dl.dropboxusercontent.com/u/41007907/states.dta" rel="nofollow">Stata 11 version of .dta</a> </p>
  
  <p>The following code generates <code>AK not found r(111);</code> However <code>AK</code> does exist in <code>state_nsw</code>, which is a string variable:</p>
  
  <pre><code>foreach i in "AK" "AL" "AR" "AZ" {
       if (state_nsw=="`i'" &amp; year==2012) { local val_`i'=income}
       }
  </code></pre>
  
  <p>I also tried using <a href="http://www.stata.com/statalist/archive/2004-03/msg00928.html" rel="nofollow">this method</a> and this:</p>
  
  <pre><code>if (state_nsw=="`i'" ...
  ...
  // and got invalid name r(198)
  </code></pre>
  
  <p>Where is my mistake?</p>