<?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: How do I use a SAS macro to merge and then concatenate multiple datasets? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-SAS-macro-to-merge-and-then-concatenate-multiple/m-p/513714#M138444</link>
    <description>&lt;P&gt;Though I don't have your&amp;nbsp;data, hope this helps.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*A sample dataset that mimics the shape of your dataset*/

%macro repeat1;

%do repeat=2000 %to 2010;

data clin_&amp;amp;repeat._phys_exam clin_&amp;amp;repeat._lab(drop=id) clin_&amp;amp;repeat._group(drop=form) clin_&amp;amp;repeat._final(drop=form);
	do form=1,2;
		do id=1,2;
			output clin_&amp;amp;repeat._phys_exam;
			if id=1 then output clin_&amp;amp;repeat._lab;
			if form=1 then output clin_&amp;amp;repeat._group;
			if form=1 then output clin_&amp;amp;repeat._final;
		end;
	end;
run;

%end;

%mend;

%repeat1;

/*The code that does what you need*/

%macro repeat2;

%do repeat=2000 %to 2010;

proc sort data=clin_&amp;amp;repeat._phys_exam;
	by form;
run;

proc sort data=clin_&amp;amp;repeat._lab;
	by form;
run;

data phys_lab_&amp;amp;repeat.;
	merge clin_&amp;amp;repeat._phys_exam clin_&amp;amp;repeat._lab;
	by form;
run;

proc sort data=phys_lab_&amp;amp;repeat.;
	by id;
run;

proc sort data=clin_&amp;amp;repeat._group;
	by id;
run;

proc sort data=clin_&amp;amp;repeat._final;
	by id;
run;

data phys_lab_&amp;amp;repeat.;
	merge phys_lab_&amp;amp;repeat. clin_&amp;amp;repeat._group clin_&amp;amp;repeat._final;
	by id;
run;

proc append base=year2000_year2010 data=phys_lab_&amp;amp;repeat.;
run;

%end;

%mend;

%repeat2;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Though this may not be the best solution, this may do something you mentioned.&lt;/P&gt;</description>
    <pubDate>Thu, 15 Nov 2018 23:26:30 GMT</pubDate>
    <dc:creator>Junyong</dc:creator>
    <dc:date>2018-11-15T23:26:30Z</dc:date>
    <item>
      <title>How do I use a SAS macro to merge and then concatenate multiple datasets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-SAS-macro-to-merge-and-then-concatenate-multiple/m-p/513702#M138438</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working with multiple data sets. There are a total of 4 data sets per year (~250 variables total across all 4 datasets and I have to keep all of them),and I have 10 years of data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;I need help coming up with a macro to minimize the code I have to write.&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is a list of the 4 data sets for year 2000 (I have 4 data sets each for years 2000-2015):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;*List of data sets for year 2000;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;clin_2000_phys_exam&lt;/P&gt;&lt;P&gt;clin_2000_lab&lt;/P&gt;&lt;P&gt;clin_2000_group&lt;/P&gt;&lt;P&gt;clin_2000_final&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;EM&gt;*List of data sets for year 2001;&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;clin_2001_phys_exam&lt;/P&gt;&lt;P&gt;clin_2001_lab&lt;/P&gt;&lt;P&gt;clin_2001_group&lt;/P&gt;&lt;P&gt;clin_2001_final&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;......and this repeats&amp;nbsp;until we reach year 2010&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I am trying to do is something like this for each year:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;*Merging "clin_2000_phys_exam"&amp;nbsp;data with "clin_2000_lab"&amp;nbsp;data by form;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;proc sort data=&lt;SPAN&gt;clin_2000_phys_exam;&amp;nbsp;&lt;/SPAN&gt;by form; run;&lt;/P&gt;&lt;P&gt;proc sort data=&lt;SPAN&gt;clin_2000_lab; by form; run;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data phys_lab_2000;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;merge&amp;nbsp;&lt;EM&gt;clin_2000_phys_exam&lt;/EM&gt; &amp;nbsp;&lt;EM&gt;clin_2000_lab&lt;/EM&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;by form;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;*Merging the newly created "phys_lab_2000" data with "clin_2000_group" and "clin_2000_final" data by ID;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;proc sort data=phys_lab_2000&lt;/SPAN&gt;&lt;SPAN&gt;; by ID; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;proc sort data=&lt;SPAN&gt;clin_2000_group; by ID; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;proc sort data=clin_2000_final; by ID; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data 2000_final data;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;merge&amp;nbsp;&lt;EM&gt;phys_lab_2000&lt;/EM&gt;&amp;nbsp;&lt;EM&gt;clin_2000_group&lt;/EM&gt;&amp;nbsp;&lt;EM&gt;clin_2000_final&lt;/EM&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;by ID;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;*Repeat the code above for all 10 years (years 2000-2010) until each year has a merged final dataset;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;*Concatenate data together to create a long dataset that contains data for all 10 years;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;data year200_year2011;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;set 2000_final data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2001_final data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2002_final data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2003_final data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2004_final data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2005_final data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2006_final data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2007_final data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2008_final data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2009_final data&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;20010_final data;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I need help coming up with a SAS macro that does this so that I don't have to rewrite the same line of code for each year since the only difference would be the name of the dataset.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I appreciate any help you can provide.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 22:52:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-SAS-macro-to-merge-and-then-concatenate-multiple/m-p/513702#M138438</guid>
      <dc:creator>lousam</dc:creator>
      <dc:date>2018-11-15T22:52:39Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a SAS macro to merge and then concatenate multiple datasets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-SAS-macro-to-merge-and-then-concatenate-multiple/m-p/513708#M138442</link>
      <description>&lt;P&gt;It looks like you know precisely what code needs to run for each year.&amp;nbsp; Here's an example of turning it into a macro.&amp;nbsp; I'll start with a piece of the code you supplied:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;proc sort data=phys_lab_2000&lt;/SPAN&gt;&lt;SPAN&gt;; by ID; run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;proc sort data=&lt;SPAN&gt;clin_2000_group; by ID; run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;proc sort data=clin_2000_final; by ID; run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data 2000_final data;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;merge&amp;nbsp;&lt;EM&gt;phys_lab_2000&lt;/EM&gt;&amp;nbsp;&lt;EM&gt;clin_2000_group&lt;/EM&gt;&amp;nbsp;&lt;EM&gt;clin_2000_final&lt;/EM&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;by ID;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;You might want to add to that code, since you have indicated that more processing does take place for each year.&amp;nbsp; Turning it into a macro that processes one year would look like this.&amp;nbsp; It's as simple as replacing "2000" with "&amp;amp;year.":&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%macro one_year (year=);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;proc sort data=phys_lab_&amp;amp;year.&lt;/SPAN&gt;&lt;SPAN&gt;; by ID; run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;proc sort data=&lt;SPAN&gt;clin_&amp;amp;year._group; by ID; run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;proc sort data=clin_&amp;amp;year._final; by ID; run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data &amp;amp;year._final data;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;merge&amp;nbsp;&lt;EM&gt;phys_lab_&amp;amp;year.&lt;/EM&gt;&amp;nbsp;&lt;EM&gt;clin_&amp;amp;year._group&lt;/EM&gt;&amp;nbsp;&lt;EM&gt;clin_&amp;amp;year._final&lt;/EM&gt;;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;by ID;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%mend one_year;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Then actually executing the code looks like this:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%one_year (year=2000)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;%one_year (year=2001)&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;etc.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Note that you will need to change the names of one of your data sets.&amp;nbsp; Data set names cannot begin with a number, so this would be an invalid name:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2000_final data&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 23:17:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-SAS-macro-to-merge-and-then-concatenate-multiple/m-p/513708#M138442</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-11-15T23:17:24Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a SAS macro to merge and then concatenate multiple datasets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-SAS-macro-to-merge-and-then-concatenate-multiple/m-p/513714#M138444</link>
      <description>&lt;P&gt;Though I don't have your&amp;nbsp;data, hope this helps.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*A sample dataset that mimics the shape of your dataset*/

