title: How to check if elements of macro are in another macro
body: <p>I have a simple-seeming problem, but in practice it seems to be more involved. In python, for example, it seems like it would be much more straightforward. But I would really like to learn how to do this in Stata.</p>
  
  <p>Say that I have a big dataset. I have several string variables, <code>S1</code>, <code>S2</code>, and <code>S3</code>. I get a subset of <code>S1</code> based on some criteria. Let's say that this gets me (after sorting and only the data of interest are displayed):</p>
  
  <pre><code>    S1
  1   A
  2   B
  3   C
  4   D
  5   E
  </code></pre>
  
  <p>Based on different criteria, I get, for <code>S2</code>:</p>
  
  <pre><code>    S2
  1   B
  2   B
  3   C
  4   F
  </code></pre>
  
  <p>For <code>S3</code>:</p>
  
  <pre><code>    S3
  1   B
  2   Long string
  </code></pre>
  
  <p>What I am interested in doing is to get a list of all of the distinct values across <code>S1</code>, <code>S2</code>, and <code>S3</code>. One way I have thought about doing this is:</p>
  
  <ol>
  <li>Save all desired values of <code>S1</code> into a macro, <code>M1</code>. I didn't figure out how one is able to do this.</li>
  <li>Save all desired values of <code>S2</code> into a macro, <code>M2</code>.</li>
  <li>Check if the values of <code>M2</code> are in <code>M1</code>. Do not add the values of <code>M2</code> to <code>M1</code> that are already in <code>M1</code>, but do add the values of <code>M2</code> to <code>M1</code> that are not already there. It seems like <a href="https://stackoverflow.com/questions/24456229/comparing-elements-of-two-different-local-macros-in-stata">this post</a> is similar to how to do this step. (Why is there a <code>:</code> in front of <code>list</code>?)</li>
  <li>Repeat step 3, except for <code>S3</code>/<code>M3</code> instead of <code>S2</code>/<code>M2</code>.</li>
  </ol>
  
  <p>This would produce the macro <code>M1</code> with values:</p>
  
  <pre><code>A   B   C   D   E   F   Long String
  </code></pre>
  
  <p>Note that I do not need this to be in a macro. If it could be in a matrix or some other way, that would work as well. The important part is to get the information. </p>