<?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: keeping matched observations in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/keeping-matched-observations/m-p/650152#M194974</link>
    <description>&lt;PRE&gt;DATA HAVE;
INPUT patientid drug  class $ matchto;
datalines;
1  1  a   4
2  1  a   5
3  1   b  6
4  2   .   1
5  2  .    2
6  2  .    3
;
run; 
data temp;
 set have;
 by drug;
 if first.drug then n=0;
 n+1;
run;
proc sql;
create table want as
select * from temp
 where n in (select distinct n from temp where drug=1 and class='a');
quit;&lt;/PRE&gt;</description>
    <pubDate>Sun, 24 May 2020 11:04:57 GMT</pubDate>
    <dc:creator>Ksharp</dc:creator>
    <dc:date>2020-05-24T11:04:57Z</dc:date>
    <item>
      <title>keeping matched observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keeping-matched-observations/m-p/650122#M194961</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
INPUT patientid drug  class matchto;
datalines;
1  1  a   4
2  1  a   5
3  1   b  6
4  2   .   1
5  2  .    2
6  2  .    3
;
run; &lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I am trying to create a data subset where drug=1 and class =a but I want to keep the matching observations from drug=2&lt;/P&gt;&lt;P&gt;output dataset&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1  1  a   4
2  1  a   5

4  2   .   1
5  2  .    2&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 23 May 2020 23:36:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keeping-matched-observations/m-p/650122#M194961</guid>
      <dc:creator>lillymaginta</dc:creator>
      <dc:date>2020-05-23T23:36:41Z</dc:date>
    </item>
    <item>
      <title>Re: keeping matched observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keeping-matched-observations/m-p/650134#M194969</link>
      <description>&lt;P&gt;You will need to track the MATCHTO values in some manner for later comparison. Tracking possibilities:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;array&lt;/LI&gt;
&lt;LI&gt;hash&lt;/LI&gt;
&lt;LI&gt;query&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Examples (edited to test drug=2 before checking matchto):&lt;/P&gt;
&lt;PRE&gt;DATA HAVE;
INPUT patientid drug class $ matchto;
datalines;
1  1  a   4
2  1  a   5
3  1  b   6
4  2  .   1
5  2  .   2
6  2  .   3
;
&lt;BR /&gt;* array.  only works if all matchtos come after drug=1 class='a';
data want;
  array match[100] _temporary_;
  set have;

  if drug=1 and class='a' then do;
    top+1;
    match[top] = matchto;                                    /* tracking */
    output;                                                  /* output original */
  end;
&lt;BR /&gt;if drug=2 then do;
  do ix = 1 to top while (match[ix] ne patientid);
  end;

  if match[ix] eq patientid then output;                     /* output match */&lt;BR /&gt;end;&lt;BR /&gt;
  drop ix top;
run;
&lt;BR /&gt;* hash. load lookups from same as SET. does not matter where matches occur;
data want;
  if _n_ = 1 then do;
    declare hash lookup(dataset:'have(where=(drug=1 and class="a"))');
    lookup.defineKey('matchto');
    lookup.defineData('patientid');
    lookup.defineDone();                                       /* load tracking */
  end;

  set have;

  if (drug=1 and class='a') or (drug=2 and lookup.check(key:patientid) eq 0);&lt;BR /&gt;                                                                   /* implicit output, either original or match */
run;

* existential query for selecting matched rows;&lt;BR /&gt;proc sql;&lt;BR /&gt;create table want as&lt;BR /&gt;select * from have as outer&lt;BR /&gt;where drug=1 and class='a'                                   /* select original */&lt;BR /&gt;or exists ( select * from have as inner                      /* select a match via existential correlated sub-query */&lt;BR /&gt;            where inner.drug=1 and inner.class='a'&lt;BR /&gt;            and inner.matchto = outer.patientid&lt;BR /&gt;            and inner.drug = 2 &lt;BR /&gt;          )&lt;BR /&gt;;&lt;BR /&gt;&lt;BR /&gt;* another version using SQL;
proc sql;
  create table want as
  select A.* from have A where A.drug=1 and A.class='a'
  union
  select B.* from have B where 
  exists(select * from have where drug=1 and class='a' and matchto=B.patientid and B.drug=2)
  ;

&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 24 May 2020 11:19:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keeping-matched-observations/m-p/650134#M194969</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2020-05-24T11:19:43Z</dc:date>
    </item>
    <item>
      <title>Re: keeping matched observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keeping-matched-observations/m-p/650147#M194973</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/72105"&gt;@lillymaginta&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First select the wanted observations (&lt;SPAN&gt;drug=1 and class =a).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Then select observations where matchto matches observations from the first select.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Finally append:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA HAVE;
INPUT patientid drug class$ matchto;
datalines;
1  1  a   4
2  1  a   5
3  1   b  6
4  2   .   1
5  2  .    2
6  2  .    3
;
run; 

proc sql;
	create table a as select * from have
	where drug = 1 and class = 'a';
quit;
proc sql;
	create table b as select * from have
	where matchto in (select patientid from a);
quit;
data want; set a b;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 May 2020 09:31:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keeping-matched-observations/m-p/650147#M194973</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2020-05-24T09:31:11Z</dc:date>
    </item>
    <item>
      <title>Re: keeping matched observations</title>
      <link>https://communities.sas.com/t5/SAS-Programming/keeping-matched-observations/m-p/650152#M194974</link>
      <description>&lt;PRE&gt;DATA HAVE;
INPUT patientid drug  class $ matchto;
datalines;
1  1  a   4
2  1  a   5
3  1   b  6
4  2   .   1
5  2  .    2
6  2  .    3
;
run; 
data temp;
 set have;
 by drug;
 if first.drug then n=0;
 n+1;
run;
proc sql;
create table want as
select * from temp
 where n in (select distinct n from temp where drug=1 and class='a');
quit;&lt;/PRE&gt;</description>
      <pubDate>Sun, 24 May 2020 11:04:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/keeping-matched-observations/m-p/650152#M194974</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2020-05-24T11:04:57Z</dc:date>
    </item>
  </channel>
</rss>

