<?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: Subset data based on the values NOT specified using EXCEPT logic in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504720#M135125</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This solved the problem&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data; set;&lt;BR /&gt;code=substr(omm_dx_cd,1,3);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table undefined as&lt;BR /&gt;select * from&amp;nbsp;source_data&lt;BR /&gt;where display_id not in &lt;BR /&gt;(select display_id from &lt;SPAN&gt;source_data&lt;/SPAN&gt; where code in &lt;BR /&gt;('153','154','159','C18','C19','C78','D01','C20','C21','C7A','239', '197','209','230','211','235','Z12','199','D12','D37','D49'));&lt;BR /&gt;quit;&lt;/P&gt;</description>
    <pubDate>Tue, 16 Oct 2018 13:55:57 GMT</pubDate>
    <dc:creator>Cruise</dc:creator>
    <dc:date>2018-10-16T13:55:57Z</dc:date>
    <item>
      <title>Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504513#M135015</link>
      <description>&lt;P&gt;I'd like to look at the patients who did not take certain codes, in this example: 1,2,6&amp;nbsp;and create data WANT. So I will be able to assess the patients who did not take the codes.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id code;
cards;
1	1
1	2
1	3
1	3
1	5
2	1
2	2
2	2
2	4
2	5
3	3
3	4
3	5
4   8
4   9
5   6
5   10
;

data want;
input id code;
cards;
3	3
3	4
3	5
4   8
4   9
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I don't&amp;nbsp;patients' info anymore as long as they were defined having 1,2 or 6. I tried the code below, but it doesn't exclude the patients who has codes of 1,2 and 6.&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 
where id in 
    (select id from have where code not in (1,2,6));
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 12 Nov 2018 15:42:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504513#M135015</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-11-12T15:42:49Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504519#M135018</link>
      <description>proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select * from have &lt;BR /&gt;where id not in &lt;BR /&gt;    (select id from have where code in (1,2,6));&lt;BR /&gt;quit;&lt;BR /&gt;&lt;BR /&gt;What about that?</description>
      <pubDate>Mon, 15 Oct 2018 22:41:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504519#M135018</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-15T22:41:26Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504525#M135023</link>
      <description>Thanks! I see a bad logic in my code.</description>
      <pubDate>Mon, 15 Oct 2018 22:59:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504525#M135023</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-10-15T22:59:06Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504527#M135025</link>
      <description>&lt;P&gt;Your logic is wrong.&lt;/P&gt;
&lt;P&gt;ID=1 is kept since it has CODE=3.&lt;/P&gt;
&lt;P&gt;This works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table WANT as
  select * from HAVE 
  where ID not in 
    (select unique ID from HAVE where CODE in (1,2,6));
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Boolean logic requires careful thinking. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 23:03:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504527#M135025</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-10-15T23:03:55Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504529#M135027</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&amp;nbsp;and&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;both codes produced the same result with N=46,840,259 using my actual datasets.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table want_Reeza as&lt;BR /&gt;select * from have&lt;BR /&gt;where id not in&lt;BR /&gt;(select id from have where code in (1,2,6));&lt;BR /&gt;quit;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt; create table WANT_cris as&lt;BR /&gt; select * from HAVE &lt;BR /&gt; where ID not in &lt;BR /&gt; (select unique ID from HAVE where CODE in (1,2,6));&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 23:16:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504529#M135027</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-10-15T23:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504530#M135028</link>
      <description>I guess Chris's version would deduplicate ID to distinct ones had I had the same IDs multiple times.</description>
      <pubDate>Mon, 15 Oct 2018 23:18:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504530#M135028</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-10-15T23:18:23Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504532#M135029</link>
      <description>&lt;P&gt;I only return&amp;nbsp;each ID once in the list. My reply is basically the same as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;'s, I didn't see there already was an answer.&lt;/P&gt;</description>
      <pubDate>Mon, 15 Oct 2018 23:21:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504532#M135029</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-10-15T23:21:56Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504576#M135053</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt; ,&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;@&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm PUZZLED here. So I expect to end up with mutually exclusive datasets to defined vs not defined. And n of rows in final datasets suppose to add up to n of rows in the SOURCE_DATA. However, it doesn't and also there is no missing in CODE variable in the source data.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;15,101+947,133 do NOT add up to 997,910&lt;/P&gt;
