<?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: Choosing obs based on outcomes in a particular order based on dates in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/767813#M243464</link>
    <description>Thank you! This worked, although I don't understand the meaning behind each line, starting with the do loop. Would like an explanation of each line if you have time.</description>
    <pubDate>Wed, 15 Sep 2021 01:48:06 GMT</pubDate>
    <dc:creator>vegan_renegade</dc:creator>
    <dc:date>2021-09-15T01:48:06Z</dc:date>
    <item>
      <title>Choosing obs based on outcomes in a particular order based on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/766725#M243012</link>
      <description>&lt;P&gt;I'm working with a dataset with multiple obs per patient (multiple test dates and results). I want to isolate any patient that has had a positive test, then later a negative, then later a positive again among all tests done. It doesn't have to be in this particular order- for example it's OK if patient had a positive, then 3 negatives, then a positive. Or any other possible combination of results...as long as at any time he had a positive, then later a negative, then again a positive. In the example below, patients 1 and 2 should be isolated as they met the criteria. Patient 3 did not as he never got&amp;nbsp; a second positive after the last negative. Thank you!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;DATA have;
input patient_id collection_date : $15. test_result : $15.;
datalines;
1 05/18/2015 Positive
1 05/21/2015 Negative
1 08/24/2016 Positive
1 11/01/2016 Negative
1 04/11/2018 Positive
2 01/02/2015 Negative
2 01/10/2016 Positive
2 02/06/2016 Negative
2 03/16/2016 Positive
3 05/21/2016 Positive
3 05/27/2016 Positive
3 01/07/2017 Negative
;
RUN;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 08 Sep 2021 22:55:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/766725#M243012</guid>
      <dc:creator>vegan_renegade</dc:creator>
      <dc:date>2021-09-08T22:55:05Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing obs based on outcomes in a particular order based on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/766732#M243014</link>
      <description>&lt;P&gt;I think collection_date is easier to handle with the date type.&lt;BR /&gt;(In the following, I've created another date variable.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data next;
  set have;
  coldt=input(collection_date,mmddyy10.);
run;

proc sort data=next;
  by patient_id coldt;
run;
data want;
  set next;
  by patient_id coldt;
  retain flgp flgn flgo;
  if first.patient_id then do;
    flgp=0;
    flgn=0;
    flgo=0;
  end;
       if flgn=0 and test_result='Positive' then flgp=1;
  else if flgp=1 and test_result='Negative' then flgn=1;
  else if flgn=1 and test_result='Positive' then flgo=1;
  /* Enable "and test_result='Positive'" if you want to exclude patients who meet the criteria but test negative again. */
  if flgo=1 /*and test_result='Positive'*/ and last.patient_id then output;
  drop coldt flg:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 09 Sep 2021 00:19:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/766732#M243014</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-09-09T00:19:53Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing obs based on outcomes in a particular order based on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/767813#M243464</link>
      <description>Thank you! This worked, although I don't understand the meaning behind each line, starting with the do loop. Would like an explanation of each line if you have time.</description>
      <pubDate>Wed, 15 Sep 2021 01:48:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/767813#M243464</guid>
      <dc:creator>vegan_renegade</dc:creator>
      <dc:date>2021-09-15T01:48:06Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing obs based on outcomes in a particular order based on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/767815#M243466</link>
      <description>&lt;P&gt;How about this?&lt;BR /&gt;I put as many comments as I could in each line.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data next;
  set have;
  coldt=input(collection_date,mmddyy10.);/* Create a date variable for sorting. */
run;

proc sort data=next;
  by patient_id coldt;/* Sort by patient_id and date */
run;
data want;
  set next;
  by patient_id coldt;
  retain flgp flgn flgo;/* Specify the flag variables flgp, flgn, and flgo with retain so that they remain after the next obs is read. */
  if first.patient_id then do;/* Initialize the flag variable at the first obs of the patient */
    flgp=0;/* 1 If Positive */
    flgn=0;/* 1 if Negative */
    flgo=0;/* 1 if output target  */
  end;
       if flgn=0 and test_result='Positive' then flgp=1;/* Set the flag "flgp" when a Positive is detected. */
  else if flgp=1 and test_result='Negative' then flgn=1;/* Set the flag "flgn" when a Negative is detected after a Positive. */
  else if flgn=1 and test_result='Positive' then flgo=1;/* Set the flag "flgo" if there is a Positive after a Negative */
  /* Enable "and test_result='Positive'" if you want to exclude patients who meet the criteria but test negative again. */
  if flgo=1 /*and test_result='Positive'*/ and last.patient_id then output;/* If the patient is an output target (flgo=1), output the last obs of the patient */
  drop coldt flg:;/* Delete the work variables */
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 15 Sep 2021 02:08:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/767815#M243466</guid>
      <dc:creator>japelin</dc:creator>
      <dc:date>2021-09-15T02:08:10Z</dc:date>
    </item>
    <item>
      <title>Re: Choosing obs based on outcomes in a particular order based on dates</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/768015#M243557</link>
      <description>Yes this is perfect, thank you!</description>
      <pubDate>Thu, 16 Sep 2021 01:23:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Choosing-obs-based-on-outcomes-in-a-particular-order-based-on/m-p/768015#M243557</guid>
      <dc:creator>vegan_renegade</dc:creator>
      <dc:date>2021-09-16T01:23:20Z</dc:date>
    </item>
  </channel>
</rss>

