<?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: Selection based on pairs of records within BY group where test occurred within defined time peri in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Selection-based-on-pairs-of-records-within-BY-group-where-test/m-p/338528#M272582</link>
    <description>&lt;P&gt;Are you saying that you want LEAD1009&amp;nbsp; 8/31/2007 output 2 times?&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Once because it is within 84 days of 7/11/2007 and has a higher pb?&lt;/LI&gt;
&lt;LI&gt;Once because it is within 84 days of 9/21/2007 and has a higher pb?&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;If so, then this works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Record_id :$8. Collected_date :mmddyy10. Pb_result Days ;
  format collected_date yymmddn8.;
datalines;
LEAD1000 9/14/2007 9 . 
LEAD1005 4/30/2007 5 . 
LEAD1005 1/22/2008 7 267 
LEAD1007 3/9/2007 6 . 
LEAD1009 7/11/2007 17 . 
LEAD1009 8/31/2007 22 51     
LEAD1009 9/21/2007 10 21 
LEAD1009 2/22/2008 15 154 
LEAD1011 1/22/2007 6 . 
LEAD1011 7/9/2007 7 168 
LEAD1011 9/17/2007 10 70     
LEAD1012 10/22/2007 8 . 
run;

data want (drop=nxt_:);
  set have ;
  by record_id;

  /* Compare to predecessor */
  if pb_result&amp;gt;lag(pb_result) and first.record_id=0 and days&amp;lt;=84 then output;

  /* ReCompare, this time to successor */
  merge have (keep=record_id)
        have (firstobs=2 keep=pb_result days rename=(pb_result=nxt_pb days=nxt_days));
  if pb_result&amp;gt;nxt_pb and last.record_id=0 and nxt_days&amp;lt;=84 then output;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notes;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Comparing to predessor is pretty straightforward, but one caveat&amp;nbsp; In the IF statement make sure that the "pb_result&amp;gt;lag(pb_result) condition precedes the other conditions.&amp;nbsp; Because all the conditions are connected by "and", sas is smart enough to stop checking once a failed condition is detected.&amp;nbsp; But since we always want the lag queue (this queue have size 1) updated, it should be the first condition.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;SAS has no lead functino.&amp;nbsp; To the compare to successor uses the MERGE statement (notice no accompanying BY statement), where the data set is merged with itself, but the second HAVE has "firstobs=2" meaning that it reads one observation ahead of the first have.&amp;nbsp; The "rename=" parameters is needed to avoid two records trying to populate the same variable.&lt;/LI&gt;
&lt;/OL&gt;</description>
    <pubDate>Mon, 06 Mar 2017 19:01:47 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-03-06T19:01:47Z</dc:date>
    <item>
      <title>Selection based on pairs of records within BY group where test occurred within defined time period</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selection-based-on-pairs-of-records-within-BY-group-where-test/m-p/338476#M272581</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I am processing blood lead tests. &amp;nbsp;Below is a sample of the data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;STRONG&gt;Record_id&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Collected_date&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Pb_result&lt;/STRONG&gt;&lt;/TD&gt;&lt;TD&gt;&lt;STRONG&gt;Days&lt;/STRONG&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&g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TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;LEAD1011&lt;/TD&gt;&lt;TD&gt;7/9/2007&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;168&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;LEAD1011&lt;/TD&gt;&lt;TD&gt;9/17/2007&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;70&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;LEAD1012&lt;/TD&gt;&lt;TD&gt;10/22/2007&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&lt;BR /&gt;The record_id is the unique id for each child in the dataset, collected_date is the date of collection/test, Pb_result is the result of the test, and Days is the number of days between tests for the same child. I have sorted the data by record_id and collected_date. I need to look for confirmatory pairs - tests where the the same child (record_id) received a blood lead test within 1 to 84 days of a previous test, and then select the test with the highest result. Record_id LEAD1009 had 2 tests within 51 days of each other and the second test, where the record for the 8/31/2007 test with Pb_result=22, should be output. The next pair would be the test performed on 8/31/2007 and 9/21/2007, where the 8/31/2007 test should again be output because it was the higher test result of the two.The third and fourth tests for LEAD1009 should not result in any output because the time between tests was greater than 84 days. If the child had only one test then I'd like to output those records to a different dataset.&lt;/P&gt;&lt;P&gt;I have attached an additional schematic of what I am trying to accomplish for further clarification.&lt;/P&gt;&lt;P&gt;I just can't seem to wrap my head around how to successfully code to achieve the results I am looking for. Thanks in advance for any help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13527i1B8C155EC60B0A48/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="SAS help example.PNG" title="SAS help example.PNG" /&gt;</description>
      <pubDate>Mon, 06 Mar 2017 15:46:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selection-based-on-pairs-of-records-within-BY-group-where-test/m-p/338476#M272581</guid>
      <dc:creator>aldreilly</dc:creator>
      <dc:date>2017-03-06T15:46:13Z</dc:date>
    </item>
    <item>
      <title>Re: Selection based on pairs of records within BY group where test occurred within defined time peri</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Selection-based-on-pairs-of-records-within-BY-group-where-test/m-p/338528#M272582</link>
      <description>&lt;P&gt;Are you saying that you want LEAD1009&amp;nbsp; 8/31/2007 output 2 times?&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Once because it is within 84 days of 7/11/2007 and has a higher pb?&lt;/LI&gt;
&lt;LI&gt;Once because it is within 84 days of 9/21/2007 and has a higher pb?&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;If so, then this works:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input Record_id :$8. Collected_date :mmddyy10. Pb_result Days ;
  format collected_date yymmddn8.;
datalines;
LEAD1000 9/14/2007 9 . 
LEAD1005 4/30/2007 5 . 
LEAD1005 1/22/2008 7 267 
LEAD1007 3/9/2007 6 . 
LEAD1009 7/11/2007 17 . 
LEAD1009 8/31/2007 22 51     
LEAD1009 9/21/2007 10 21 
LEAD1009 2/22/2008 15 154 
LEAD1011 1/22/2007 6 . 
LEAD1011 7/9/2007 7 168 
LEAD1011 9/17/2007 10 70     
LEAD1012 10/22/2007 8 . 
run;

data want (drop=nxt_:);
  set have ;
  by record_id;

  /* Compare to predecessor */
  if pb_result&amp;gt;lag(pb_result) and first.record_id=0 and days&amp;lt;=84 then output;

  /* ReCompare, this time to successor */
  merge have (keep=record_id)
        have (firstobs=2 keep=pb_result days rename=(pb_result=nxt_pb days=nxt_days));
  if pb_result&amp;gt;nxt_pb and last.record_id=0 and nxt_days&amp;lt;=84 then output;

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Notes;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Comparing to predessor is pretty straightforward, but one caveat&amp;nbsp; In the IF statement make sure that the "pb_result&amp;gt;lag(pb_result) condition precedes the other conditions.&amp;nbsp; Because all the conditions are connected by "and", sas is smart enough to stop checking once a failed condition is detected.&amp;nbsp; But since we always want the lag queue (this queue have size 1) updated, it should be the first condition.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;SAS has no lead functino.&amp;nbsp; To the compare to successor uses the MERGE statement (notice no accompanying BY statement), where the data set is merged with itself, but the second HAVE has "firstobs=2" meaning that it reads one observation ahead of the first have.&amp;nbsp; The "rename=" parameters is needed to avoid two records trying to populate the same variable.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Mon, 06 Mar 2017 19:01:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Selection-based-on-pairs-of-records-within-BY-group-where-test/m-p/338528#M272582</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-03-06T19:01:47Z</dc:date>
    </item>
  </channel>
</rss>

