<?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: Querying dataset for unique column values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633267#M187856</link>
    <description>&lt;P&gt;Data step solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
ind_x = 0;
do until (last.system);
  set have;
  by system notsorted;
  if ind_x ne . and class = 'x' then ind_x = 1;
  if class ne 'x' then ind_x = .;
end;
do until (last.system);
  set have;
  by system notsorted;
  if ind_x then output;
end;
drop ind_x;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 19 Mar 2020 13:14:16 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-03-19T13:14:16Z</dc:date>
    <item>
      <title>Querying dataset for unique column values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633254#M187844</link>
      <description>&lt;P&gt;I have an input dataset with two columns(system and class). I want to create a dataset from that containing only those systems that only have a class of x. The same system can have multiple entries. If one entry has a class of X, and another entry a different class, then I don't want the system returned. There are 36 potential values of class(a-z and 0-9).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My input looks like this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;  data have;                  
    input system $ class $ ;  
    datalines;                
 fred  a                      
 fred  x                      
 wilma a                      
 wilma b                      
 wilma b                      
 betty x                      
 betty x                      
 ;                            
 run;                         &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;From this I would only want Betty returned. As Fred has a class of both A &amp;amp; X, while Wilma doesn't have X, I don't want either of them. Betty is the only one that only has a class of X.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm assuming PROC SQL would be the way to go here. Though I'm struggling to get a query that can return what I'm after.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Mar 2020 12:51:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633254#M187844</guid>
      <dc:creator>serge68</dc:creator>
      <dc:date>2020-03-19T12:51:39Z</dc:date>
    </item>
    <item>
      <title>Re: Querying dataset for unique column values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633262#M187850</link>
      <description>&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 have;                  
    input system $ class $ ;  
    datalines;                
 fred  a                      
 fred  x                      
 wilma a                      
 wilma b                      
 wilma b                      
 betty x                      
 betty x                      
 ;                            
 run;                   

proc sql;
   create table want as
   select * from have
   group by system
   having sum(class='x')=count(system);
quit; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Mar 2020 13:02:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633262#M187850</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2020-03-19T13:02:08Z</dc:date>
    </item>
    <item>
      <title>Re: Querying dataset for unique column values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633263#M187852</link>
      <description>&lt;P&gt;Assuming you want to select rows for any case where there is only one class per system and not only when it's 'x'&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
  create table want as
    select * 
    from have
    group by system
    having count(distinct class)=1
  ;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Mar 2020 13:12:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633263#M187852</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2020-03-19T13:12:37Z</dc:date>
    </item>
    <item>
      <title>Re: Querying dataset for unique column values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633264#M187853</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/100218"&gt;@serge68&lt;/a&gt;&amp;nbsp; A way to approach is to check&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Distinct values of class within BY GROUP system&lt;/P&gt;
&lt;P&gt;2. We need the distinct value as 1 and that value should be 'x'&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in SQL syntax&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;                  
    input system $ class $ ;  
    datalines;                
 fred  a                      
 fred  x                      
 wilma a                      
 wilma b                      
 wilma b                      
 betty x                      
 betty x                      
 ;                            
 run;      

proc sql;
create table want as
select *
from have
group by system
having count(distinct class)=1 and class='x';
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Mar 2020 13:08:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633264#M187853</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-03-19T13:08:44Z</dc:date>
    </item>
    <item>
      <title>Re: Querying dataset for unique column values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633267#M187856</link>
      <description>&lt;P&gt;Data step solution:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
ind_x = 0;
do until (last.system);
  set have;
  by system notsorted;
  if ind_x ne . and class = 'x' then ind_x = 1;
  if class ne 'x' then ind_x = .;
end;
do until (last.system);
  set have;
  by system notsorted;
  if ind_x then output;
end;
drop ind_x;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 19 Mar 2020 13:14:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633267#M187856</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-03-19T13:14:16Z</dc:date>
    </item>
    <item>
      <title>Re: Querying dataset for unique column values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633268#M187857</link>
      <description>Perfect! Thanks.</description>
      <pubDate>Thu, 19 Mar 2020 13:14:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633268#M187857</guid>
      <dc:creator>serge68</dc:creator>
      <dc:date>2020-03-19T13:14:17Z</dc:date>
    </item>
    <item>
      <title>Re: Querying dataset for unique column values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633275#M187861</link>
      <description>&lt;P&gt;A DATA step can process pre-grouped data and keep one row for each system having only class='x' cases&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA want(keep=system);&lt;BR /&gt;  do until (last.system);&lt;BR /&gt;    set have;&lt;BR /&gt;    by system notsorted;&lt;BR /&gt;    if not not_x then not_x = class ne 'x';&lt;BR /&gt;  end;&lt;BR /&gt;  if not not_x;&lt;BR /&gt;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Mar 2020 13:35:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Querying-dataset-for-unique-column-values/m-p/633275#M187861</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-03-19T13:35:04Z</dc:date>
    </item>
  </channel>
</rss>

