<?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 How to merge tables, returning only the rows that matched in the by variable? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880162#M347768</link>
    <description>&lt;P&gt;Hi everybody!&lt;/P&gt;&lt;P&gt;Suppose I have three tables named TABLE1, CODES1 and CODES:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;----------------------------------------------------&lt;/P&gt;&lt;P&gt;TABLE1&lt;/P&gt;&lt;P&gt;name, category_broad, category_exact&lt;BR /&gt;BANANA, 1 (foods), 101 (fruits)&lt;BR /&gt;APPLE, 1 (foods), 101 (fruits)&lt;BR /&gt;CAR, 2 (things), 201 (cars)&lt;BR /&gt;HOUSE, 2 (things), 202 (houses)&lt;BR /&gt;BALL, 2 (things), 203 (balls)&lt;BR /&gt;BREAD, 1 (foods), 102 (breads)&lt;/P&gt;&lt;P&gt;----------------------------&lt;/P&gt;&lt;P&gt;CODES1&lt;/P&gt;&lt;P&gt;category_broad, code&lt;BR /&gt;1 (foods), 34&lt;BR /&gt;2 (things), 35&lt;/P&gt;&lt;P&gt;----------------------------&lt;/P&gt;&lt;P&gt;CODES2&lt;/P&gt;&lt;P&gt;category_exact, code&lt;BR /&gt;101 (fruits), 463&lt;BR /&gt;102 (breads), 464&lt;BR /&gt;202 (houses), 564&lt;BR /&gt;203 (balls), 565&lt;/P&gt;&lt;P&gt;----------------------------------------------------&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I would like to achieve is two merges:&lt;BR /&gt;-first merge where if the category_broad from codes1 matches with category_broad from table1, then the code is added to the row that matched, and only matched rows are included.&lt;BR /&gt;-second merge where if the category_exact from codes2 matches with category_exact from table1, then the code is added to the row that matched, and only matched rows are included. So for example the row with "car" would be left out from the merge with codes2 table.&lt;BR /&gt;&lt;BR /&gt;So the desired resulting tables would be:&lt;BR /&gt;&lt;BR /&gt;TABLE1xCODES1&lt;/P&gt;&lt;P&gt;name, category_broad, category_exact, code&lt;BR /&gt;BANANA, 1 (foods), 101 (fruits), 34&lt;BR /&gt;APPLE, 1 (foods), 101 (fruits), 34&lt;BR /&gt;CAR, 2 (things), 201 (cars), 35&lt;BR /&gt;HOUSE, 2 (things), 202 (houses), 35&lt;BR /&gt;BALL, 2 (things), 203 (balls), 35&lt;BR /&gt;BREAD, 1 (foods), 102 (breads), 34&lt;BR /&gt;&lt;BR /&gt;TABLE1xCODES2&lt;/P&gt;&lt;P&gt;name, category_broad, category_exact, code&lt;BR /&gt;BANANA, 1 (foods), 101 (fruits), 463&lt;BR /&gt;APPLE, 1 (foods), 101 (fruits), 463&lt;BR /&gt;HOUSE, 2 (things), 202 (houses), 564&lt;BR /&gt;BALL, 2 (things), 203 (balls), 565&lt;BR /&gt;BREAD, 1 (foods), 102 (breads), 464&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I know this should not be too difficult but I am having trouble in achieving this.&lt;BR /&gt;Regarding the second merge, I have so far tried using this:&lt;BR /&gt;&lt;BR /&gt;-------------------------------------&lt;BR /&gt;proc sort data=table1;&lt;/P&gt;&lt;P&gt;by category_exact;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=codes2;&lt;/P&gt;&lt;P&gt;by category_exact;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data merged;&lt;/P&gt;&lt;P&gt;merge table1 codes2;&lt;/P&gt;&lt;P&gt;by category_exact;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;-------------------------------------------&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;The problem with this approach is that it brings to the resulting table also the row that did not match, which is the CAR row. And in that row, the value for column "code" is missing. This is something I don't understand regarding the 'merge by' option in SAS, since the by-variable did not match there. What is the correct way to solve this? Thank you so much in advance! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
    <pubDate>Mon, 12 Jun 2023 11:12:48 GMT</pubDate>
    <dc:creator>noobnbrnoobest</dc:creator>
    <dc:date>2023-06-12T11:12:48Z</dc:date>
    <item>
      <title>How to merge tables, returning only the rows that matched in the by variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880162#M347768</link>
      <description>&lt;P&gt;Hi everybody!&lt;/P&gt;&lt;P&gt;Suppose I have three tables named TABLE1, CODES1 and CODES:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;----------------------------------------------------&lt;/P&gt;&lt;P&gt;TABLE1&lt;/P&gt;&lt;P&gt;name, category_broad, category_exact&lt;BR /&gt;BANANA, 1 (foods), 101 (fruits)&lt;BR /&gt;APPLE, 1 (foods), 101 (fruits)&lt;BR /&gt;CAR, 2 (things), 201 (cars)&lt;BR /&gt;HOUSE, 2 (things), 202 (houses)&lt;BR /&gt;BALL, 2 (things), 203 (balls)&lt;BR /&gt;BREAD, 1 (foods), 102 (breads)&lt;/P&gt;&lt;P&gt;----------------------------&lt;/P&gt;&lt;P&gt;CODES1&lt;/P&gt;&lt;P&gt;category_broad, code&lt;BR /&gt;1 (foods), 34&lt;BR /&gt;2 (things), 35&lt;/P&gt;&lt;P&gt;----------------------------&lt;/P&gt;&lt;P&gt;CODES2&lt;/P&gt;&lt;P&gt;category_exact, code&lt;BR /&gt;101 (fruits), 463&lt;BR /&gt;102 (breads), 464&lt;BR /&gt;202 (houses), 564&lt;BR /&gt;203 (balls), 565&lt;/P&gt;&lt;P&gt;----------------------------------------------------&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I would like to achieve is two merges:&lt;BR /&gt;-first merge where if the category_broad from codes1 matches with category_broad from table1, then the code is added to the row that matched, and only matched rows are included.&lt;BR /&gt;-second merge where if the category_exact from codes2 matches with category_exact from table1, then the code is added to the row that matched, and only matched rows are included. So for example the row with "car" would be left out from the merge with codes2 table.&lt;BR /&gt;&lt;BR /&gt;So the desired resulting tables would be:&lt;BR /&gt;&lt;BR /&gt;TABLE1xCODES1&lt;/P&gt;&lt;P&gt;name, category_broad, category_exact, code&lt;BR /&gt;BANANA, 1 (foods), 101 (fruits), 34&lt;BR /&gt;APPLE, 1 (foods), 101 (fruits), 34&lt;BR /&gt;CAR, 2 (things), 201 (cars), 35&lt;BR /&gt;HOUSE, 2 (things), 202 (houses), 35&lt;BR /&gt;BALL, 2 (things), 203 (balls), 35&lt;BR /&gt;BREAD, 1 (foods), 102 (breads), 34&lt;BR /&gt;&lt;BR /&gt;TABLE1xCODES2&lt;/P&gt;&lt;P&gt;name, category_broad, category_exact, code&lt;BR /&gt;BANANA, 1 (foods), 101 (fruits), 463&lt;BR /&gt;APPLE, 1 (foods), 101 (fruits), 463&lt;BR /&gt;HOUSE, 2 (things), 202 (houses), 564&lt;BR /&gt;BALL, 2 (things), 203 (balls), 565&lt;BR /&gt;BREAD, 1 (foods), 102 (breads), 464&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I know this should not be too difficult but I am having trouble in achieving this.&lt;BR /&gt;Regarding the second merge, I have so far tried using this:&lt;BR /&gt;&lt;BR /&gt;-------------------------------------&lt;BR /&gt;proc sort data=table1;&lt;/P&gt;&lt;P&gt;by category_exact;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sort data=codes2;&lt;/P&gt;&lt;P&gt;by category_exact;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data merged;&lt;/P&gt;&lt;P&gt;merge table1 codes2;&lt;/P&gt;&lt;P&gt;by category_exact;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;-------------------------------------------&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;The problem with this approach is that it brings to the resulting table also the row that did not match, which is the CAR row. And in that row, the value for column "code" is missing. This is something I don't understand regarding the 'merge by' option in SAS, since the by-variable did not match there. What is the correct way to solve this? Thank you so much in advance! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 11:12:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880162#M347768</guid>
      <dc:creator>noobnbrnoobest</dc:creator>
      <dc:date>2023-06-12T11:12:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge tables, returning only the rows that matched in the by variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880168#M347772</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/442814"&gt;@noobnbrnoobest&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi everybody!&lt;/P&gt;
