<?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: Keep group if value appears in both columns in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/509075#M136801</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/182623"&gt;@MB_Analyst&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if _n_=1 then do;
  dcl hash h1();
  h1.definekey('current');
  h1.definedone();
  dcl hash h2();
  h2.definekey('from');
  h2.definedone();
end;
do until(last.id);
  set have;
  by id;
  if current&amp;gt;. then h1.ref();
  if from&amp;gt;. then h2.ref();
end;
do until(last.id);
  set have;
  by id;
  if h1.check(key:from)=0 | h2.check(key:current)=0 then output;
end;
h1.clear();
h2.clear();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;UL&gt;
&lt;LI&gt;In the first DO-END block, which is executed only in the first iteration of the DATA step, two temporary look-up tables (hash objects) H1 and H2 are declared. They will store the distinct non-missing CURRENT and FROM values, respectively, for&amp;nbsp;a single ID (one ID per iteration of the&amp;nbsp;DATA step).&lt;/LI&gt;
&lt;LI&gt;The second DO-END block populates H1 and H2 with the values described above. It is assumed that HAVE is sorted by ID.&lt;/LI&gt;
&lt;LI&gt;The third DO-END block reads the same BY group again and applies the selection criterion. The CHECK method returns 0 if the key value is found (i.e. the FROM value in the table of CURRENT values or vice versa). If at least one of the two look-ups yields a match, the observation is written to dataset WANT. Note that observations with missing values for CURRENT &lt;EM&gt;and&lt;/EM&gt; FROM are &lt;EM&gt;not&lt;/EM&gt; written to the output dataset.&lt;/LI&gt;
&lt;LI&gt;After each BY group the look-up tables are cleared.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 31 Oct 2018 12:27:06 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2018-10-31T12:27:06Z</dc:date>
    <item>
      <title>Keep group if value appears in both columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/508894#M136715</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have some data with an ID (grouping variable) and I want to keep observations in a group if any number from column "current" shows up in column "from". Or vice versa.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA have;
    input ID current from;
    datalines;
1     100    .
1     200    .
1     300   100
1     400   100
1     200    .
1     500    300
;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;So in this case I would drop the 2nd and 5th observation as it's only contained within one column.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 19:18:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/508894#M136715</guid>
      <dc:creator>MB_Analyst</dc:creator>
      <dc:date>2018-10-30T19:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: Keep group if value appears in both columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/508897#M136716</link>
      <description>&lt;P&gt;Do you just have 2 columns to check or many. if it is just two columns, very straight forward. Can you confirm plz&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And what about 400,500 obs 4 and 6. Those are also contained in only current column???&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 19:31:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/508897#M136716</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-30T19:31:51Z</dc:date>
    </item>
    <item>
      <title>Re: Keep group if value appears in both columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/508921#M136728</link>
      <description>&lt;P&gt;I only have two columns to check. The 4th and 6th column would stay in because:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;OBS 4 - 100 is within the first column&lt;/P&gt;&lt;P&gt;OBS 6 - 300 is within the first column&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 20:11:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/508921#M136728</guid>
      <dc:creator>MB_Analyst</dc:creator>
      <dc:date>2018-10-30T20:11:03Z</dc:date>
    </item>
    <item>
      <title>Re: Keep group if value appears in both columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/508922#M136729</link>
      <description>&lt;P&gt;I mean the 400, 500&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 20:12:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/508922#M136729</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2018-10-30T20:12:09Z</dc:date>
    </item>
    <item>
      <title>Re: Keep group if value appears in both columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/508926#M136731</link>
      <description>&lt;P&gt;Yes, when the "current" column is 400 or 500, the "from" column is 100 and 300. Because both 100 and 300 appear in the first column, both observations are retained.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 30 Oct 2018 20:15:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/508926#M136731</guid>
      <dc:creator>MB_Analyst</dc:creator>
      <dc:date>2018-10-30T20:15:13Z</dc:date>
    </item>
    <item>
      <title>Re: Keep group if value appears in both columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/509075#M136801</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/182623"&gt;@MB_Analyst&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Try this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
if _n_=1 then do;
  dcl hash h1();
  h1.definekey('current');
  h1.definedone();
  dcl hash h2();
  h2.definekey('from');
  h2.definedone();
