<?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: How to find two variables that exist in the same group only in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829149#M327572</link>
    <description>&lt;P&gt;Untested:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=sorted nodupkey;
   by group code;
run;

data want;
   set sorted;
   by group;
   
   if not first.group and code = 'B' and lag(code) = 'A';
   
   keep group;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;EDIT: Simplified the code.&amp;nbsp; &lt;/P&gt;</description>
    <pubDate>Thu, 18 Aug 2022 04:58:58 GMT</pubDate>
    <dc:creator>andreas_lds</dc:creator>
    <dc:date>2022-08-18T04:58:58Z</dc:date>
    <item>
      <title>How to find two variables that exist in the same group only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829092#M327545</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for how to find two different variables but only if they exist within the same group. For example I want a list of groups where both A and B exist together. In the example below, that would be Group 1, Group 3, and Group 4. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Group&lt;/TD&gt;&lt;TD&gt;Code&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;C&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;B&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Wed, 17 Aug 2022 17:39:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829092#M327545</guid>
      <dc:creator>sashelp123</dc:creator>
      <dc:date>2022-08-17T17:39:52Z</dc:date>
    </item>
    <item>
      <title>Re: How to find two variables that exist in the same group only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829093#M327546</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/169209"&gt;@sashelp123&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am looking for how to find two different variables but only if they exist within the same group.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Do you mean you want two &lt;FONT color="#FF0000"&gt;observations&amp;nbsp;&lt;FONT color="#333333"&gt;if two different values of CODE exists in the same group?&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#333333"&gt;For example I want a list of groups where both A and B exist together. In the example below, that would be Group 1, Group 3, and Group 4.&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#333333"&gt;&lt;SPAN&gt;I don't see how Group 1 meets this criterion.&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;FONT color="#333333"&gt;&lt;SPAN&gt;&lt;BR /&gt;Show us the desired output&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Aug 2022 18:05:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829093#M327546</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-17T18:05:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to find two variables that exist in the same group only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829098#M327549</link>
      <description>Hi Paige,&lt;BR /&gt;&lt;BR /&gt;Thanks, you are correct. I mean observations, and my desired output would be Group 3, and Group 4, have both A and B in the same group (not group 1).</description>
      <pubDate>Wed, 17 Aug 2022 18:26:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829098#M327549</guid>
      <dc:creator>sashelp123</dc:creator>
      <dc:date>2022-08-17T18:26:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to find two variables that exist in the same group only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829102#M327551</link>
      <description>&lt;P&gt;So is that the entire question, A and B in the same group?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or is the question really like this:&lt;/P&gt;
&lt;P&gt;What groups have both A and B; and what groups have both A and C, and so on?&lt;/P&gt;</description>
      <pubDate>Wed, 17 Aug 2022 19:06:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829102#M327551</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-17T19:06:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to find two variables that exist in the same group only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829103#M327552</link>
      <description>Yep that's the entire question, how to find what groups have both A and B?</description>
      <pubDate>Wed, 17 Aug 2022 19:14:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829103#M327552</guid>
      <dc:creator>sashelp123</dc:creator>
      <dc:date>2022-08-17T19:14:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to find two variables that exist in the same group only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829107#M327555</link>
      <description>&lt;P&gt;UNTESTED CODE&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
    by group;
run;
proc transpose data=have out=have_t;
    by group;
    id code;
run;
data want;
    set have_t;
    if whichc('A',of col:)&amp;gt;0 and whichc('B',of col:)&amp;gt;0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want tested code, please provide the data as SAS data step code (&lt;A href="https://blogs.sas.com/content/sastraining/2016/03/11/jedi-sas-tricks-data-to-data-step-macro/" target="_self"&gt;instructions&lt;/A&gt;) and not as screen captures.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Aug 2022 19:34:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829107#M327555</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-08-17T19:34:40Z</dc:date>
    </item>
    <item>
      <title>Re: How to find two variables that exist in the same group only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829149#M327572</link>
      <description>&lt;P&gt;Untested:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have out=sorted nodupkey;
   by group code;
run;

data want;
   set sorted;
   by group;
   
   if not first.group and code = 'B' and lag(code) = 'A';
   
   keep group;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;EDIT: Simplified the code.&amp;nbsp; &lt;/P&gt;</description>
      <pubDate>Thu, 18 Aug 2022 04:58:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829149#M327572</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2022-08-18T04:58:58Z</dc:date>
    </item>
    <item>
      <title>Re: How to find two variables that exist in the same group only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829153#M327575</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have;
by group;
run;

data want;
do until (last.group);
  set have;
  by group;
  if code = "A" then _a = 1;
  if code = "B" then _b = 1;
end;
if _a and _b;
keep group;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Aug 2022 06:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829153#M327575</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2022-08-18T06:52:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to find two variables that exist in the same group only</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829184#M327587</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Group	Code $;
cards;
1	C
2	B
3	B
4	B
5	C
3	A
1	B
4	A
5	B
1	B
;

proc sql;
create table want as
select * from have
 group by group
  having sum(code='A') ne 0 and sum(code='B') ne 0;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 18 Aug 2022 12:33:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-find-two-variables-that-exist-in-the-same-group-only/m-p/829184#M327587</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-08-18T12:33:58Z</dc:date>
    </item>
  </channel>
</rss>

