<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Merging multiple datasets in SAS using a MACRO - Merge the last dataset to the last MERGED dataset in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Merging-multiple-datasets-in-SAS-using-a-MACRO-Merge-the-last/m-p/766061#M30627</link>
    <description>&lt;P&gt;Hi there:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RE: Merging multiple datasets in SAS using a MACRO - Merge the last dataset to the last MERGED dataset&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Note:&lt;/P&gt;&lt;P&gt;- I'm not stacking the data (so proc append is not required)&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;- &lt;/STRONG&gt;Is there a more eloquent way of writing my SAS code below (The SAS code works but I'm sure there is a simpler way to write a SAS program in order to merge the last dataset to the last MERGED dataset). Any help would be greatly appreciated&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using a macro, where I have a &lt;STRONG&gt;main_dataset (called A)&lt;/STRONG&gt; and &lt;STRONG&gt;other_datasets to merge (called B, C, D, E, F)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The below SAS code that I've worked out (after reading all these forums) is below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I've used the macro below to ensure that when I perform a multiple merge that I &lt;STRONG&gt;"merge the last dataset to the last MERGED dataset"&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run %XXX(A,B); .... to get merged&lt;BR /&gt;run %XXX(merged,C); .... and then the main dataset always becomes merged&lt;BR /&gt;run %XXX(merged,D);&lt;BR /&gt;run %XXX(merged,E);&lt;BR /&gt;run %XXX(merged,F);&lt;BR /&gt;run %XXX(merged,G);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Is there a more eloquent way of doing the code than what's written above &amp;amp; below&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;The SAS code is below&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%macro XXX(main_dataset,other_dataset);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;proc sort data=N.&amp;amp;main_dataset; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;by DSCD year month_name; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;proc sort data=N.&amp;amp;other_dataset.; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;by DSCD year month_name; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;data N.merged;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;merge N.&amp;amp;main_dataset (in=indata)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;N.&amp;amp;other_dataset (in=inb);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;by DSCD year month_name; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;if indata;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;* if indata and inb;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;where DSCD in ('992816');&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%mend; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;%XXX(A,B);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,C);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,D);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,E);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,F);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,G);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;/* Note: I place merged after the ( ...... in order to merge the last dataset to the last MERGED dataset. Is there a better way to do this?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,C);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,D);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,E);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,F);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,G);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;R&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 04 Sep 2021 23:55:28 GMT</pubDate>
    <dc:creator>reubens</dc:creator>
    <dc:date>2021-09-04T23:55:28Z</dc:date>
    <item>
      <title>Merging multiple datasets in SAS using a MACRO - Merge the last dataset to the last MERGED dataset</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-multiple-datasets-in-SAS-using-a-MACRO-Merge-the-last/m-p/766061#M30627</link>
      <description>&lt;P&gt;Hi there:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;RE: Merging multiple datasets in SAS using a MACRO - Merge the last dataset to the last MERGED dataset&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Note:&lt;/P&gt;&lt;P&gt;- I'm not stacking the data (so proc append is not required)&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;- &lt;/STRONG&gt;Is there a more eloquent way of writing my SAS code below (The SAS code works but I'm sure there is a simpler way to write a SAS program in order to merge the last dataset to the last MERGED dataset). Any help would be greatly appreciated&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm using a macro, where I have a &lt;STRONG&gt;main_dataset (called A)&lt;/STRONG&gt; and &lt;STRONG&gt;other_datasets to merge (called B, C, D, E, F)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;The below SAS code that I've worked out (after reading all these forums) is below.&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I've used the macro below to ensure that when I perform a multiple merge that I &lt;STRONG&gt;"merge the last dataset to the last MERGED dataset"&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;run %XXX(A,B); .... to get merged&lt;BR /&gt;run %XXX(merged,C); .... and then the main dataset always becomes merged&lt;BR /&gt;run %XXX(merged,D);&lt;BR /&gt;run %XXX(merged,E);&lt;BR /&gt;run %XXX(merged,F);&lt;BR /&gt;run %XXX(merged,G);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Is there a more eloquent way of doing the code than what's written above &amp;amp; below&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;&lt;STRONG&gt;The SAS code is below&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%macro XXX(main_dataset,other_dataset);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;proc sort data=N.&amp;amp;main_dataset; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;by DSCD year month_name; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;proc sort data=N.&amp;amp;other_dataset.; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;by DSCD year month_name; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;data N.merged;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;merge N.&amp;amp;main_dataset (in=indata)&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;N.&amp;amp;other_dataset (in=inb);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;by DSCD year month_name; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;if indata;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;* if indata and inb;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;where DSCD in ('992816');&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%mend; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;%XXX(A,B);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,C);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,D);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,E);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,F);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,G);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;/* Note: I place merged after the ( ...... in order to merge the last dataset to the last MERGED dataset. Is there a better way to do this?&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,C);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,D);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,E);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,F);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#0000FF"&gt;%XXX(merged,G);&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;R&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 04 Sep 2021 23:55:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-multiple-datasets-in-SAS-using-a-MACRO-Merge-the-last/m-p/766061#M30627</guid>
      <dc:creator>reubens</dc:creator>
      <dc:date>2021-09-04T23:55:28Z</dc:date>
    </item>
    <item>
      <title>Re: Merging multiple datasets in SAS using a MACRO - Merge the last dataset to the last MERGED datas</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-multiple-datasets-in-SAS-using-a-MACRO-Merge-the-last/m-p/766063#M30628</link>
      <description>&lt;P&gt;Merging all the tables at once will be more efficient as you don't have to pass through the data multiple times.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Applying the data selection prior to sorting will also be more efficient because then you only need to sort the selected rows and not the full table.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro prep_tbl(inds,outview);
  proc sql;
    create view &amp;amp;outview as
    select *
    from &amp;amp;inds
    where DSCD ='992816'
    order by DSCD, year, month_name
    ;
  quit;
%mend;
%prep_tbl(a,v_1);
%prep_tbl(b,v_2);
%prep_tbl(c,v_3);
%prep_tbl(d,v_4);
%prep_tbl(e,v_5);
%prep_tbl(f,v_6);

data want;
  merge v_1 (in=v1) v_2 - v_6;
  by DSCD year month_name;
  if v1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 05 Sep 2021 01:04:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-multiple-datasets-in-SAS-using-a-MACRO-Merge-the-last/m-p/766063#M30628</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2021-09-05T01:04:39Z</dc:date>
    </item>
    <item>
      <title>Re: Merging multiple datasets in SAS using a MACRO - Merge the last dataset to the last MERGED datas</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Merging-multiple-datasets-in-SAS-using-a-MACRO-Merge-the-last/m-p/766072#M30629</link>
      <description>That's an amazing program. Thanks very much Patrick for sharing and providing such a speedy response</description>
      <pubDate>Sun, 05 Sep 2021 00:31:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Merging-multiple-datasets-in-SAS-using-a-MACRO-Merge-the-last/m-p/766072#M30629</guid>
      <dc:creator>reubens</dc:creator>
      <dc:date>2021-09-05T00:31:31Z</dc:date>
    </item>
  </channel>
</rss>

