<?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: Unable to do a horizontal few-to-many merge in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Unable-to-do-a-horizontal-few-to-many-merge/m-p/837554#M331174</link>
    <description>&lt;P&gt;Looks it did what you asked.&amp;nbsp; But since none of the values of BUYERTYPE matched you did not get what you wanted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that is the code you used the easiest thing to fix is make the values in the DISCOUNT dataset also be in uppercase so that your merge will actual finds some values that match.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could just fix the in-line data to use uppercase letters.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data discount;
  input buyertype :$10. discount;
datalines;
CONSUMER 0
REPAIR .33
STORE .4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also just read the values using the $UPCASE informat.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data discount;
  input buyertype :$upcase10. discount;
datalines;
consumer 0
repair .33
store .4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you could change the values you already have by using the UPCASE() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data discount;
  input buyertype :$10. discount;
  buyertype = upcase(buyertype);
datalines;
consumer 0
repair .33
store .4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that what you are doing is called a one-to-many merge. You want to match one observation from one dataset to many observations from another.&amp;nbsp; And that is what your example code is doing.&amp;nbsp; This only one observation per BUYERTYPE in the DISCOUNT dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The term "few-to-many merge" seems to imply that you want to merge more than one observations from both sources.&amp;nbsp; That is called a many-to-many merge. You cannot (at least without a lot of extra coding) do a many-to-merge with a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are those terms&amp;nbsp;"horizontal merge"&amp;nbsp; and "vertical merge" what is used in that book?&lt;/P&gt;
&lt;P&gt;I assume by "horizontal merge" they just mean a &lt;STRONG&gt;merge&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;I assume that by "vertical merge" they mean &lt;STRONG&gt;concatenating&lt;/STRONG&gt; the data using a SET statement with multiple dataset.&amp;nbsp; That is not really merging the data in any sense.&amp;nbsp; But perhaps they mean&amp;nbsp;&lt;STRONG&gt;interleaving&lt;/STRONG&gt; the data by using a SET statement with a BY statement.&amp;nbsp; That at least has the potential to intermix the observations from the source dataset instead of just appending them together so perhaps you could consider it a merge of some sort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 09 Oct 2022 07:06:15 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2022-10-09T07:06:15Z</dc:date>
    <item>
      <title>Unable to do a horizontal few-to-many merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unable-to-do-a-horizontal-few-to-many-merge/m-p/837547#M331171</link>
      <description>&lt;P&gt;Hi I am following a textbook (Elliot, SAS Essentials pg 99) example on a Few to Many merge using MERGE statement. Screenshot of the textbook example is below.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 590px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76032i58A602D312AEC5C0/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I opened the SAS file and completed the code as shown in the example&lt;/P&gt;&lt;PRE&gt;data sales;
format item $20. buyertype $8.;
input item buyertype price;
datalines;
CARBCLEANER REPAIR 2.30
BELT CONSUMER 6.99
MOTOROIL CONSUMER 14.34
CHAIN STORE 18.99
SPARKPLUGS REPAIR 28.99
CLEANER CONSUMER 1.99
WRENCH STORE 18.88
;
run;
proc print data = sales; run;


data discount;
format buyertype $10.;
input buyertype discount;
datalines;
consumer 0
repair .33
store .4
;
run; 
proc print data=discount; run;


proc sort data=sales; by buyertype; run;
proc sort data=discount; by buyertype; run;


data report;
	Merge sales discount; by buyertype;
	final = round(price*(1-discount),.01);
	run;
	
proc print data=report;
sum final;
run;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I must have done something wrong because instead of getting a horizontal merge as shown in Table 4.14 above, instead I got a vertical merger like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 477px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/76033iB2E547038C72851F/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;what did I do wrong in my code that cause SAS to produce a vertical merge? In the textbook to get vertical merge, they used the SET statement and MERGE statement is for horizontal merge.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be greatly appreciated.&lt;/P&gt;</description>
      <pubDate>Sun, 09 Oct 2022 03:01:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unable-to-do-a-horizontal-few-to-many-merge/m-p/837547#M331171</guid>
      <dc:creator>Nietzsche</dc:creator>
      <dc:date>2022-10-09T03:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to do a horizontal few-to-many merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unable-to-do-a-horizontal-few-to-many-merge/m-p/837548#M331172</link>
      <description>&lt;P&gt;The strings must be an&amp;nbsp;&lt;EM&gt;exact&lt;/EM&gt; match for the BY to work. "REPAIR" is not equal to "repair".&lt;/P&gt;
&lt;P&gt;Use the UPCASE function to convert the lowercase values to uppercase.&lt;/P&gt;</description>
      <pubDate>Sun, 09 Oct 2022 03:07:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unable-to-do-a-horizontal-few-to-many-merge/m-p/837548#M331172</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-10-09T03:07:36Z</dc:date>
    </item>
    <item>
      <title>Re: Unable to do a horizontal few-to-many merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Unable-to-do-a-horizontal-few-to-many-merge/m-p/837554#M331174</link>
      <description>&lt;P&gt;Looks it did what you asked.&amp;nbsp; But since none of the values of BUYERTYPE matched you did not get what you wanted.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If that is the code you used the easiest thing to fix is make the values in the DISCOUNT dataset also be in uppercase so that your merge will actual finds some values that match.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could just fix the in-line data to use uppercase letters.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data discount;
  input buyertype :$10. discount;
datalines;
CONSUMER 0
REPAIR .33
STORE .4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also just read the values using the $UPCASE informat.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data discount;
  input buyertype :$upcase10. discount;
datalines;
consumer 0
repair .33
store .4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you could change the values you already have by using the UPCASE() function.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data discount;
  input buyertype :$10. discount;
  buyertype = upcase(buyertype);
datalines;
consumer 0
repair .33
store .4
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that what you are doing is called a one-to-many merge. You want to match one observation from one dataset to many observations from another.&amp;nbsp; And that is what your example code is doing.&amp;nbsp; This only one observation per BUYERTYPE in the DISCOUNT dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The term "few-to-many merge" seems to imply that you want to merge more than one observations from both sources.&amp;nbsp; That is called a many-to-many merge. You cannot (at least without a lot of extra coding) do a many-to-merge with a data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are those terms&amp;nbsp;"horizontal merge"&amp;nbsp; and "vertical merge" what is used in that book?&lt;/P&gt;
&lt;P&gt;I assume by "horizontal merge" they just mean a &lt;STRONG&gt;merge&lt;/STRONG&gt;.&lt;/P&gt;
&lt;P&gt;I assume that by "vertical merge" they mean &lt;STRONG&gt;concatenating&lt;/STRONG&gt; the data using a SET statement with multiple dataset.&amp;nbsp; That is not really merging the data in any sense.&amp;nbsp; But perhaps they mean&amp;nbsp;&lt;STRONG&gt;interleaving&lt;/STRONG&gt; the data by using a SET statement with a BY statement.&amp;nbsp; That at least has the potential to intermix the observations from the source dataset instead of just appending them together so perhaps you could consider it a merge of some sort.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 09 Oct 2022 07:06:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Unable-to-do-a-horizontal-few-to-many-merge/m-p/837554#M331174</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-10-09T07:06:15Z</dc:date>
    </item>
  </channel>
</rss>

