<?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: Merge multiple datasets with one dataset using macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/471994#M120959</link>
    <description>&lt;P&gt;Instead of creating data.merged in the merge step, create work.merged, and then append it to data.merged within the macro loop.&lt;/P&gt;</description>
    <pubDate>Thu, 21 Jun 2018 07:16:07 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-06-21T07:16:07Z</dc:date>
    <item>
      <title>Merge multiple datasets with one dataset using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/471989#M120957</link>
      <description>&lt;P&gt;Hi there,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS 9.4.&lt;/P&gt;&lt;P&gt;I have multiple datasets titled as 2007q1, 2007q2, 2007q3, ..., 2017q3, 2017q4. I also have a dataset titled Cohort.&amp;nbsp;All dataset contain the variable patid.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to merge all those &amp;amp;yr.q&amp;amp;qr. datasets with the dataset Cohort by the common variable patid. There should be only 1 final merged dataset. The merged file should contain all and only patids that are in the dataset Cohort.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my code:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mergedata;
	proc sort data=data.cohort; by patid; run;
	
	%do yr=2007 %to 2017;
		%do qr=1 %to 4;
			proc sort data=in1perc.ses_diag&amp;amp;yr.q&amp;amp;qr.; 
				by patid; 
			run;
			data data.merged;
				merge
				data.cohort (in=ina)
				in1perc.&amp;amp;yr.q&amp;amp;qr. (in=inb);
				by patid;
				if ina and inb;
			run;
		%end;
	%end;
%mend;

%mergedata&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I know there is something wrong with the ina and inb part, but I cannot figure out how to fix it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The log file shows that the next dataset in the loop keeps replacing the previous merged dataset, instead of adding into it.&lt;/P&gt;&lt;P&gt;Here is the log for iterations 2007Q4 and 2008Q1. Note how the total observations has decreased in data.Merged.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: There were 1687 observations read from the data set DATA.COHORT.
NOTE: There were 712195 observations read from the data set IN1PERC.2007Q4.
NOTE: The data set DATA.MERGED has 7185 observations and 17 variables.&lt;/CODE&gt;&lt;/PRE&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;NOTE: There were 1687 observations read from the data set DATA.COHORT.
NOTE: There were 731171 observations read from the data set IN1PERC.2008Q1.
NOTE: The data set DATA.MERGED has 7031 observations and 17 variables.&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;How do I fix this? Thank you for your help!&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 06:45:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/471989#M120957</guid>
      <dc:creator>zzfsimona</dc:creator>
      <dc:date>2018-06-21T06:45:08Z</dc:date>
    </item>
    <item>
      <title>Re: Merge multiple datasets with one dataset using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/471994#M120959</link>
      <description>&lt;P&gt;Instead of creating data.merged in the merge step, create work.merged, and then append it to data.merged within the macro loop.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 07:16:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/471994#M120959</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-06-21T07:16:07Z</dc:date>
    </item>
    <item>
      <title>Re: Merge multiple datasets with one dataset using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/472004#M120966</link>
      <description>&lt;P&gt;I haven't tested this as in a meeting, however maybe something like - needs all datasets in one library:&lt;/P&gt;
&lt;PRE&gt;data merged;
  merge in1perc.ses_diag:;
  by patid;
  if ina and inb;
run;&lt;/PRE&gt;
&lt;P&gt;Note the : after the prefix part, normally it takes anything with the prefix, but not sure about merge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Question is why do you have lots of same data in dated files in the first place?&amp;nbsp; Every time when I see this I immediately think that the process before could be improved to give one dataset with date in the data - makes everyones life so much easier.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 08:05:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/472004#M120966</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-06-21T08:05:58Z</dc:date>
    </item>
    <item>
      <title>Re: Merge multiple datasets with one dataset using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/472036#M120974</link>
      <description>&lt;P&gt;If I'm interpreting the question correctly, you would need to make this sort of change.&amp;nbsp; First, delete your data.merged data set.&amp;nbsp; Then change the last part of your loop where you now have:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;merged&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;merge&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;cohort &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;in&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;ina&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt; in1perc&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;yr&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;q&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;qr&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;in&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;inb&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;BR /&gt;&lt;/SPAN&gt; &lt;SPAN class="token statement"&gt;by&lt;/SPAN&gt; patid&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; ina and inb&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt; &lt;BR /&gt;&lt;SPAN class="token procnames"&gt;run&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;CODE&gt;&lt;/CODE&gt;&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Instead, replace that section with:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data merged;
