<?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 data in proc sql in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550944#M153016</link>
    <description>&lt;P&gt;1. See &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;'s post for a row-by-row merge/join.&lt;/P&gt;
&lt;P&gt;2. Read about the fundamental differences between how a data step and a SQL query work.&lt;/P&gt;
&lt;P&gt;Many resources, like these 2:&lt;/P&gt;
&lt;DIV class="r"&gt;
&lt;DIV class="TbwUpd"&gt;&lt;CITE class="iUh30"&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/249-30.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/249-30.pdf&lt;/A&gt;&lt;/CITE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="s"&gt;
&lt;DIV class="r"&gt;
&lt;DIV class="TbwUpd"&gt;&lt;CITE class="iUh30"&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug06/hw/hw06.pdf" target="_blank" rel="noopener"&gt;https://www.lexjansen.com/nesug/nesug06/hw/hw06.pdf&lt;/A&gt;&lt;/CITE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="s"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="s"&gt;[Edited to point to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;'s post]&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 15 Apr 2019 03:10:45 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-04-15T03:10:45Z</dc:date>
    <item>
      <title>merging data in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550941#M153014</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have some problem in proc sql&amp;nbsp;when I merge two data sets.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;test1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;test2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="캡처1.PNG" style="width: 358px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/28690i72FB66E8DC0014D9/image-size/large?v=v2&amp;amp;px=999" role="button" title="캡처1.PNG" alt="캡처1.PNG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="캡처2.PNG" style="width: 341px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/28689iEF089ACA9E212ADD/image-size/large?v=v2&amp;amp;px=999" role="button" title="캡처2.PNG" alt="캡처2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;When I merge these two data sets, I want this data set&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;res (This result made by merge options in DATA)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="캡처3.PNG" style="width: 499px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/28691iE762E30C91101669/image-size/large?v=v2&amp;amp;px=999" role="button" title="캡처3.PNG" alt="캡처3.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But Using proc sql, It print wrong data set.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Wrong res (This result made by proc sql)&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="캡처4.PNG" style="width: 501px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/28692i0E8819D1C4B94F6F/image-size/large?v=v2&amp;amp;px=999" role="button" title="캡처4.PNG" alt="캡처4.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is my proc sql codes.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
input id$ id2 var1 var2;
cards;
A01 12 221 42
A02 15 947 79
A02 15 837 19
A03 21 217 12
A04 23 612 79
A04 23 182 25
A04 23 352 81
A05 30 797 42
;
run;

/* missing이 있는 data set */
data test2;
input id$ id2 var3$ var4;
cards;
A01 12 QP 96
A02 15 IM 81
A02 15 CB 81
A03 21 NR 71
A04 23 WV .
A04 23 DO .
A04 23 GV .
A05 30 MS .
;
run;

proc sql noprint;
	create table All1_2 as
		select *
	from test1, test2
	where test1.id=test2.id and test1.id2=test2.id2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Please help me!!!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 01:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550941#M153014</guid>
      <dc:creator>J_hoon</dc:creator>
      <dc:date>2019-04-15T01:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: merging data in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550944#M153016</link>
      <description>&lt;P&gt;1. See &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;'s post for a row-by-row merge/join.&lt;/P&gt;
&lt;P&gt;2. Read about the fundamental differences between how a data step and a SQL query work.&lt;/P&gt;
&lt;P&gt;Many resources, like these 2:&lt;/P&gt;
&lt;DIV class="r"&gt;
&lt;DIV class="TbwUpd"&gt;&lt;CITE class="iUh30"&gt;&lt;A href="https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/249-30.pdf" target="_blank" rel="noopener"&gt;https://support.sas.com/resources/papers/proceedings/proceedings/sugi30/249-30.pdf&lt;/A&gt;&lt;/CITE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="s"&gt;
&lt;DIV class="r"&gt;
&lt;DIV class="TbwUpd"&gt;&lt;CITE class="iUh30"&gt;&lt;A href="https://www.lexjansen.com/nesug/nesug06/hw/hw06.pdf" target="_blank" rel="noopener"&gt;https://www.lexjansen.com/nesug/nesug06/hw/hw06.pdf&lt;/A&gt;&lt;/CITE&gt;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="s"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;DIV class="s"&gt;[Edited to point to&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;'s post]&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 03:10:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550944#M153016</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-04-15T03:10:45Z</dc:date>
    </item>
    <item>
      <title>Re: merging data in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550946#M153018</link>
      <description>&lt;P&gt;What a SQL join does is not the same as a data step merge.SQL operations are performed without consideration for data record ordering. If you want to emulate a datastep merge operation in SQL you have to add an explicit ordering column to your data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
