<?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: Find IDs common to two distinct values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Find-IDs-common-to-two-distinct-values/m-p/692268#M210854</link>
    <description>&lt;P&gt;The dataset your code created can be modified further (didn't run this but should work):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length ph $21;
  set want;
  if input(strip(phone1), best.) &amp;gt; input(strip(phone2), best.) then  ph=phone1||" "||phone2;
  else ph=phone2||" "||phone1;
run;

proc sort;
  by ph;
run;

data want;
  set want;
  by ph;
  if first.ph;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sat, 17 Oct 2020 02:43:09 GMT</pubDate>
    <dc:creator>vellad</dc:creator>
    <dc:date>2020-10-17T02:43:09Z</dc:date>
    <item>
      <title>Find IDs common to two distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-IDs-common-to-two-distinct-values/m-p/692266#M210852</link>
      <description>&lt;P&gt;Want to know if there is a better way to approach this problem.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset that links a PH number to an account ID. See below:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;infile cards dsd ;&lt;BR /&gt;input Phone :$10. Account $ ;&lt;BR /&gt;cards;&lt;BR /&gt;123456789, 12345&lt;BR /&gt;123456789, 54321&lt;BR /&gt;123456789, 67890&lt;BR /&gt;123456789, 09876&lt;BR /&gt;987654321, 12345&lt;BR /&gt;987654321, 54321&lt;BR /&gt;987654321, 45879&lt;BR /&gt;987654321, 94532&lt;BR /&gt;987654321, 67890&lt;BR /&gt;543219876, 45879&lt;BR /&gt;543219876, 54873&lt;BR /&gt;543219876, 56454&lt;BR /&gt;543219876, 87654&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to create a dataset that counts the amount of common account IDs between two PH numbers (I would normally create an adjacency list in NetworkX/Python or iGraph/R to do this, but I cannot use either these technologies for this exercise).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;An example of my desired output below:&lt;/P&gt;&lt;P&gt;Phone1,Phone2,Common&lt;/P&gt;&lt;P&gt;123456789,543219876,0&lt;/P&gt;&lt;P&gt;123456789,987654321,3&lt;/P&gt;&lt;P&gt;543219876,987654321,1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The code I have created (see below) kind of does this. However it obviously duplicates the rows and creates following output:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Phone1,Phone2,Common&lt;/P&gt;&lt;DIV class="dgrid-header dgrid-header-row ui-widget-header"&gt;123456789,543219876,0&lt;BR /&gt;123456789,987654321,3&lt;BR /&gt;543219876,123456789,0&lt;BR /&gt;543219876,987654321,1&lt;BR /&gt;987654321,123456789,3&lt;BR /&gt;987654321,543219876,1&lt;/DIV&gt;&lt;DIV class="dgrid-scroller"&gt;&lt;DIV class="dgrid-content ui-widget-content"&gt;&lt;DIV class=" dgrid-row dgrid-row-even ui-state-default"&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;Thus wondering if there is a more efficient way to do this, perhaps using a loop in a data step?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table BlahBlah&lt;BR /&gt;as select distinct a.Phone as A, b.Phone as B,&lt;BR /&gt;sum(case when a.Account = b.Account then 1 else 0 end) as Common&lt;BR /&gt;from have a&lt;BR /&gt;inner join have b on a.Phone &amp;lt;&amp;gt; b.Phone&lt;BR /&gt;group by 1,2&lt;BR /&gt;; quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Oct 2020 02:11:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-IDs-common-to-two-distinct-values/m-p/692266#M210852</guid>
      <dc:creator>wylie_ma</dc:creator>
      <dc:date>2020-10-17T02:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: Find IDs common to two distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-IDs-common-to-two-distinct-values/m-p/692268#M210854</link>
      <description>&lt;P&gt;The dataset your code created can be modified further (didn't run this but should work):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  length ph $21;
  set want;
  if input(strip(phone1), best.) &amp;gt; input(strip(phone2), best.) then  ph=phone1||" "||phone2;
  else ph=phone2||" "||phone1;
run;

proc sort;
  by ph;
run;

data want;
  set want;
  by ph;
  if first.ph;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 17 Oct 2020 02:43:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-IDs-common-to-two-distinct-values/m-p/692268#M210854</guid>
      <dc:creator>vellad</dc:creator>
      <dc:date>2020-10-17T02:43:09Z</dc:date>
    </item>
    <item>
      <title>Re: Find IDs common to two distinct values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Find-IDs-common-to-two-distinct-values/m-p/692312#M210882</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/341412"&gt;@wylie_ma&lt;/a&gt;&amp;nbsp; Keeping it simple albeit I acknowledge is boring-&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards dsd ;
input Phone :$10. Account $ ;
cards;
123456789, 12345
123456789, 54321
123456789, 67890
123456789, 09876
987654321, 12345
987654321, 54321
987654321, 45879
987654321, 94532
987654321, 67890
543219876, 45879
543219876, 54873
543219876, 56454
543219876, 87654
;
/*All pairs*/
data temp/view=temp;
 merge have(rename=(phone=phone1 account=account1)) have(rename=(phone=phone2 account=account2));
run;
proc freq data=temp noprint;
 tables phone1*phone2/ sparse out=temp2(where=(phone1 ne phone2) keep=phone:);
run;
/* Compute common*/
proc sql;
 create table common as
 select a.*,ifn(common=.,0,common) as common
 from
 (select * from temp2) a
 left join
 (select a.phone as phone1,b.phone as phone2,sum(a.account= b.account) as common
 from have a, have b
 where a.account=b.account and a.phone ne b.phone
 group by phone1,phone2) b
 on a.phone1=b.phone1 and a.phone2=b.phone2;
quit;
/*get the Phone pairs order to remove dups in the next step*/
data _common;
 set common;
 call sortc(phone1,phone2);
run;
/*Final step*/
proc sort data=_common out=want nodupkey;
 by phone1 phone2;
run;

proc print noobs;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure Print: Data Set WORK.WANT" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="l header" scope="col"&gt;phone1&lt;/TH&gt;
&lt;TH class="l header" scope="col"&gt;phone2&lt;/TH&gt;
&lt;TH class="r header" scope="col"&gt;common&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;123456789&lt;/TD&gt;
&lt;TD class="l data"&gt;543219876&lt;/TD&gt;
&lt;TD class="r data"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;123456789&lt;/TD&gt;
&lt;TD class="l data"&gt;987654321&lt;/TD&gt;
&lt;TD class="r data"&gt;3&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD class="l data"&gt;543219876&lt;/TD&gt;
&lt;TD class="l data"&gt;987654321&lt;/TD&gt;
&lt;TD class="r data"&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;</description>
      <pubDate>Sat, 17 Oct 2020 20:21:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Find-IDs-common-to-two-distinct-values/m-p/692312#M210882</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-10-17T20:21:30Z</dc:date>
    </item>
  </channel>
</rss>

