<?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: Identifying ids with two values in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570519#M160882</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                    
  input id test:$1. ;          
  cards ;                      
1  A                           
1  B                           
2  B                           
3  A                           
3  B                           
3  B                           
4  A                           
4  B                           
5  A         
; 
run ;     

proc sql;
select distinct id
 from have
  group by id
   having max(test='A') and max(test='B');
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 02 Jul 2019 12:32:26 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2019-07-02T12:32:26Z</dc:date>
    <item>
      <title>Identifying ids with two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570129#M160741</link>
      <description>&lt;P&gt;I have a question about how to identify id values that meet certain criteria.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is a sample of the data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;id &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;test&lt;/P&gt;&lt;P&gt;1&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;B&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;3&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;B&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;4&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;B&lt;/P&gt;&lt;P&gt;5&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;A&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to figure out the code that would allow me to identify the ids that have both A and B as test values.&lt;/P&gt;&lt;P&gt;So in this example, I want to identify the following ids:&lt;/P&gt;&lt;P&gt;id&lt;/P&gt;&lt;P&gt;1&lt;/P&gt;&lt;P&gt;3&lt;/P&gt;&lt;P&gt;4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any advice?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 00:05:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570129#M160741</guid>
      <dc:creator>SarahW13</dc:creator>
      <dc:date>2019-07-01T00:05:11Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying ids with two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570135#M160744</link>
      <description>&lt;P&gt;This may look like over-egging the pudding, but it allows for any variations in the population of your source data. #ThinkingAhead&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data start;
infile cards dsd dlm=',' firstobs=2;
attrib id length=3;
attrib test length=$ 1;
input id
      test;
cards;
id,test
1,A
1,B
2,B
3,A
3,B
3,B
4,A
4,B
5,A
;
run;

proc sort data=start noequals;
by id;
run;

data target;
set start;
by id;
attrib test_a test_b length=$ 1;
retain test_a test_b;
if first.id then 
   call missing(test_a, test_b);
select(test);
   when('A') test_a = 'Y';
   when('B') test_b = 'Y';
   otherwise;
   end;
if last.id;
if test_a ne ' ' and test_b ne ' ';
keep id;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 01 Jul 2019 03:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570135#M160744</guid>
      <dc:creator>LaurieF</dc:creator>
      <dc:date>2019-07-01T03:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying ids with two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570137#M160745</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/194212"&gt;@SarahW13&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;Assuming that your input file is sorted by ID:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                    
  input id test:$1. ;          
  cards ;                      
1  A                           
1  B                           
2  B                           
3  A                           
3  B                           
3  B                           
4  A                           
4  B                           
5  A                           
run ;                          
                               
data want (keep = id) ;        
  do until (last.id) ;         
    set have ;                 
    by id ;                    
    if test = "A" then _A = 1 ;
    if test = "B" then _B = 1 ;
  end ;                        
  if _A and _B ;               
run ;                          
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if your input file isn't sorted and/or you prefer SQL:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;                                                          
  create table want as select unique id from have                          
  where  test = "A" and id in (select id from have where test = "B")
  ;                                                                 
quit ;                                                              
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;"A" and "B" in the WHERE clause are mutually interchangeable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 05:12:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570137#M160745</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-01T05:12:07Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying ids with two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570139#M160746</link>
      <description>If the variable "test" has only the values A and B,you could try the following untested code:&lt;BR /&gt;&lt;BR /&gt;proc sort data=have out=sorted nodupkey;&lt;BR /&gt;by id test;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;data want;&lt;BR /&gt;set sorted;&lt;BR /&gt;by id;&lt;BR /&gt;if first.id and not last.id;&lt;BR /&gt;keep id;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;Written on my way to work.</description>
      <pubDate>Mon, 01 Jul 2019 05:50:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570139#M160746</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2019-07-01T05:50:22Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying ids with two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570286#M160788</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id test $;
cards;
1 A
1 B
2 B
3 A
3 B
3 B
4 A
4 B
5 A
;

data new;
  merge have (in=a where = (test="A")) have (in=b where = (test="B"));
  by id;
  if a and b;
run;
&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;PROC SQL;
CREATE TABLE want AS
SELECT DISTINCT id
FROM new
;
QUIT;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 15:17:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570286#M160788</guid>
      <dc:creator>ghosh</dc:creator>
      <dc:date>2019-07-01T15:17:24Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying ids with two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570305#M160799</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/78622"&gt;@ghosh&lt;/a&gt;&amp;nbsp;:&lt;/P&gt;
&lt;P&gt;If you end up using SQL anyway, why precede it with the extra merge step with the assumption that the input file is sorted by ID when a single SQL step can produce the result without making the assumption, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                              
  input id test:$1. ;                    
  cards ;                                
1  A                                     
1  B                                     
2  B                                     
3  A                                     
3  B                                     
3  B                                     
4  A                                     
4  B                                     
5  A                                     
run ;                                    
                                         
proc sql ;                               
  create table want as select unique A.id
  from   have (where = (test = "A")) A   
       , have (where = (test = "B")) B   
  where  A.id = B.id                     
  ;                                      
quit ;                                   
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Of course, this is merely a join alternative to the subquery I've offered earlier. On the other hand, if the input file is sorted by ID, SQL isn't needed, as a single DATA step with BY will suffice and be more efficient to boot.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kind regards&lt;/P&gt;
&lt;P&gt;Paul D.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Jul 2019 15:41:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570305#M160799</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2019-07-01T15:41:11Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying ids with two values</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570519#M160882</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;                    
  input id test:$1. ;          
  cards ;                      
1  A                           
1  B                           
2  B                           
3  A                           
3  B                           
3  B                           
4  A                           
4  B                           
5  A         
; 
run ;     

proc sql;
select distinct id
 from have
  group by id
   having max(test='A') and max(test='B');
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Jul 2019 12:32:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Identifying-ids-with-two-values/m-p/570519#M160882</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-07-02T12:32:26Z</dc:date>
    </item>
  </channel>
</rss>