input id$ id2 var1 var2;
if id ne lag(id) or id2 ne lag(id2) then obs = 0;
obs + 1;
cards;
A01 12 221 42
A02 15 947 79
A02 15 837 19
A03 21 217 12
A04 23 612 79
A04 23 182 25
A04 23 352 81
A05 30 797 42
;

data test2;
input id$ id2 var3$ var4;
if id ne lag(id) or id2 ne lag(id2) then obs = 0;
obs + 1;
cards;
A01 12 QP 96
A02 15 IM 81
A02 15 CB 81
A03 21 NR 71
A04 23 WV .
A04 23 DO .
A04 23 GV .
A05 30 MS .
;

proc sql;
create table all1_2 as 
select * from test1 natural full join test2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But I can't see any good reason for trying to do that &lt;img id="smileyhappy" class="emoticon emoticon-smileyhappy" src="https://communities.sas.com/i/smilies/16x16_smiley-happy.png" alt="Smiley Happy" title="Smiley Happy" /&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT : Corrected code typo. Thank you &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt; .&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 03:43:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550946#M153018</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-04-15T03:43:11Z</dc:date>
    </item>
    <item>
      <title>Re: merging data in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550956#M153024</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/462"&gt;@PGStats&lt;/a&gt;&amp;nbsp;meant&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if id ne lag(id) or id2 ne lag(id2) then obs = 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;not&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if id ne lag(id) or id2 ne id2 then obs = 0;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 03:18:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550956#M153024</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-04-15T03:18:47Z</dc:date>
    </item>
    <item>
      <title>Re: merging data in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550975#M153029</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/265389"&gt;@J_hoon&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A merge with duplicate keys in both data sets is always problematic, and there is no correct solution. A data step merge joins observations as they occur in input, while a sql join gives a cartesian product.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In your example data seems to be unordered within a set of key values, and with no information present to maintain/restore a certain order, so your result is based on the assumption that data happens to be in a useful order to do perform a row-by row merge.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Consider the following example:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data a&lt;/STRONG&gt;&lt;BR /&gt;Strawberry red&lt;BR /&gt;Strawberry green&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;data b&lt;/STRONG&gt;&lt;BR /&gt;Strawberry unripe&lt;BR /&gt;Strawberry ripe&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;merge a b&lt;/STRONG&gt; &lt;BR /&gt;Strawberry red unripe&lt;BR /&gt;Strawberry green ripe&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;join a b&lt;/STRONG&gt;&lt;BR /&gt;Strawberry red unripe&lt;BR /&gt;Strawberry red ripe&lt;BR /&gt;Strawberry green unripe&lt;BR /&gt;Strawberry green ripe&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What do you prefer for further analysis, a merge with wrong observations connected or a join that gives you both right and wrong? - This is not a coding problem, but a design problem, and I would take it as a stop sign and reconsider the data model and the previous steps that led to this situation.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Apr 2019 09:08:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/550975#M153029</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-04-15T09:08:36Z</dc:date>
    </item>
    <item>
      <title>Re: merging data in proc sql</title>
      <link>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/585819#M167157</link>
      <description>Kindly share proc sql query for below mentioned query&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;data final;&lt;BR /&gt;length flag1 $100.;&lt;BR /&gt;merge ae (in=a) ex(in=b);&lt;BR /&gt;by subject site;&lt;BR /&gt;if a and not b ;&lt;BR /&gt;flag1 = "Subject is NOT present in Exposure form";&lt;BR /&gt;run;</description>
      <pubDate>Tue, 03 Sep 2019 13:39:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/merging-data-in-proc-sql/m-p/585819#M167157</guid>
      <dc:creator>pdhokriya</dc:creator>
      <dc:date>2019-09-03T13:39:26Z</dc:date>
    </item>
  </channel>
</rss>