&lt;P&gt;while&lt;/P&gt;
&lt;P&gt;15,2014 + 608 do NOT add up to 17,068.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is something wrong.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any hints, as to why? Any guess or pointers?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table not_defined /*N=15,101*/ as
select * from SOURCE_DATA /*N=997,910*/
where display_id not in 
(select display_id from med_dx where substr(code,1,3) in 
('153','154','159','C18','C19','C78','D01','C20','C21','C7A','239',
 '197','209','230','211','235','Z12','199','D12','D37','D49'));
quit;

proc sql;
create table defined /*N=947,133*/ as
select * from SOURCE_DATA /*N=997,910*/
where display_id in 
(select display_id from med_dx_mmyy where substr(code,1,3) in ('153','154','159','C18','C19','C78','D01','C20','C21',
'C7A','239','197','209','230','211','235','Z12','199','D12','D37','D49'));
quit;

/*VALIDATING*/

proc sql;
select count(distinct display_id)
from SOURCE_DATA; /*N=17,068*/
quit;

proc sql;
select count(distinct display_id)
from defined; /*N=15,214 defined*/
quit;

proc sql;
select count(distinct display_id)
from not_defined; /*N=608 not defined*/
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Oct 2018 03:13:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504576#M135053</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-10-16T03:13:27Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504577#M135054</link>
      <description>&lt;P&gt;Are you querying SAS tables?&lt;/P&gt;
&lt;P&gt;If not, the issue is probably null values, which satisfy neither condition.&lt;/P&gt;</description>
      <pubDate>Tue, 16 Oct 2018 03:25:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504577#M135054</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-10-16T03:25:51Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504583#M135060</link>
      <description>You may have some categories not included in your list. Check the codes in your not defined dataset.</description>
      <pubDate>Tue, 16 Oct 2018 04:03:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504583#M135060</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2018-10-16T04:03:21Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504587#M135062</link>
      <description>&lt;P&gt;Also note that a join would be much&amp;nbsp;faster. Something like&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table UNDEFINED as
  select * &lt;BR /&gt;  from SOURCE_DATA source
  left join 
  (select unique DISPLAY_ID &lt;BR /&gt;   from med_dx_mmyy &lt;BR /&gt;   where substr(code,1,3) in ('153','154','159','C18','C19','C78','D01','C20','C21',
                  'C7A','239','197','209','230','211','235','Z12','199','D12','D37','D49')) list
  on source.DISPLAY_ID=list.DISPLAY_ID
  where list.DISPLAY_ID is null;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Oct 2018 04:22:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504587#M135062</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2018-10-16T04:22:07Z</dc:date>
    </item>
    <item>
      <title>Re: Subset data based on the values NOT specified using EXCEPT logic</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504720#M135125</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This solved the problem&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data; set;&lt;BR /&gt;code=substr(omm_dx_cd,1,3);&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;create table undefined as&lt;BR /&gt;select * from&amp;nbsp;source_data&lt;BR /&gt;where display_id not in &lt;BR /&gt;(select display_id from &lt;SPAN&gt;source_data&lt;/SPAN&gt; where code in &lt;BR /&gt;('153','154','159','C18','C19','C78','D01','C20','C21','C7A','239', '197','209','230','211','235','Z12','199','D12','D37','D49'));&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Tue, 16 Oct 2018 13:55:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subset-data-based-on-the-values-NOT-specified-using-EXCEPT-logic/m-p/504720#M135125</guid>
      <dc:creator>Cruise</dc:creator>
      <dc:date>2018-10-16T13:55:57Z</dc:date>
    </item>
  </channel>
</rss>

