<?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 datasets while retaining only shared IDs in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983876#M379602</link>
    <description>&lt;P&gt;One approach is shown below. The code is untested. If you want tested code, please provide data as working SAS data step code (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;examples and instructions&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* UNTESTED CODE */

/* Combine all rows */
data intermediate;
    set dataset1(in=in1) dataset2(in=in2);
    by id;
    ds1=in1;
    ds2=in2;
run;

/* Determine for each ID if there is one or more rows from dataset1 and one or more rows from dataset 2 */
proc summary data=intermediate;
    class id;
    var ds1 ds2;
    output out=sums sum=;
run;

/* Remove unwanted rows */
data want;
    merge intermediate sums;
    by id;
    if ds1&amp;gt;0 and ds2&amp;gt;0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 23 Feb 2026 16:25:12 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2026-02-23T16:25:12Z</dc:date>
    <item>
      <title>Stacking datasets while retaining only shared IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983875#M379601</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My data roughly looks something like the below, where all variables are shared in each of two datasets, and there can be multiple rows for each given ID.&lt;/P&gt;&lt;P&gt;I'd like to make a dataset that stacks the two datasets together, but only retains IDs that appear in both Dataset1 and Dataset2. It's expected that there will be multiple rows for a single ID in both the source and final datasets, I just do not want to retain IDs that appeared in only one of Dataset1 or Dataset2.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset1:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Var1&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dataset2&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Var1&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Desired end state:&lt;/P&gt;&lt;TABLE border="1"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;ID&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Var1&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, as you can see, my desired end state retains only IDs 3 and 4, since those are the shared IDs between the two source datasets. But it still is fundamentally just stacking: I still want separate rows for each individual observation (as in reality, there are many more variables and ID is just identifying unique individuals, not unique observations).&lt;/P&gt;</description>
      <pubDate>Mon, 23 Feb 2026 15:58:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983875#M379601</guid>
      <dc:creator>des123</dc:creator>
      <dc:date>2026-02-23T15:58:06Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking datasets while retaining only shared IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983876#M379602</link>
      <description>&lt;P&gt;One approach is shown below. The code is untested. If you want tested code, please provide data as working SAS data step code (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;examples and instructions&lt;/A&gt;).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* UNTESTED CODE */

/* Combine all rows */
data intermediate;
    set dataset1(in=in1) dataset2(in=in2);
    by id;
    ds1=in1;
    ds2=in2;
run;

/* Determine for each ID if there is one or more rows from dataset1 and one or more rows from dataset 2 */
proc summary data=intermediate;
    class id;
    var ds1 ds2;
    output out=sums sum=;
run;

/* Remove unwanted rows */
data want;
    merge intermediate sums;
    by id;
    if ds1&amp;gt;0 and ds2&amp;gt;0;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Feb 2026 16:25:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983876#M379602</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2026-02-23T16:25:12Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking datasets while retaining only shared IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983898#M379611</link>
      <description>&lt;P&gt;If the data is sorted by ID then it is pretty simple.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First let's convert your listings into actual datasets so we have something to program with.&lt;/P&gt;
&lt;LI-SPOILER&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
  input id var1 ;
cards;
1 0
2 0
2 1
3 1
3 0
4 1
; 

data two;
  input id var1 ;
cards;
3 2
4 2
5 0
6 0
7 0
; 

data expect;
  input id var1;
cards;
3 1
3 0
3 2
4 1
4 2
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;/LI-SPOILER&gt;
&lt;P&gt;Now just use a couple of DO loops.&amp;nbsp; The first to check whether both datasets are contributing. And the second to actually process the data and output the desired observations.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do until(last.id);
     merge one(in=in1) two(in=in2);
     by id;
  end;
  do until (last.id);
     set one two;
     by id;
     if in1 and in2 then output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="image.png" style="width: 806px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/113302i7DDCFC4FF64B69AC/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Feb 2026 20:20:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983898#M379611</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-02-23T20:20:48Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking datasets while retaining only shared IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983944#M379622</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data datasets1;
input id var1;
cards;
1 0
2 0
2 1
3 1
3 0
4 1
;
data datasets2;
input id var1;
cards;
3 2
4 2
5 0
6 0
7 0
;


proc sql;
create table want as
select * from datasets1 where id in (select id from datasets2)
union all
select * from datasets2 where id in (select id from datasets1)
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 24 Feb 2026 05:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983944#M379622</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2026-02-24T05:54:25Z</dc:date>
    </item>
    <item>
      <title>Re: Stacking datasets while retaining only shared IDs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983975#M379631</link>
      <description>Thanks so much! This method was the first one I tried from this thread, and it worked like a charm!</description>
      <pubDate>Tue, 24 Feb 2026 16:21:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Stacking-datasets-while-retaining-only-shared-IDs/m-p/983975#M379631</guid>
      <dc:creator>des123</dc:creator>
      <dc:date>2026-02-24T16:21:20Z</dc:date>
    </item>
  </channel>
</rss>