&lt;P&gt;Suppose I have three tables named TABLE1, CODES1 and CODES:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;----------------------------------------------------&lt;/P&gt;
&lt;P&gt;TABLE1&lt;/P&gt;
&lt;P&gt;name, category_broad, category_exact&lt;BR /&gt;BANANA, 1 (foods), 101 (fruits)&lt;BR /&gt;APPLE, 1 (foods), 101 (fruits)&lt;BR /&gt;CAR, 2 (things), 201 (cars)&lt;BR /&gt;HOUSE, 2 (things), 202 (houses)&lt;BR /&gt;BALL, 2 (things), 203 (balls)&lt;BR /&gt;BREAD, 1 (foods), 102 (breads)&lt;/P&gt;
&lt;P&gt;----------------------------&lt;/P&gt;
&lt;P&gt;CODES1&lt;/P&gt;
&lt;P&gt;category_broad, code&lt;BR /&gt;1 (foods), 34&lt;BR /&gt;2 (things), 35&lt;/P&gt;
&lt;P&gt;----------------------------&lt;/P&gt;
&lt;P&gt;CODES2&lt;/P&gt;
&lt;P&gt;category_exact, code&lt;BR /&gt;101 (fruits), 463&lt;BR /&gt;102 (breads), 464&lt;BR /&gt;202 (houses), 564&lt;BR /&gt;203 (balls), 565&lt;/P&gt;
&lt;P&gt;----------------------------------------------------&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What I would like to achieve is two merges:&lt;BR /&gt;-first merge where if the category_broad from codes1 matches with category_broad from table1, then the code is added to the row that matched, and only matched rows are included.&lt;BR /&gt;-second merge where if the category_exact from codes2 matches with category_exact from table1, then the code is added to the row that matched, and only matched rows are included. So for example the row with "car" would be left out from the merge with codes2 table.&lt;BR /&gt;&lt;BR /&gt;So the desired resulting tables would be:&lt;BR /&gt;&lt;BR /&gt;TABLE1xCODES1&lt;/P&gt;
&lt;P&gt;name, category_broad, category_exact, code&lt;BR /&gt;BANANA, 1 (foods), 101 (fruits), 34&lt;BR /&gt;APPLE, 1 (foods), 101 (fruits), 34&lt;BR /&gt;CAR, 2 (things), 201 (cars), 35&lt;BR /&gt;HOUSE, 2 (things), 202 (houses), 35&lt;BR /&gt;BALL, 2 (things), 203 (balls), 35&lt;BR /&gt;BREAD, 1 (foods), 102 (breads), 34&lt;BR /&gt;&lt;BR /&gt;TABLE1xCODES2&lt;/P&gt;
&lt;P&gt;name, category_broad, category_exact, code&lt;BR /&gt;BANANA, 1 (foods), 101 (fruits), 463&lt;BR /&gt;APPLE, 1 (foods), 101 (fruits), 463&lt;BR /&gt;HOUSE, 2 (things), 202 (houses), 564&lt;BR /&gt;BALL, 2 (things), 203 (balls), 565&lt;BR /&gt;BREAD, 1 (foods), 102 (breads), 464&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;I know this should not be too difficult but I am having trouble in achieving this.&lt;BR /&gt;Regarding the second merge, I have so far tried using this:&lt;BR /&gt;&lt;BR /&gt;-------------------------------------&lt;BR /&gt;proc sort data=table1;&lt;/P&gt;
&lt;P&gt;by category_exact;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sort data=codes2;&lt;/P&gt;
&lt;P&gt;by category_exact;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data merged;&lt;/P&gt;
&lt;P&gt;merge table1 codes2;&lt;/P&gt;
&lt;P&gt;by category_exact;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;-------------------------------------------&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;The problem with this approach is that it brings to the resulting table also the row that did not match, which is the CAR row. And in that row, the value for column "code" is missing. This is something I don't understand regarding the 'merge by' option in SAS, since the by-variable did not match there. What is the correct way to solve this? Thank you so much in advance! &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data merged;
    merge table1(in=in1) codes2(in=in2);
    by category_exact;
    if in1 and in2;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Jun 2023 11:27:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880168#M347772</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-12T11:27:47Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge tables, returning only the rows that matched in the by variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880170#M347774</link>
      <description>&lt;P&gt;Well this was quickly resolved with this great documentation:&lt;BR /&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings18/1746-2018.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings18/1746-2018.pdf&lt;/A&gt;&lt;BR /&gt;(at "&lt;SPAN class=""&gt;&lt;SPAN&gt;EXAMPLE 3:&lt;/SPAN&gt; &lt;SPAN&gt;UNMATCHED BY VALUE&lt;/SPAN&gt;&lt;/SPAN&gt;")&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Turned out there's a way to specify that the other table will have all the distinct possible values for the by-variable:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data table1xcodes2;&lt;BR /&gt;merge codes2 (in=isfound) table1;&lt;BR /&gt;by category_exact;&lt;BR /&gt;if isfound;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;EDIT: What finally helped me understand this code was the explanation, that the "(in=variable)" -option creates a temporary variable, that gets values 0 and 1 for all rows. Then when it is called in the if-statement it does the magic.&lt;BR /&gt;"A&lt;SPAN class=""&gt;&lt;SPAN&gt;ny records with a value of [the by-variable "category_exact"]&lt;/SPAN&gt;&lt;SPAN&gt; that did not appear in the [table1] dataset&lt;/SPAN&gt; &lt;SPAN&gt;will have&lt;/SPAN&gt; &lt;SPAN&gt;a value of 0 for [isfound]&lt;/SPAN&gt;&lt;/SPAN&gt;"&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 12:43:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880170#M347774</guid>
      <dc:creator>noobnbrnoobest</dc:creator>
      <dc:date>2023-06-12T12:43:49Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge tables, returning only the rows that matched in the by variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880172#M347775</link>
      <description>Thank you, this looks like the way to solve this once and for all!</description>
      <pubDate>Mon, 12 Jun 2023 11:31:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880172#M347775</guid>
      <dc:creator>noobnbrnoobest</dc:creator>
      <dc:date>2023-06-12T11:31:24Z</dc:date>
    </item>
    <item>
      <title>Re: How to merge tables, returning only the rows that matched in the by variable?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880185#M347781</link>
      <description>&lt;P&gt;There are many data set options that can be used in the SET and MERGE statements which may come in handy at some time in the future. You might want to bookmark this list of data set options&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://documentation.sas.com/doc/en/pgmmvacdc/9.4/ledsoptsref/p1pczmnhbq4axpn1l15s9mk6mobp.htm" target="_blank"&gt;https://documentation.sas.com/doc/en/pgmmvacdc/9.4/ledsoptsref/p1pczmnhbq4axpn1l15s9mk6mobp.htm&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Jun 2023 12:03:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-merge-tables-returning-only-the-rows-that-matched-in-the/m-p/880185#M347781</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-06-12T12:03:28Z</dc:date>
    </item>
  </channel>
</rss>

