<?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: Merging the data sets using macro code in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411426#M279704</link>
    <description>&lt;P&gt;I could see doing it that way for learning purposes. In a way it shows a student how to iteratively build the code they need.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now the assignment is to convert it to a macro. I would highly suspect some of those intermediary steps are no longer necessary and possibly why your assignment received full marks - your prof only checked the final results wanted.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Im not sure what the question is here though?&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171577"&gt;@savanahb&lt;/a&gt;&amp;nbsp;what exactly do you need help with?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 08 Nov 2017 04:22:27 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2017-11-08T04:22:27Z</dc:date>
    <item>
      <title>Merging the data sets using macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411407#M279701</link>
      <description>&lt;P&gt;I am currently taking an intro to SAS class and was instructed to complete the same thing this code does using Macro statements.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc import out=datafile1&lt;BR /&gt;datafile= 'C:\Users\savanahb\Downloads\datafile1-2.xlsx'&lt;BR /&gt;DBMS= EXCEL2000 REPLACE;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc import out=datafile2&lt;BR /&gt;datafile= 'C:\Users\savanahb\Downloads\datafile2-2.xlsx'&lt;BR /&gt;DBMS= EXCEL2000 REPLACE;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc import out=datafile3&lt;BR /&gt;datafile= 'C:\Users\savanahb\Downloads\datafile3-2.xlsx'&lt;BR /&gt;DBMS= EXCEL2000 REPLACE;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data datafile123;&lt;BR /&gt;merge datafile1 datafile2 datafile3;&lt;BR /&gt;by id;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data datafile123;&lt;BR /&gt;merge datafile1 (in=a) datafile2;&lt;BR /&gt;by id;&lt;BR /&gt;if a;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data datafile123;&lt;BR /&gt;merge datafile2 (in=a) datafile3;&lt;BR /&gt;by ID;&lt;BR /&gt;if a;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data datafile123;&lt;BR /&gt;merge datafile1 datafile3 (in=a);&lt;BR /&gt;by ID;&lt;BR /&gt;if a;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;data datafile123;&lt;BR /&gt;merge datafile1 (in=a) datafile2 (in=b) datafile3 (in=c);&lt;BR /&gt;by ID;&lt;BR /&gt;if a and b and c;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;proc print data=datafile123;&lt;BR /&gt;title 'Tabulation of data from merged dataset Datafile123';&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Unfortunately, I am stuck trying to figure out how to only show the variables that have the same ID in all three datasets. Here is what I have come up with:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%let u=datafile1;&lt;BR /&gt;%let v=datafile1-3.xlsx;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc import out= &amp;amp;u&lt;BR /&gt;datafile= "C:\Users\savanahb\Downloads\&amp;amp;v"&lt;BR /&gt;dbms=xlsx replace;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%let w=datafile2;&lt;BR /&gt;%let x=datafile3-3.xlsx;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc import out= &amp;amp;w&lt;BR /&gt;datafile= "C:\Users\savanahb\Downloads\&amp;amp;x"&lt;BR /&gt;dbms=xlsx replace;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%let y=datafile3;&lt;BR /&gt;%let z=datafile3-3.xlsx;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;proc import out= &amp;amp;y&lt;BR /&gt;datafile= "C:\Users\savanahb\Downloads\&amp;amp;z"&lt;BR /&gt;dbms=xlsx replace;&lt;BR /&gt;getnames=yes;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;%macro combine(u,w,y);&lt;BR /&gt;data datafile123;&lt;BR /&gt;merge &amp;amp;u &amp;amp;w &amp;amp;y;&lt;BR /&gt;by id;&lt;/P&gt;&lt;P&gt;data datafile123&lt;BR /&gt;match merge &amp;amp;u &amp;amp;y;&lt;BR /&gt;by ID;&lt;/P&gt;&lt;P&gt;data datafile123&lt;BR /&gt;merge &amp;amp;w(in=a) &amp;amp;y;&lt;BR /&gt;by ID;&lt;BR /&gt;if a;&lt;/P&gt;&lt;P&gt;data datafile123&lt;BR /&gt;merge &amp;amp;u(in=a) &amp;amp;w(in=b) &amp;amp;y(in=c);&lt;BR /&gt;by ID;&lt;BR /&gt;if a and b and c;&lt;BR /&gt;run;&lt;BR /&gt;%mend combine;&lt;/P&gt;&lt;P&gt;proc print data=datafile123;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any suggestions?&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 02:20:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411407#M279701</guid>
      <dc:creator>savanahb</dc:creator>
      <dc:date>2017-11-08T02:20:09Z</dc:date>
    </item>
    <item>
      <title>Re: Merging the data sets using macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411414#M279702</link>
      <description>&lt;P&gt;Just a few guidelines to get you started ...&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First, check the assignment to make sure you are looking at the proper program.&amp;nbsp; It seems highly unusual that a program should create 5 different versions of datafile123, each one replacing the previous version.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Second, you did well to switch from single quotes to double quotes.&amp;nbsp; Macro variables would not get resolved within single quotes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Third, you need to know how to define a macro (beginning with a %macro statement and ending with a %mend statement).&amp;nbsp; Assigning values to a few macro variables is not what this assignment is about.&amp;nbsp; Inside your macro, you will end up with a loop along the lines of:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%do i=1 %to 3;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;/P&gt;
