<?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: Stacking two datasets with overlapping IDs in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245507#M45824</link>
    <description>&lt;P&gt;A SQL solution that's probably not more efficient &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; and after borrowing&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__﻿&lt;/a&gt;'s fake data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
   do id = 1,3,4,6,7;
      output;
      end;
   retain x 'a';
   run;
data b;
   do id=1,2,3,5,6,7;
      output;
      end;
   retain x 'b';
   run;

proc sql;
create table want as
select * from(
select * from a
union
select * from b
where id not in (select distinct Id from a)) a
order by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 22 Jan 2016 17:33:22 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-01-22T17:33:22Z</dc:date>
    <item>
      <title>Stacking two datasets with overlapping IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245499#M45819</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have two datasets which I would like to stack. They have the same variables. The tricky part is that I only want to keep people from Dataset 2 if they don't already have an entry in Dataset 1; that is, I want to add to Dataset 1 those people from Dataset 2 that are unique and not already in the dataset only.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know how to do this in two steps (merge the two such that only the people who are in Dataset 2 but not Dataset 1 are left, then stack that with Dataset 1 using a set statement), but I wanted to know if there's a way to do it in one step.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is much appreciated.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2016 17:02:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245499#M45819</guid>
      <dc:creator>Walternate</dc:creator>
      <dc:date>2016-01-22T17:02:23Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking two datasets with overlapping IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245504#M45822</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&amp;nbsp; &amp;nbsp; data want;
&amp;nbsp; &amp;nbsp; merge  have1(in=a)
&amp;nbsp; &amp;nbsp;        have2(in=b);
&amp;nbsp; &amp;nbsp; by key;
&amp;nbsp; &amp;nbsp; if (b=1 and a=0) /*keep people from 2 if not in 1*/
&amp;nbsp; &amp;nbsp; or (b=0 and a=1) /*keep people that 1 if not in 2*/
&amp;nbsp; &amp;nbsp; ;
&amp;nbsp; &amp;nbsp; run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jan 2016 17:23:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245504#M45822</guid>
      <dc:creator>DanZ</dc:creator>
      <dc:date>2016-01-22T17:23:12Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking two datasets with overlapping IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245505#M45823</link>
      <description>&lt;P&gt;This may do what you want. &amp;nbsp;Without more details it is hard to say. &amp;nbsp;Assumes one record per ID.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
   do id = 1,3,4,6,7;
      output;
      end;
   retain x 'a';
   run;
data b;
   do id=1,2,3,5,6,7;
      output;
      end;
   retain x 'b';
   run;
data c;
   set a b;
   by id;
   if first.id;
   run;
proc print;
   run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/1545iD24449FEDD83D9D0/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2016 17:22:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245505#M45823</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-01-22T17:22:01Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking two datasets with overlapping IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245507#M45824</link>
      <description>&lt;P&gt;A SQL solution that's probably not more efficient &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; and after borrowing&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15410"&gt;@data_null__﻿&lt;/a&gt;'s fake data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
   do id = 1,3,4,6,7;
      output;
      end;
   retain x 'a';
   run;
data b;
   do id=1,2,3,5,6,7;
      output;
      end;
   retain x 'b';
   run;

proc sql;
create table want as
select * from(
select * from a
union
select * from b
where id not in (select distinct Id from a)) a
order by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jan 2016 17:33:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245507#M45824</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-22T17:33:22Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking two datasets with overlapping IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245516#M45828</link>
      <description>&lt;P&gt;What is missing in your request is what to do with people that are present in both datasets but with different information. If you want to keep both records, try this approach:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data a;
input id x$;
datalines;
1 a
3 b
4 c
6 d
7 e
;
data b;
input id x$;
datalines;
1 a
2 b
3 c
5 d
6 e
7 f
;

proc sql;
create table c as
select *
from
    (select * from a)
    union 
    (select * from b);
select * from c;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jan 2016 18:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245516#M45828</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-01-22T18:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking two datasets with overlapping IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245538#M45839</link>
      <description>&lt;P&gt;As others have noted, it makes a big difference knowing how many observations might be in the data sets for each person. &amp;nbsp;If there is only one observation for each person, the program is actually as simple as this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;merge dataset2 dataset1;&lt;/P&gt;
&lt;P&gt;by key;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any KEY values that appear in both data sets will have the DATASET1 values in them after a MERGE. &amp;nbsp;Of course, this assumes you are working with sorted data sets.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If there could be more than one observation per person in either data set, we need to know that. &amp;nbsp;And we also need to know more details about what you want the outcome to be.&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2016 20:39:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-two-datasets-with-overlapping-IDs/m-p/245538#M45839</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-01-22T20:39:45Z</dc:date>
    </item>
  </channel>
</rss>

