<?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 Re: Using DO loop to merge multiple datasets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682340#M206536</link>
    <description>Are you trying to append data sets together or merge them - these are not the same things. MERGE means adding columns typically whereas an append stacks data. &lt;BR /&gt;&lt;BR /&gt;If you trace your code through five loops you'll find it's overwriting itself all the time. So what are you trying to do here?</description>
    <pubDate>Tue, 08 Sep 2020 17:36:59 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2020-09-08T17:36:59Z</dc:date>
    <item>
      <title>Using DO loop to merge multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682320#M206530</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;I am trying to use DO LOOP to merge&amp;nbsp;multiple datasets with different names.&amp;nbsp;&amp;nbsp;&amp;nbsp;But the coding is not working, please help.&amp;nbsp; Thanks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%let Y1=ulung;
%let Y2=uheme;
%let Y3=ucancer;
%let Y4=utransplant;
%let Y5=ugm;

%macro merge;
	%do i = 1 %to 5;
         data Underlying;
	 set &amp;amp;&amp;amp;Y&amp;amp;i.;
          run;
	
	%end;
%mend;
%merge;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Sep 2020 17:09:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682320#M206530</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2020-09-08T17:09:07Z</dc:date>
    </item>
    <item>
      <title>Re: Using DO loop to merge multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682336#M206535</link>
      <description>&lt;P&gt;Your macro will create 5 data steps, all writing (and thereby &lt;STRONG&gt;over&lt;/STRONG&gt;writing) the same dataset. So in the end, dataset underlying will hold the contents of ugm.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From the code, I have no idea what you want to achieve, but it sure ain't a merge.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 17:34:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682336#M206535</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-09-08T17:34:30Z</dc:date>
    </item>
    <item>
      <title>Re: Using DO loop to merge multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682340#M206536</link>
      <description>Are you trying to append data sets together or merge them - these are not the same things. MERGE means adding columns typically whereas an append stacks data. &lt;BR /&gt;&lt;BR /&gt;If you trace your code through five loops you'll find it's overwriting itself all the time. So what are you trying to do here?</description>
      <pubDate>Tue, 08 Sep 2020 17:36:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682340#M206536</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-09-08T17:36:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using DO loop to merge multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682342#M206538</link>
      <description>&lt;P&gt;The set statement will accept multiple data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data Underlying;
   set ulung uheme ucancer utransplant ugm;&lt;/PRE&gt;
&lt;P&gt;So no macro needed.&lt;/P&gt;
&lt;P&gt;If the sets are the only ones in your library that start with a common "stem" you can use the : list operator:&lt;/P&gt;
&lt;PRE&gt;data underlying;
   set u: ;
run;&lt;/PRE&gt;
&lt;P&gt;And if you really must have a macro this makes a SET statement like the first example above.&lt;/P&gt;
&lt;PRE&gt;%macro merge;
	
   data Underlying;
      set 
      %do i = 1 %to 5;
        &amp;amp;&amp;amp;Y&amp;amp;i.
      %end;
      ; /*&amp;lt;= this ; ends the SET statement*/
   run;
	
%mend;&lt;/PRE&gt;
&lt;P&gt;Since your macro was generating code like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set a;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; set b;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Do you see why your output was always the last data set? And you really need to provide a better description than "is not working".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is often less than best practice to have macro variables appear in the body of a macro without being parameters. Otherwise it can be pretty hard to find where the values are coming from when "something is not working".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;By the way, have you figured out how to use option MPRINT yet? I believe it has been suggested several times as a basic tool for debugging macro issues.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Sep 2020 17:44:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682342#M206538</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2020-09-08T17:44:29Z</dc:date>
    </item>
    <item>
      <title>Re: Using DO loop to merge multiple datasets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682389#M206551</link>
      <description>Thank you so much! I didn't know there is a tricky way to use 'U:' to save tying my 15 dataset names one by one.</description>
      <pubDate>Tue, 08 Sep 2020 19:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Using-DO-loop-to-merge-multiple-datasets/m-p/682389#M206551</guid>
      <dc:creator>ybz12003</dc:creator>
      <dc:date>2020-09-08T19:45:58Z</dc:date>
    </item>
  </channel>
</rss>