%macro repeat1;

%do repeat=2000 %to 2010;

data clin_&amp;amp;repeat._phys_exam clin_&amp;amp;repeat._lab(drop=id) clin_&amp;amp;repeat._group(drop=form) clin_&amp;amp;repeat._final(drop=form);
	do form=1,2;
		do id=1,2;
			output clin_&amp;amp;repeat._phys_exam;
			if id=1 then output clin_&amp;amp;repeat._lab;
			if form=1 then output clin_&amp;amp;repeat._group;
			if form=1 then output clin_&amp;amp;repeat._final;
		end;
	end;
run;

%end;

%mend;

%repeat1;

/*The code that does what you need*/

%macro repeat2;

%do repeat=2000 %to 2010;

proc sort data=clin_&amp;amp;repeat._phys_exam;
	by form;
run;

proc sort data=clin_&amp;amp;repeat._lab;
	by form;
run;

data phys_lab_&amp;amp;repeat.;
	merge clin_&amp;amp;repeat._phys_exam clin_&amp;amp;repeat._lab;
	by form;
run;

proc sort data=phys_lab_&amp;amp;repeat.;
	by id;
run;

proc sort data=clin_&amp;amp;repeat._group;
	by id;
run;

proc sort data=clin_&amp;amp;repeat._final;
	by id;
run;

data phys_lab_&amp;amp;repeat.;
	merge phys_lab_&amp;amp;repeat. clin_&amp;amp;repeat._group clin_&amp;amp;repeat._final;
	by id;
run;

proc append base=year2000_year2010 data=phys_lab_&amp;amp;repeat.;
run;

%end;

%mend;

%repeat2;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Though this may not be the best solution, this may do something you mentioned.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Nov 2018 23:26:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-SAS-macro-to-merge-and-then-concatenate-multiple/m-p/513714#M138444</guid>
      <dc:creator>Junyong</dc:creator>
      <dc:date>2018-11-15T23:26:30Z</dc:date>
    </item>
    <item>
      <title>Re: How do I use a SAS macro to merge and then concatenate multiple datasets?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-SAS-macro-to-merge-and-then-concatenate-multiple/m-p/513936#M138543</link>
      <description>&lt;P&gt;Here's a quick tutorial on turning working code into a macro, which is what you want to do here:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Nov 2018 15:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-use-a-SAS-macro-to-merge-and-then-concatenate-multiple/m-p/513936#M138543</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-11-16T15:58:39Z</dc:date>
    </item>
  </channel>
</rss>

