title: SAS multiple dataset and Coalesce function
body: <p>Good day, </p>
  
  <p>I have old data with different format and updating it with new. Idea is that variable <code>WantedName</code> had been written with underscore <code>Wanted_Name</code> whereas new data is without this. No big deal I though.  </p>
  
  <p>EDIT: <code>WantedName</code> and <code>Wanted_Name</code> are SAS dates.</p>
  
  <p>Original data: </p>
  
  <pre><code>year Wanted_Name 
  2013  1234       
  2013  4321       
  2013  3241       
  </code></pre>
  
  <p>and from year 2015 forward: </p>
  
  <pre><code>year WantedName 
  2015 5678     
  2015 8765
  ....
  </code></pre>
  
  <p>I tried the logic:</p>
  
  <pre><code>%macro macro_env;
  data _null_; 
      call symput ("curr_year", year(date()) ); 
  run;
  
  data long_data;
      set 
      %do year=2013 %to &amp;curr_year.;
          data.history&amp;year.
      %end;
      ;
      WantedName=COALESCE(WantedName, Wanted_Name);
  run;
  %mend macro_env;
  </code></pre>
  
  <p>Nope. Did not work. For some reason it took the first value of <code>Wanted_Name</code> and pasted that over the whole data range except for the new data: </p>
  
  <pre><code>year Wanted_Name WantedName
  2013  1234        1234
  2013  4321        1234
  2013  3241        1234
  ....
  2015  .           5678     
  2015  .           8765
  </code></pre>
  
  <p>Now I managed to fix the issue by removing the <code>coalesce</code> function and adding a additional data statement: </p>
  
  <pre><code>data long_data;
      set 
      %do year=2013 %to &amp;curr_year.;
          data.history&amp;year.
      %end;
      ;
  run;
  
  data long_data;
      set long_data;
      WantedName=COALESCE(WantedName, Wanted_Name);
  run;
  </code></pre>
  
  <p>Question: <strong>What happens or why isn't the original macro_env working?</strong> </p>
  
  <p>I thought datasets in <code>set</code> were first loaded and then function applied. (Which is working in the latter data statement. Apparently not... Maybe?</p>