<?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 Proc compare a common variable in two data sets in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-compare-a-common-variable-in-two-data-sets/m-p/759519#M240029</link>
    <description>&lt;P&gt;&lt;FONT size="2"&gt;Hi All,&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;I have two data sets and would like to see the number of common IDs, number of IDs in data set 1 but not 2 and number of IDs in data set 2 but not 1. How should I write the&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;PROC COMPARE. Thank you. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Data Set 1:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;ID&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;1937&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;3947&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;6932&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;7059&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;9960&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;9999&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Data Set 2:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;ID&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;1937&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;3947&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;4509&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;7059&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;8394&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;8899&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;9304&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 04 Aug 2021 21:23:15 GMT</pubDate>
    <dc:creator>di_niu0</dc:creator>
    <dc:date>2021-08-04T21:23:15Z</dc:date>
    <item>
      <title>Proc compare a common variable in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-compare-a-common-variable-in-two-data-sets/m-p/759519#M240029</link>
      <description>&lt;P&gt;&lt;FONT size="2"&gt;Hi All,&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;I have two data sets and would like to see the number of common IDs, number of IDs in data set 1 but not 2 and number of IDs in data set 2 but not 1. How should I write the&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;PROC COMPARE. Thank you. &lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Data Set 1:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;ID&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;1937&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;3947&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;6932&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;7059&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;9960&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;9999&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;Data Set 2:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;ID&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;1937&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;3947&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;4509&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;7059&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;8394&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;8899&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;9304&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 21:23:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-compare-a-common-variable-in-two-data-sets/m-p/759519#M240029</guid>
      <dc:creator>di_niu0</dc:creator>
      <dc:date>2021-08-04T21:23:15Z</dc:date>
    </item>
    <item>
      <title>Re: Proc compare a common variable in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-compare-a-common-variable-in-two-data-sets/m-p/759526#M240030</link>
      <description>&lt;P&gt;Code OUT= but not OUTBASE, OUTCOMP, or OUTDIFF.&amp;nbsp; The basic stats produced by a Compare will give you the record count for the first dataset (well, both, but you asked about the first).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sample code, below.&amp;nbsp; Results below that.&amp;nbsp; This code is borrowed from a macro I wrote for something else; you don't need to use a macro, but it's helpful if you have multiple datasets to compare.&amp;nbsp; The results will be in&amp;nbsp;&lt;CODE class=" language-sas"&gt;WORK.Set_1c&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET	Old_Lib		=	WORK;
%LET	New_Lib		=	WORK;

%LET	Old_Data	=	Set_1;
%LET	New_Data	=	Set_2;

%LET	ID			=	ID;

**------------------------------------------------------------------------------**;

&amp;amp;Null	%MACRO	Compare_Datasets(Old_Lib, Old_Data, New_Lib, New_Data, ID=);
	PROC	SORT	DATA	=	&amp;amp;Old_Lib..&amp;amp;Old_Data
					OUT		=	WORK.&amp;amp;Old_Data.o;
		BY	&amp;amp;ID;
	RUN;

	PROC	SORT	DATA	=	&amp;amp;New_Lib..&amp;amp;New_Data
					OUT		=	WORK.&amp;amp;New_Data.n;
		BY	&amp;amp;ID;
	RUN;

	PROC	COMPARE	BASE	=	WORK.&amp;amp;Old_Data.o
					COMPARE	=	WORK.&amp;amp;New_Data.n
					OUT		=	WORK.&amp;amp;Old_Data.c	(DROP=_TYPE_ _OBS_)
					NOVALUES	LISTVAR
					;
		ID	&amp;amp;ID;
	RUN;
%MEND	Compare_Datasets;

%Compare_Datasets(&amp;amp;Old_Lib, &amp;amp;Old_Data, &amp;amp;New_Lib, &amp;amp;New_Data, ID=&amp;amp;ID);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&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="jimbarbour_0-1628114115725.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62276i146B05080465D484/image-size/large?v=v2&amp;amp;px=999" role="button" title="jimbarbour_0-1628114115725.png" alt="jimbarbour_0-1628114115725.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jimbarbour_1-1628114238210.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/62277i32334D0F421CC682/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jimbarbour_1-1628114238210.png" alt="jimbarbour_1-1628114238210.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Jim&lt;/P&gt;</description>
      <pubDate>Wed, 04 Aug 2021 21:57:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-compare-a-common-variable-in-two-data-sets/m-p/759526#M240030</guid>
      <dc:creator>jimbarbour</dc:creator>
      <dc:date>2021-08-04T21:57:33Z</dc:date>
    </item>
    <item>
      <title>Re: Proc compare a common variable in two data sets</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-compare-a-common-variable-in-two-data-sets/m-p/759546#M240039</link>
      <description>&lt;P&gt;A data step or SQL merge also work quite well here.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge data1(keep=ID in = d1) data2 (keep=ID in=d2);
by ID;

length flag $4.;

if d1 and d2 then flag="Both;
else if d1 then flag = "D1";
else if d2 then flag = "D2";

run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 04 Aug 2021 22:33:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-compare-a-common-variable-in-two-data-sets/m-p/759546#M240039</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-08-04T22:33:13Z</dc:date>
    </item>
  </channel>
</rss>

