<?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: Data merge in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534097#M146486</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Not sure why you need SQL when you have very elegant SET syntax. However, you'd need to use OUTER UNION CORRESPONDING with SQL:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sql_outer_union.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27016i6C02FCB77E354A60/image-size/large?v=v2&amp;amp;px=999" role="button" title="sql_outer_union.png" alt="sql_outer_union.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
    <pubDate>Fri, 08 Feb 2019 23:51:23 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2019-02-08T23:51:23Z</dc:date>
    <item>
      <title>Data merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534040#M146468</link>
      <description>&lt;P&gt;Hi Channel ,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set as&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data one ;&lt;/P&gt;&lt;P&gt;set two three;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to put this into proc SQL&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data set two has columns a,b,c,d,e&lt;/P&gt;&lt;P&gt;data set three has columns a,b,x,y,z,m&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks in advance&amp;nbsp;&lt;/P&gt;&lt;P&gt;kajal&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 19:26:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534040#M146468</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2019-02-08T19:26:07Z</dc:date>
    </item>
    <item>
      <title>Re: Data merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534060#M146473</link>
      <description>&lt;P&gt;If you just want to stack the datasets, I would say using the data step code you have above or proc append is going to be easier and faster. But if you want to use proc sql, you can use the INSERT and SELECT statements:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;INSERT INTO one&lt;/P&gt;
&lt;P&gt;SELECT * from two;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This requires all of the columns to be the same. You can also choose which columns from the second dataset to insert into their corresponding columns in the first dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;INSERT INTO one (a,b)&lt;/P&gt;
&lt;P&gt;SELECT a,b FROM two;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 20:24:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534060#M146473</guid>
      <dc:creator>ebowen</dc:creator>
      <dc:date>2019-02-08T20:24:52Z</dc:date>
    </item>
    <item>
      <title>Re: Data merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534063#M146474</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I'm not sure what you mean by your question. Your posting asks about a MERGE, but your code shows a SET. I don't understand what you mean by "put this into PROC SQL".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; For example, with some FAKE data, this is what your SET is doing.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="after_set_statement.png" style="width: 576px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27012iB2D25845033BEF31/image-size/large?v=v2&amp;amp;px=999" role="button" title="after_set_statement.png" alt="after_set_statement.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;in the above output, the yellow highlighted output is from data set TWO and the green highlights are from data set THREE. A SET statement will concatenate or "stack" data sets vertically. But a MERGE, like an SQL JOIN will bring the data sets together horizontally.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Can you clarify your question??&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 20:37:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534063#M146474</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2019-02-08T20:37:41Z</dc:date>
    </item>
    <item>
      <title>Re: Data merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534069#M146476</link>
      <description>&lt;P&gt;Thanks for reaching out in short&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I need exactly the same results using proc SQL as we are getting using set statement as shown by you&amp;nbsp;&lt;/P&gt;&lt;P&gt;hope this helps.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 21:17:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534069#M146476</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2019-02-08T21:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Data merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534093#M146483</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/259983"&gt;@kajal_30&lt;/a&gt;&amp;nbsp;you can do it with Proc SQL but it's a little more long-winded than&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13549"&gt;@Cynthia_sas&lt;/a&gt;&amp;nbsp;solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In order to append files in SQL you can use UNION or UNION ALL but the column names must be identical in both tables. In your case they aren't so we need to select missing values (dots) as the missing column names from the respective tables. Here is the code using&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13549"&gt;@Cynthia_sas&lt;/a&gt;&amp;nbsp;data sets&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data two;
	infile datalines;
	input a $ b c d e;
return;
datalines;
AA 1 2 3 4
BB 11 12 13 14
BB 15 16 17 18
;
run;

data three;
	infile datalines;
	input a $ b x y z m;
return;
datalines;
AA 21 22 23 24 25
AA 26 27 28 29 30
BB 31 32 33 34 35
BB 36 37 38 39 40
;
run;

