title: Append new column to existing table using SAS
body: <p>I have a <code>do loop</code> in which I do calculation on new variable and results are stored as additional column, this column-s (at each iteration) should be attached to the output table defined by macro.</p>
  <p><a href="https://stackoverflow.com/questions/28222386/insert-into-specific-column-from-other-table">Here on SO</a> something similar has been asked but the answer is not acceptable, the last answer is not compatible with <code>sas</code> command but very close, getting incomplete script with following:</p>
  <pre><code>    proc sql;
           update &amp;outlib..&amp;out. 
           set var._iqr = b.&amp;var._iqr
           from &amp;outlib..&amp;out. as a 
           left join cal_resul as b
           on a.id_client=b.id_client 
           and a.reference_date=b.reference_date; 
      quit;
  </code></pre>
  <p>Here is my attempt which works but very slow:</p>
  <pre><code>    proc sql; create table &amp;outlib..&amp;out. as select * from &amp;inlib..&amp;in.; quit; /* the input is as a basis for output table */
      proc sql; alter table &amp;outlib..&amp;out. add &amp;var._iqr numeric; quit; /* create empty column to be filled at each iteration */
  
      proc sql;
           update &amp;outlib..&amp;out. as a
           set &amp;var._iqr=(select b.&amp;var._iqr from cal_resul as b
           where a.id_client=b.id_client 
           and a.reference_date=b.reference_date 
           and a.data_source=b.data_source);
      quit;
  </code></pre>
  <p>Attempt 2:</p>
  <p>This is somewhat faster:</p>
  <pre><code>    proc sort data=cal_resul; by id_client reference_date data_source; run;
  
      data &amp;outlib..&amp;out.;
           update &amp;outlib..&amp;out. cal_resul; 
           by id_client reference_date data_source;
      run;
  </code></pre>
  <p>Simple left join (adding new column into existing table is way faster) but with left join I did not figure out how I can update (always retain the same dataset) the <code>&amp;outlib..&amp;out.</code> at each iteration. Many thanks for any help;</p>