end;
do until(last.id);
  set have;
  by id;
  if current&amp;gt;. then h1.ref();
  if from&amp;gt;. then h2.ref();
end;
do until(last.id);
  set have;
  by id;
  if h1.check(key:from)=0 | h2.check(key:current)=0 then output;
end;
h1.clear();
h2.clear();
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;UL&gt;
&lt;LI&gt;In the first DO-END block, which is executed only in the first iteration of the DATA step, two temporary look-up tables (hash objects) H1 and H2 are declared. They will store the distinct non-missing CURRENT and FROM values, respectively, for&amp;nbsp;a single ID (one ID per iteration of the&amp;nbsp;DATA step).&lt;/LI&gt;
&lt;LI&gt;The second DO-END block populates H1 and H2 with the values described above. It is assumed that HAVE is sorted by ID.&lt;/LI&gt;
&lt;LI&gt;The third DO-END block reads the same BY group again and applies the selection criterion. The CHECK method returns 0 if the key value is found (i.e. the FROM value in the table of CURRENT values or vice versa). If at least one of the two look-ups yields a match, the observation is written to dataset WANT. Note that observations with missing values for CURRENT &lt;EM&gt;and&lt;/EM&gt; FROM are &lt;EM&gt;not&lt;/EM&gt; written to the output dataset.&lt;/LI&gt;
&lt;LI&gt;After each BY group the look-up tables are cleared.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Oct 2018 12:27:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/509075#M136801</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2018-10-31T12:27:06Z</dc:date>
    </item>
    <item>
      <title>Re: Keep group if value appears in both columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/509100#M136813</link>
      <description>&lt;P&gt;OK.&amp;nbsp; assume I understood your question. And there was only one ID/group&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA x;
    input ID current from;
    datalines;
1     100    .
1     200    .
1     300   100
1     400   100
1     200    .
1     500    300
;
data have;
 set x;
 	drop id;
	rename current=to;
run;


data full;
  set have end=last;
  if _n_ eq 1 then do;
   declare hash h();
    h.definekey('node');
     h.definedata('node');
     h.definedone();
  end;
  output;
  node=from; h.replace();
  from=to; to=node;
  output;
  node=from; h.replace();
  if last then h.output(dataset:'node');
  drop node;
run;


data want(keep=node household);
declare hash ha(ordered:'a');
declare hiter hi('ha');
ha.definekey('count');
ha.definedata('last');
ha.definedone();
declare hash _ha(hashexp: 20);
_ha.definekey('key');
_ha.definedone();

if 0 then set full;
declare hash from_to(dataset:'full(where=(from is not missing and to is not missing))',hashexp:20,multidata:'y');
 from_to.definekey('from');
 from_to.definedata('to');
 from_to.definedone();

if 0 then set node;
declare hash no(dataset:'node');
declare hiter hi_no('no');
 no.definekey('node');
 no.definedata('node');
 no.definedone();
 

do while(hi_no.next()=0);
 household+1; output;
 count=1;
 key=node;_ha.add();
 last=node;ha.add();
 rc=hi.first();
 do while(rc=0);
   from=last;rx=from_to.find();
   do while(rx=0);
     key=to;ry=_ha.check();
      if ry ne 0 then do;
       node=to;output;rr=no.remove(key:node);
       key=to;_ha.add();
       count+1;
       last=to;ha.add();
      end;
      rx=from_to.find_next();
   end;
   rc=hi.next();
end;
ha.clear();_ha.clear();
end;
stop;
run;


proc sql;
create table key as
 select * from want
  group by household
   having count(*)=1;

create table final_want as
 select * from x
  where  current not in (select node from key);
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 31 Oct 2018 13:36:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/509100#M136813</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-10-31T13:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: Keep group if value appears in both columns</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/509441#M136963</link>
      <description>&lt;P&gt;I have more than one ID group, but this does work for the one ID. Thank you.&lt;/P&gt;</description>
      <pubDate>Thu, 01 Nov 2018 10:28:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-group-if-value-appears-in-both-columns/m-p/509441#M136963</guid>
      <dc:creator>MB_Analyst</dc:creator>
      <dc:date>2018-11-01T10:28:51Z</dc:date>
    </item>
  </channel>
</rss>