proc sql;
	create table one as
	select a, b, c, d, e, . as x, . as y, . as z, . as m
	from two
	union all
	select a, b, . as c, . as d, . as e, x, y, z, m
	from three;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 08 Feb 2019 23:35:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534093#M146483</guid>
      <dc:creator>ChrisBrooks</dc:creator>
      <dc:date>2019-02-08T23:35:44Z</dc:date>
    </item>
    <item>
      <title>Re: Data merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534097#M146486</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;Not sure why you need SQL when you have very elegant SET syntax. However, you'd need to use OUTER UNION CORRESPONDING with SQL:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="sql_outer_union.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27016i6C02FCB77E354A60/image-size/large?v=v2&amp;amp;px=999" role="button" title="sql_outer_union.png" alt="sql_outer_union.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Fri, 08 Feb 2019 23:51:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534097#M146486</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2019-02-08T23:51:23Z</dc:date>
    </item>
    <item>
      <title>Re: Data merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534133#M146498</link>
      <description>&lt;P&gt;&amp;lt;pre&amp;gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select *&lt;BR /&gt;from sashelp.class&lt;BR /&gt;outer union corr&lt;BR /&gt;select *&lt;BR /&gt;from sashelp.heart;&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;lt;/pre&amp;gt;&lt;/P&gt;</description>
      <pubDate>Sat, 09 Feb 2019 14:06:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534133#M146498</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-02-09T14:06:13Z</dc:date>
    </item>
    <item>
      <title>Re: Data merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534226#M146557</link>
      <description>&lt;P&gt;Thanks a lot sir but got stuck in second merge could you please advice&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data merge (drop = w);&lt;/P&gt;&lt;P&gt;merge x (in = a rename = ( new = old))&amp;nbsp;&lt;/P&gt;&lt;P&gt;y (in= b) ;&lt;/P&gt;&lt;P&gt;by t ;&lt;/P&gt;&lt;P&gt;if a and b then d = "e" ;&lt;/P&gt;&lt;P&gt;if a and not b then d = "f" ;&lt;/P&gt;&lt;P&gt;if b and not a then d = "g" ;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;please convert this to sql&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Feb 2019 00:42:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534226#M146557</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2019-02-10T00:42:40Z</dc:date>
    </item>
    <item>
      <title>Re: Data merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534227#M146558</link>
      <description>thanks a lot for ur help could you also please advice of the second issue as follows i need to convert this to sql as well&lt;BR /&gt;&lt;BR /&gt;data merge (drop = w);&lt;BR /&gt;&lt;BR /&gt;merge x (in = a rename = ( new = old))&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;y (in= b) ;&lt;BR /&gt;&lt;BR /&gt;by t ;&lt;BR /&gt;&lt;BR /&gt;if a and b then d = "e" ;&lt;BR /&gt;&lt;BR /&gt;if a and not b then d = "f" ;&lt;BR /&gt;&lt;BR /&gt;if b and not a then d = "g" ;&lt;BR /&gt;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Sun, 10 Feb 2019 00:42:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534227#M146558</guid>
      <dc:creator>kajal_30</dc:creator>
      <dc:date>2019-02-10T00:42:00Z</dc:date>
    </item>
    <item>
      <title>Re: Data merge</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534281#M146600</link>
      <description>&lt;P&gt;You should use JOIN .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;create table want as&lt;/P&gt;
&lt;P&gt;select *,case when not missing(y.t) and not missing(x.t) then 'e'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;when missing(y.t) then 'f'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;when missing(x.t) then 'g'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;else ' '&lt;/P&gt;
&lt;P&gt;end as flag&lt;/P&gt;
&lt;P&gt;&amp;nbsp;from x(rename = ( new = old)) full join y&lt;/P&gt;
&lt;P&gt;&amp;nbsp; on x.t=y.t&lt;/P&gt;
&lt;P&gt;;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Feb 2019 11:29:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Data-merge/m-p/534281#M146600</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-02-10T11:29:41Z</dc:date>
    </item>
  </channel>
</rss>