&lt;P&gt;%end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Each iteration through the loop will run one of the PROC IMPORTs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code after the three PROC IMPORTs may or may not need to be part of the macro.&amp;nbsp; As it stands now, there is no reason to use macro language.&amp;nbsp; But remember, I'm not convinced that the code presented is the proper code for this assignment.&amp;nbsp; That needs to be checked.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 02:47:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411414#M279702</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-11-08T02:47:32Z</dc:date>
    </item>
    <item>
      <title>Re: Merging the data sets using macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411419#M279703</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/4954"&gt;@Astounding&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;Thank you for you feedback. The code I initially posted was what I submitted for the last homework and received full credit. Basically, what the professor wanted us to do was to import the 3 excel files and then combine them to produce a table that only showed the variables with ID's that were found in all 3 files. The way he gave us directions was step by step combining each data file, hence the multiple datafile123. I think I understand what you are saying regarding the&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;%do i=1 %to 3;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;I was able to get the program to run using this code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro combine;&lt;BR /&gt;data datafile123;&lt;BR /&gt;merge&lt;BR /&gt;%do i = 1 %to 3;&lt;BR /&gt;datafile&amp;amp;i&lt;BR /&gt;%end;&lt;BR /&gt;;&lt;BR /&gt;by ID;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;%combine;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But it again did not merge the data so that it only showed the overlapping variables if that makes sense. That is why I had separated them out to different statements. When I tried to use a similar formula for the import, I kept getting stuck on&amp;nbsp;where the datafile was coming from.&lt;/P&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 03:35:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411419#M279703</guid>
      <dc:creator>savanahb</dc:creator>
      <dc:date>2017-11-08T03:35:08Z</dc:date>
    </item>
    <item>
      <title>Re: Merging the data sets using macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411426#M279704</link>
      <description>&lt;P&gt;I could see doing it that way for learning purposes. In a way it shows a student how to iteratively build the code they need.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now the assignment is to convert it to a macro. I would highly suspect some of those intermediary steps are no longer necessary and possibly why your assignment received full marks - your prof only checked the final results wanted.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Im not sure what the question is here though?&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171577"&gt;@savanahb&lt;/a&gt;&amp;nbsp;what exactly do you need help with?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 04:22:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411426#M279704</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-11-08T04:22:27Z</dc:date>
    </item>
    <item>
      <title>Re: Merging the data sets using macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411448#M279705</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171577"&gt;@savanahb&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;Many forum members don't post solutions for homework but just&amp;nbsp;provide direction. Given that you're demonstrating actual own work and effort I feel posting below (not tested) code won't just be the lazy path for you but will give you some direction and idea how you could approach this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro combine();

  %do i=1 %to 3;
    proc import out=datafile&amp;amp;i
      datafile= "C:\Users\savanahb\Downloads\datafile&amp;amp;i.-2.xlsx"
      DBMS= EXCEL2000 REPLACE;
      getnames=yes;
    run;

    proc sort data=datafile&amp;amp;i;
      by id;
    run;
  %end;

  data want;
    merge 
      datafile1 (in=in1)
      datafile2 (in=in2)
      datafile3 (in=in3)
      ;
    by id;
    if in1 and in2 and in3;
  run;

%mend;

%combine();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 08:26:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411448#M279705</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-11-08T08:26:45Z</dc:date>
    </item>
    <item>
      <title>Re: Merging the data sets using macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411533#M279706</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&lt;BR /&gt;Basically what I am looking for is guidance on how to get the same result but using macros instead and if I’m even on the right page here starting out.&lt;BR /&gt;and &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12447"&gt;@Patrick&lt;/a&gt;&lt;BR /&gt;Thank you! I totally get it and I want to actually learn this because I’ll be using it down the road in other classes. Normally what I am able to do is find various instructions online and in the book and the notes and extrapolate a way to do it, but I was having a lot of trouble doing this with macros. I am not a computer person at all so simplifying the codes is not my forte. I think I understand now from your post what I am missing. It seems to be a combination of sorts of the more recent code I posted and the original.</description>
      <pubDate>Wed, 08 Nov 2017 14:49:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411533#M279706</guid>
      <dc:creator>savanahb</dc:creator>
      <dc:date>2017-11-08T14:49:45Z</dc:date>
    </item>
    <item>
      <title>Re: Merging the data sets using macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411562#M279707</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171577"&gt;@savanahb&lt;/a&gt;&amp;nbsp;I would expect the code to be able to handle multiple data sets, so that IN condition would also need to be dynamic. At least that's my interpretation of the question.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 15:57:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411562#M279707</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-11-08T15:57:03Z</dc:date>
    </item>
    <item>
      <title>Re: Merging the data sets using macro code</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411663#M279708</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/171577"&gt;@savanahb&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;The posted code was for guidance. When using SAS macros it's always a judgment call how far you want/need to take things and what needs to be made dynamic - and the further you take it the harder the resulting code will be to "read".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&amp;nbsp;posted, you could also "macrotize" below bit - the list of table name in the merge statement and the IF in1... statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  data want;
    merge 
      datafile1 (in=in1)
      datafile2 (in=in2)
      datafile3 (in=in3)
      ;
    by id;
    if in1 and in2 and in3;
  run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could take it even further and implement an input parameters for the macro where you pass in root path and then&amp;nbsp;a list of source files and then have the macro generate SAS code which can deal with a varying number of source files.&lt;/P&gt;
&lt;P&gt;How far you need to take this depends on your assignment as well as how much time you can/want to spend on this.&lt;/P&gt;</description>
      <pubDate>Wed, 08 Nov 2017 19:57:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merging-the-data-sets-using-macro-code/m-p/411663#M279708</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2017-11-08T19:57:32Z</dc:date>
    </item>
  </channel>
</rss>

