<?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 Help with WHERE statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669016#M200645</link>
    <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to write a statement to include only certain variables. Take the sample data below:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Index&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in this instance, I am only trying to include the IDs when they &lt;EM&gt;only&amp;nbsp;&lt;/EM&gt;have an A Index, and no B Index. So using this sample data I would only want ID #2 because that is the only ID that doesn't also have a B Index.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this make sense? How can I code for this?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 13 Jul 2020 23:24:03 GMT</pubDate>
    <dc:creator>marleeakerson</dc:creator>
    <dc:date>2020-07-13T23:24:03Z</dc:date>
    <item>
      <title>Help with WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669016#M200645</link>
      <description>&lt;P&gt;Hello,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am trying to write a statement to include only certain variables. Take the sample data below:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Index&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;
&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;
&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;
&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in this instance, I am only trying to include the IDs when they &lt;EM&gt;only&amp;nbsp;&lt;/EM&gt;have an A Index, and no B Index. So using this sample data I would only want ID #2 because that is the only ID that doesn't also have a B Index.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this make sense? How can I code for this?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jul 2020 23:24:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669016#M200645</guid>
      <dc:creator>marleeakerson</dc:creator>
      <dc:date>2020-07-13T23:24:03Z</dc:date>
    </item>
    <item>
      <title>Re: Help with WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669023#M200650</link>
      <description>&lt;P&gt;proc sql;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; create table temp select distinct ID from have&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; where index='B;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; create table want as select * from have&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; where ID not in (select ID from temp);&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;</description>
      <pubDate>Mon, 13 Jul 2020 23:51:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669023#M200650</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2020-07-13T23:51:52Z</dc:date>
    </item>
    <item>
      <title>Re: Help with WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669024#M200651</link>
      <description>&lt;P&gt;This should be easy to accomplish with SQL. If your dataset is huge, there might be more efficient ways to do it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    infile datalines;
    input ID :8. Index $1.;
datalines;
1 A
1 B
1 B
2 A
3 A
3 B
;
run;
 
proc sql;
create table want as
select id
  from have 
 where index='A'
   and id not in (SELECT id from have where Index = 'B')
 order by id;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;ID
2&lt;/PRE&gt;</description>
      <pubDate>Mon, 13 Jul 2020 23:54:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669024#M200651</guid>
      <dc:creator>ketpt42</dc:creator>
      <dc:date>2020-07-13T23:54:36Z</dc:date>
    </item>
    <item>
      <title>Re: Help with WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669049#M200659</link>
      <description>&lt;P&gt;If the data are sorted by ID, then:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
    infile datalines;
    input ID :8. Index $1.;
datalines;
1 A
1 B
1 B
2 A
3 A
3 B
run;

data want (drop=_:);
  set have (where=(index^='A'))
      have (where=(index='A'));
  by id;
  if first.id=1 and index='A' then _keep+1;
  else if first.id then _keep=0;
  if _keep;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Jul 2020 01:33:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669049#M200659</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-07-14T01:33:27Z</dc:date>
    </item>
    <item>
      <title>Re: Help with WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669053#M200660</link>
      <description>&lt;P&gt;Proc SQL with autoremerge provides this simple solution:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select *
from have
group by ID
having sum(Index ne "A") = 0;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 14 Jul 2020 01:50:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669053#M200660</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2020-07-14T01:50:44Z</dc:date>
    </item>
    <item>
      <title>Re: Help with WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669095#M200678</link>
      <description>&lt;P&gt;The fastest method would be a data step, if your data is already sorted:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
merge
  have
  have (
    in=delete
    keep=id index
    rename=(index=_i)
    where=(_i = 'B')
  )
;
by id;
if not delete;
drop _i;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If it's not sorted, a hash table can do it:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
if _N_ = 1
then do;
  declare hash h (dataset:"have (where=(index = 'B'))");
  h.definekey("id");
  h.definedone();
end;
if h.check() ne 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The hash declaration will automatically discard duplicate entries for ID, so the in-memory table should be fairly small.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Both methods should outperform SQL if the table is quite large.&lt;/P&gt;</description>
      <pubDate>Tue, 14 Jul 2020 06:20:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669095#M200678</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-14T06:20:45Z</dc:date>
    </item>
    <item>
      <title>Re: Help with WHERE statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669142#M200694</link>
      <description>Same as PG's code:&lt;BR /&gt;&lt;BR /&gt;data have;&lt;BR /&gt;    infile datalines;&lt;BR /&gt;    input ID :8. Index $1.;&lt;BR /&gt;datalines;&lt;BR /&gt;1 A&lt;BR /&gt;1 B&lt;BR /&gt;1 B&lt;BR /&gt;2 A&lt;BR /&gt;3 A&lt;BR /&gt;3 B&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select * from have&lt;BR /&gt; group by id&lt;BR /&gt;  having count(*)=sum(Index='A');&lt;BR /&gt;quit;&lt;BR /&gt;</description>
      <pubDate>Tue, 14 Jul 2020 11:59:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-WHERE-statement/m-p/669142#M200694</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-07-14T11:59:46Z</dc:date>
    </item>
  </channel>
</rss>

