<?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 Problem in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22150#M3603</link>
    <description>Ah, now I see the problem.  The merge did what you expected for the first three rows.  The reason it didn't merge for the second instance of 836 (row 5 of table A) is because there is only one record in B for A2=836 and that was used when merging with row 2 of table A.  DATA step merges don't merge one record in B with all matching records in A.  If there is one record in B and 5 matching records in A then only the first record will be 'merged' the other four records will only have table A data.&lt;BR /&gt;
&lt;BR /&gt;
I like to use PROC SQL in these cases (and because you don't have to sort first).  It merges differently than the DATA step.  Here's an example.  In the coalesce command, you have to list the column from Table B first because you want the result from Table B to take priority.&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	create table C as&lt;BR /&gt;
	select A.A1, A.A2, coalesce(B.A3, A.A3) as A3&lt;BR /&gt;
	from A left join B&lt;BR /&gt;
	on A.A2 = B.A2&lt;BR /&gt;
	order by A.A1, A.A2;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
If you want to stick with the DATA step, try renaming the A3 variable in Table B to a new name.  Then, when you merge, the B values are "retained" in the new variable.  You can use an IF statement to assign these values back to A3.  Here's an example:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data C;&lt;BR /&gt;
	merge A (in=A) B (in=B rename=(A3=A4));&lt;BR /&gt;
	by A2;&lt;BR /&gt;
	if A;&lt;BR /&gt;
	if B then A3=A4;&lt;BR /&gt;
	drop A4;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
    <pubDate>Fri, 30 May 2008 13:37:30 GMT</pubDate>
    <dc:creator>1162</dc:creator>
    <dc:date>2008-05-30T13:37:30Z</dc:date>
    <item>
      <title>Merge Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22148#M3601</link>
      <description>Hi friends,&lt;BR /&gt;
&lt;BR /&gt;
I am in the process of mergeing 2 datasets as follows:&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;Dataset A:&lt;/B&gt;&lt;BR /&gt;
A1-------A2-------A3&lt;BR /&gt;
211-----211------ABC&lt;BR /&gt;
211-----836------ABC&lt;BR /&gt;
211-----837------ABC&lt;BR /&gt;
212-----212------ABC&lt;BR /&gt;
212-----836------ABC&lt;BR /&gt;
212-----837------ABC&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;Dataset B:&lt;/B&gt;&lt;BR /&gt;
A2------A3&lt;BR /&gt;
836-----CDE&lt;BR /&gt;
837-----FGH&lt;BR /&gt;
&lt;BR /&gt;
My intended output datasest C after merge should look like:&lt;BR /&gt;
&lt;B&gt;Dataset C:&lt;/B&gt;&lt;BR /&gt;
A1-------A2-------A3&lt;BR /&gt;
211-----211------ABC&lt;BR /&gt;
211-----836------CDE&lt;BR /&gt;
211-----837------FGH&lt;BR /&gt;
212-----212------ABC&lt;BR /&gt;
212-----836------CDE&lt;BR /&gt;
212-----837------FGH&lt;BR /&gt;
&lt;BR /&gt;
The code I am using is quite simple and is as follows:&lt;BR /&gt;
&lt;BR /&gt;
&lt;I&gt;&lt;BR /&gt;
Proc Sort Data=A;By A2;Run;&lt;BR /&gt;
Proc Sort Data=B;By A2;Run;&lt;BR /&gt;
Data C;Merge A(In=A) B(In=B);By A2;If A;Run;&lt;BR /&gt;
&lt;/I&gt;&lt;BR /&gt;
&lt;BR /&gt;
The output that I am getting is like this:&lt;BR /&gt;
&lt;B&gt;Dataset C:&lt;/B&gt;&lt;BR /&gt;
A1-------A2-------A3&lt;BR /&gt;
211-----211------ABC&lt;BR /&gt;
211-----836------CDE&lt;BR /&gt;
211-----837------FGH&lt;BR /&gt;
212-----212------ABC&lt;BR /&gt;
212-----836------ABC&lt;BR /&gt;
212-----837------ABC&lt;BR /&gt;
&lt;BR /&gt;
I was counting on the fact that in case of a merge, the data on the first(left) dataset is supposed to be overlayed by the data on the second(right) dataset.&lt;BR /&gt;
That is what is happenning in the first set(A1=211), but for the duplicates(A1=212), that is not happenning. &lt;BR /&gt;
&lt;BR /&gt;
In the example that I have put, I have only shown 1 variable A3, but in actuality there are 21 variables, so renaming and substituting conditionally (i.e. &lt;I&gt;If B Then A.A3 = B.B3;&lt;/I&gt;) would be tedious to code. I was looking for some way of doing the same directly through the merge statement.&lt;BR /&gt;
&lt;BR /&gt;
Please post your views/suggestions.&lt;BR /&gt;
&lt;BR /&gt;
Thanks in advance.</description>
      <pubDate>Fri, 30 May 2008 06:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22148#M3601</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-05-30T06:36:13Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22149#M3602</link>
      <description>I would have agreed with you about which table is used by default, especially since it seemed to work for the first three.  Maybe you have to be more explicit in telling SAS what to do. Here's a possible solution:&lt;BR /&gt;
&lt;BR /&gt;
Proc Sort Data=A;By A2;Run;&lt;BR /&gt;
Proc Sort Data=B;By A2;Run;&lt;BR /&gt;
Data C;Merge A(In=A) B(In=B rename=(A3=A4));By A2;If A;If B then A3=A4;Drop A4;Run;</description>
      <pubDate>Fri, 30 May 2008 13:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22149#M3602</guid>
      <dc:creator>1162</dc:creator>
      <dc:date>2008-05-30T13:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22150#M3603</link>
      <description>Ah, now I see the problem.  The merge did what you expected for the first three rows.  The reason it didn't merge for the second instance of 836 (row 5 of table A) is because there is only one record in B for A2=836 and that was used when merging with row 2 of table A.  DATA step merges don't merge one record in B with all matching records in A.  If there is one record in B and 5 matching records in A then only the first record will be 'merged' the other four records will only have table A data.&lt;BR /&gt;
&lt;BR /&gt;
I like to use PROC SQL in these cases (and because you don't have to sort first).  It merges differently than the DATA step.  Here's an example.  In the coalesce command, you have to list the column from Table B first because you want the result from Table B to take priority.&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql;&lt;BR /&gt;
	create table C as&lt;BR /&gt;
	select A.A1, A.A2, coalesce(B.A3, A.A3) as A3&lt;BR /&gt;
	from A left join B&lt;BR /&gt;
	on A.A2 = B.A2&lt;BR /&gt;
	order by A.A1, A.A2;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
If you want to stick with the DATA step, try renaming the A3 variable in Table B to a new name.  Then, when you merge, the B values are "retained" in the new variable.  You can use an IF statement to assign these values back to A3.  Here's an example:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
data C;&lt;BR /&gt;
	merge A (in=A) B (in=B rename=(A3=A4));&lt;BR /&gt;
	by A2;&lt;BR /&gt;
	if A;&lt;BR /&gt;
	if B then A3=A4;&lt;BR /&gt;
	drop A4;&lt;BR /&gt;
run;&lt;BR /&gt;
[/pre]</description>
      <pubDate>Fri, 30 May 2008 13:37:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22150#M3603</guid>
      <dc:creator>1162</dc:creator>
      <dc:date>2008-05-30T13:37:30Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22151#M3604</link>
      <description>Thanks a lot for the help. I think I will use the Proc SQL route.&lt;BR /&gt;
&lt;BR /&gt;
If I have same number of records in dataset B for say A2=836 as in  dataset A (i.e. 2) then will the merge as in my first post work?</description>
      <pubDate>Sat, 31 May 2008 16:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22151#M3604</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-05-31T16:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Merge Problem</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22152#M3605</link>
      <description>Well, I'd have to run some tests to be sure, but I think it will work only if you have the same number of each level in B.  In other words, you'd have to have the same numbers of '211's, '836's, etc. for column A2.  Another possibility would be to merge by both A1 and A2, but by this point you almost have your final dataset anyway.  I wouldn't take this approach unless the original dataset started out in this form.</description>
      <pubDate>Mon, 02 Jun 2008 14:36:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Merge-Problem/m-p/22152#M3605</guid>
      <dc:creator>1162</dc:creator>
      <dc:date>2008-06-02T14:36:13Z</dc:date>
    </item>
  </channel>
</rss>

