<?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: Subsetting by selecting rows of multiple observation data based on two selection criteria in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Subsetting-by-selecting-rows-of-multiple-observation-data-based/m-p/798438#M313884</link>
    <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;. I found the second method you gave to be very useful.</description>
    <pubDate>Thu, 24 Feb 2022 18:39:55 GMT</pubDate>
    <dc:creator>Manhort</dc:creator>
    <dc:date>2022-02-24T18:39:55Z</dc:date>
    <item>
      <title>Subsetting by selecting rows of multiple observation data based on two selection criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subsetting-by-selecting-rows-of-multiple-observation-data-based/m-p/798308#M313839</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a large dataset. Individuals can appear on more than 1 row based on an ID. I need to subset the data to have individuals that are given both treatment A and B.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Example data:&lt;BR /&gt;ID treatment&lt;BR /&gt;1 A&lt;BR /&gt;1 B&lt;BR /&gt;1 A&lt;BR /&gt;2 A&lt;BR /&gt;2 A&lt;BR /&gt;3 B&lt;BR /&gt;3 B&lt;BR /&gt;3 B&lt;BR /&gt;3 B&lt;BR /&gt;4 A&lt;BR /&gt;4 B&lt;BR /&gt;5 A&lt;BR /&gt;5 A&lt;BR /&gt;6 B&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Desired Result:&lt;/P&gt;&lt;P&gt;ID treatment&lt;BR /&gt;1 A&lt;BR /&gt;1 B&lt;BR /&gt;1 A&lt;BR /&gt;4 A&lt;BR /&gt;4 B&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help is appreciated.&amp;nbsp;&lt;BR /&gt;Here's the input version.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;DATA have;&lt;BR /&gt;INPUT id treatment $;&lt;BR /&gt;CARDS;&lt;BR /&gt;1 A&lt;BR /&gt;1 B&lt;BR /&gt;1 A&lt;BR /&gt;2 A&lt;BR /&gt;2 A&lt;BR /&gt;3 B&lt;BR /&gt;3 B&lt;BR /&gt;3 B&lt;BR /&gt;3 B&lt;BR /&gt;4 A&lt;BR /&gt;4 B&lt;BR /&gt;5 A&lt;BR /&gt;5 A&lt;BR /&gt;6 B&lt;BR /&gt;;&lt;BR /&gt;RUN;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 24 Feb 2022 05:16:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subsetting-by-selecting-rows-of-multiple-observation-data-based/m-p/798308#M313839</guid>
      <dc:creator>Manhort</dc:creator>
      <dc:date>2022-02-24T05:16:17Z</dc:date>
    </item>
    <item>
      <title>Re: Subsetting by selecting rows of multiple observation data based on two selection criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subsetting-by-selecting-rows-of-multiple-observation-data-based/m-p/798320#M313840</link>
      <description>&lt;P&gt;You could ask PROC SQL to do exactly what you just described.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.* from have a
where exists (select id from have b where a.id=b.id and b.treatment='A')
  and exists (select id from have c where a.id=c.id and c.treatment='B')
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if there are just two treatments perhaps it is easier to use GROUP BY instead?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want2 as
select * from have
group by id
having count(distinct treatment)=2
;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But if your dataset is really large (and already sorted by ID) then a data step will be faster.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want3;
  merge have(in=in1)
        have(in=in2 keep=id treatment rename=(treatment=trtA) where=(trtA='A'))
        have(in=in3 keep=id treatment rename=(treatment=trtB) where=(trtB='B'))
 ;
 by id;
 if first.id then selected = (in1 and in2 and in3);
 retain selected ;
 if in1 and selected then output;
 drop trtA trtB selected;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or perhaps a data step with a double DOW loop?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want4;
  do until (last.id);
    set have;
    by id;
    A=sum(A,treatment='A');    
    B=sum(B,treatment='B');
  end;
  do until (last.id);
    set have;
    by id;
    if A and B then output;
  end;
  drop A B ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 24 Feb 2022 06:02:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subsetting-by-selecting-rows-of-multiple-observation-data-based/m-p/798320#M313840</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2022-02-24T06:02:16Z</dc:date>
    </item>
    <item>
      <title>Re: Subsetting by selecting rows of multiple observation data based on two selection criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subsetting-by-selecting-rows-of-multiple-observation-data-based/m-p/798385#M313867</link>
      <description>Very interesting approaches and all four are different than the way I would've done it. You learn something new every day. Thanks for sharing.</description>
      <pubDate>Thu, 24 Feb 2022 13:54:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subsetting-by-selecting-rows-of-multiple-observation-data-based/m-p/798385#M313867</guid>
      <dc:creator>GeorgeBonanza</dc:creator>
      <dc:date>2022-02-24T13:54:40Z</dc:date>
    </item>
    <item>
      <title>Re: Subsetting by selecting rows of multiple observation data based on two selection criteria</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subsetting-by-selecting-rows-of-multiple-observation-data-based/m-p/798438#M313884</link>
      <description>Thanks &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;. I found the second method you gave to be very useful.</description>
      <pubDate>Thu, 24 Feb 2022 18:39:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subsetting-by-selecting-rows-of-multiple-observation-data-based/m-p/798438#M313884</guid>
      <dc:creator>Manhort</dc:creator>
      <dc:date>2022-02-24T18:39:55Z</dc:date>
    </item>
  </channel>
</rss>