merge data.cohort (in=ina)
      in1perc.&amp;amp;yr.q&amp;amp;qr. (in=inb);
      by patid;
      if ina and inb;
run;
proc append data=merged base=data.merged;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You can run this, even when DATA.MERGED does not yet exist.&amp;nbsp; For that case, MERGED will get copied to DATA.MERGED.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 10:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/472036#M120974</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-06-21T10:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: Merge multiple datasets with one dataset using macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/472139#M121003</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/214679"&gt;@zzfsimona&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi there,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am using SAS 9.4.&lt;/P&gt;
&lt;P&gt;I have multiple datasets titled as 2007q1, 2007q2, 2007q3, ..., 2017q3, 2017q4. I also have a dataset titled Cohort.&amp;nbsp;All dataset contain the variable patid.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to merge all those &amp;amp;yr.q&amp;amp;qr. datasets with the dataset Cohort by the common variable patid. There should be only 1 final merged dataset. The merged file should contain all and only patids that are in the dataset Cohort.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is my code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro mergedata;
	proc sort data=data.cohort; by patid; run;
	
	%do yr=2007 %to 2017;
		%do qr=1 %to 4;
			proc sort data=in1perc.ses_diag&amp;amp;yr.q&amp;amp;qr.; 
				by patid; 
			run;
			data data.merged;
				merge
				data.cohort (in=ina)
				in1perc.&amp;amp;yr.q&amp;amp;qr. (in=inb);
				by patid;
				if ina and inb;
			run;
		%end;
	%end;
%mend;

%mergedata&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I know there is something wrong with the ina and inb part, but I cannot figure out how to fix it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The log file shows that the next dataset in the loop keeps replacing the previous merged dataset, instead of adding into it.&lt;/P&gt;
&lt;P&gt;Here is the log for iterations 2007Q4 and 2008Q1. Note how the total observations has decreased in data.Merged.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;These two lines don't match:&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token procnames"&gt;proc&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;sort&lt;/SPAN&gt; &lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;in1perc&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;ses_diag&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;yr&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;q&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;qr&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&lt;BR /&gt; in1perc&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;yr&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;q&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;qr&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt; &lt;SPAN class="token punctuation"&gt;(&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;in&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt;inb&lt;SPAN class="token punctuation"&gt;)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN class="token punctuation"&gt;did you mean to use the sored data? as the data set names are not the same.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I suggest that you do with two data sets manually and see if the results are as desired. I suspect not as MERGE is going to replace like-named variable values.&lt;/P&gt;
&lt;P&gt;I suspect that what you actually want to do is append all of the 2007q1, 2007q2 etc sets together and then select.&lt;/P&gt;
&lt;P&gt;Some thing like&lt;/P&gt;
&lt;PRE&gt;data work.temp;
   set  in1perc.ses_diag: ;
run;

proc sql;
   create table data.merged as
   select b.*
   from data.cohort as a
        left join
        work.temp
        on a.patid=b.patid
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;No claims for efficiency except in short code if this actually accomplishes what you want.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The &lt;FONT color="#0000ff" face="SAS Monospace" size="2"&gt;set&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt; in1perc.ses_diag: ; will append all data sets in the in1perc library that start with ses_diag. &lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 21 Jun 2018 15:06:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-multiple-datasets-with-one-dataset-using-macro/m-p/472139#M121003</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-06-21T15:06:48Z</dc:date>
    </item>
  </channel>
</rss>

