<?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: consecutive records filter in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262857#M51406</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thanks for the clarification.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;(...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; What if (in cases with &amp;gt;=6 obs. for one PTID, if any) two blocks of observations satisfy the selection criterion (e.g. 10, 10, 20, 10, 10, 20)?&lt;BR /&gt;&lt;STRONG&gt;no, this is not the selection criterion. We should pull only those recorsd which have first two consecutive res&amp;lt;20 followed by two consecutive res &amp;gt;=20&lt;/STRONG&gt; &lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ok, then I guess there is little potential for two series within the same patient's data satisfying the criterion (like 10, 10, 20 ,20, 10, 10, 20, 20).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt; wrote:&lt;HR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; How would missing values of RES or skipped VIS numbers be handled?&lt;BR /&gt;&lt;STRONG&gt;Missing records and vis will remain missing&lt;/STRONG&gt;. &lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is, the target pattern must not be interrupted by an observation with missing RES? Example: "10, 10, 41, &lt;STRONG&gt;.&lt;/STRONG&gt;, 43, 44" would not be selected?&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 11 Apr 2016 10:14:45 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2016-04-11T10:14:45Z</dc:date>
    <item>
      <title>consecutive records filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262817#M51390</link>
      <description>&lt;P&gt;Dear Experts,&lt;/P&gt;
&lt;P&gt;I need to extract records with two consecutive &amp;lt;20 followed by &amp;gt;=20 res; in this case we get &lt;BR /&gt;&lt;BR /&gt;ptid vis res&lt;BR /&gt;2 2 10&lt;BR /&gt;2 3 10&lt;BR /&gt;2 4 50&lt;BR /&gt;2 5 60&lt;BR /&gt;4 1 10&lt;BR /&gt;4 2 10&lt;BR /&gt;4 3 20&lt;BR /&gt;4 4 40&lt;BR /&gt;4 5 40&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data is as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ptid vis res;
cards;
1 1 10 
1 2 20
1 3 20
1 4 50
2 1 20
2 2 10
2 3 10
2 4 50
2 5 60
3 1 10
3 2 20
3 3 20
3 4 50
4 1 10
4 2 10
4 3 20
4 4 40
4 5 40
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;could you please provide your inputs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2016 06:59:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262817#M51390</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-04-11T06:59:34Z</dc:date>
    </item>
    <item>
      <title>Re: consecutive records filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262819#M51391</link>
      <description>&lt;P&gt;Sort the dataset in reverse order and then use the lagX() functions for obtaining the data from previous observations. Then re-sort back into the original order.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2016 07:25:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262819#M51391</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-04-11T07:25:48Z</dc:date>
    </item>
    <item>
      <title>Re: consecutive records filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262833#M51395</link>
      <description>&lt;P&gt;Hi Jag,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I had to program it, I would need more complete specifications:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;What should be the first selected observation per PTID? The first of the two with RES&amp;lt;20 or the first of a (possibly longer) series of observations with RES&amp;lt;20?&lt;/LI&gt;
&lt;LI&gt;What should be the last selected observation per PTID? The last of a series of observations with RES&amp;gt;=20 (following the two with RES&amp;lt;20) or the last observation of the patient?&lt;/LI&gt;
&lt;LI&gt;What if (in cases with &amp;gt;=6 obs. for one PTID, if any) two blocks of observations satisfy the selection criterion (e.g. 10, 10, 20, 10, 10, 20)?&lt;/LI&gt;
&lt;LI&gt;How would missing values of RES or skipped VIS numbers be handled?&lt;/LI&gt;
&lt;LI&gt;What if repeated measurements (i.e. consecutive records with the same VIS) or unscheduled visits contributed to the target pattern?&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2016 08:38:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262833#M51395</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-11T08:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: consecutive records filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262845#M51400</link>
      <description>&lt;P&gt;@FreelanceReinhard,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Agree with your questions, here are my responses. Hope this helps. &lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; What should be the first selected observation per PTID? The first of the two with RES&amp;lt;20 or the first of a (possibly longer) series of observations with RES&amp;lt;20?&lt;BR /&gt;&lt;STRONG&gt;the firstshould be res &amp;lt; 20 and the first of the two with res&amp;lt;20&lt;/STRONG&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; What should be the last selected observation per PTID? The last of a series of observations with RES&amp;gt;=20 (following the two with RES&amp;lt;20) or the last observation of the patient?&lt;BR /&gt;&lt;STRONG&gt;the last selected observation should be res &amp;gt;=20. The last select observations should be res &amp;gt;=20.&lt;/STRONG&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; What if (in cases with &amp;gt;=6 obs. for one PTID, if any) two blocks of observations satisfy the selection criterion (e.g. 10, 10, 20, 10, 10, 20)?&lt;BR /&gt;&lt;STRONG&gt;no, this is not the selection criterion. We should pull only those recorsd which have first two consecutive res&amp;lt;20 followed by two consecutive res &amp;gt;=20&lt;/STRONG&gt; &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; How would missing values of RES or skipped VIS numbers be handled?&lt;BR /&gt;&lt;STRONG&gt;Missing records and vis will remain missing&lt;/STRONG&gt;. &lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; What if repeated measurements (i.e. consecutive records with the same VIS) or unscheduled visits contributed to the target pattern?&lt;BR /&gt;&lt;STRONG&gt;we only require consecutive res&amp;lt;20 followed consecutive res&amp;gt;=20 and it could include records with same vis or even unscheduled visits or even missing vis.&lt;/STRONG&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2016 09:22:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262845#M51400</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-04-11T09:22:47Z</dc:date>
    </item>
    <item>
      <title>Re: consecutive records filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262857#M51406</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Thanks for the clarification.&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;(...)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; What if (in cases with &amp;gt;=6 obs. for one PTID, if any) two blocks of observations satisfy the selection criterion (e.g. 10, 10, 20, 10, 10, 20)?&lt;BR /&gt;&lt;STRONG&gt;no, this is not the selection criterion. We should pull only those recorsd which have first two consecutive res&amp;lt;20 followed by two consecutive res &amp;gt;=20&lt;/STRONG&gt; &lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Ok, then I guess there is little potential for two series within the same patient's data satisfying the criterion (like 10, 10, 20 ,20, 10, 10, 20, 20).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/12151"&gt;@Jagadishkatam&lt;/a&gt; wrote:&lt;HR /&gt;&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; How would missing values of RES or skipped VIS numbers be handled?&lt;BR /&gt;&lt;STRONG&gt;Missing records and vis will remain missing&lt;/STRONG&gt;. &lt;/BLOCKQUOTE&gt;
&lt;P&gt;That is, the target pattern must not be interrupted by an observation with missing RES? Example: "10, 10, 41, &lt;STRONG&gt;.&lt;/STRONG&gt;, 43, 44" would not be selected?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 11 Apr 2016 10:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262857#M51406</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-11T10:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: consecutive records filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262866#M51412</link>
      <description>Yes, i agree with you.</description>
      <pubDate>Mon, 11 Apr 2016 10:51:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262866#M51412</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-04-11T10:51:54Z</dc:date>
    </item>
    <item>
      <title>Re: consecutive records filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262870#M51413</link>
      <description>&lt;P&gt;Good. Then please try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Select max. number of observations per patient */

proc sql noprint;
select max(c) into :maxobs
from (select count(*) as c
      from have
      group by ptid);
quit;

/* Select observations matching the pattern */

data want;
length resseq $&amp;amp;maxobs;
retain patternID;
if _n_=1 then patternID=prxparse('/1{2}?2{2,}/');
do until(last.ptid);
  set have;
  by ptid;
  resseq=cats(resseq,n(res)*(2-(res&amp;lt;20)));
end;
call prxsubstr(patternID, resseq, start, len);
do until(last.ptid);
  set have;
  by ptid;
  ctr=sum(ctr,1);
  if start&amp;lt;=ctr&amp;lt;start+len then output;
end;
drop patternID resseq start len ctr;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;In the first DOW loop a pattern of 1s and 2s and 0s representing observations with nonmissing RES&amp;lt;20, RES&amp;gt;=20 and missing RES, respectively, is written to character variable RESSEQ ("result sequence"). The Perl regular expression matches "exactly two 1s followed by two or more 2s" in RESSEQ.&amp;nbsp;&lt;SPAN&gt;In the second DOW loop, observations are written to WANT if and only if their observation number within the BY group (CTR) is between the START and end (=START+LEN-1) position of the matched pattern (if any).&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;If there is a risk of two or more matched patterns for a patient, the code needs to be amended: Using the CALL PRXNEXT routine (instead of PRXSUBSTR) one could loop through the multiple matches.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Edit: Here is the enhanced version of the data step which allows for multiple matches for the same patient.&lt;/SPAN&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
length resseq $&amp;amp;maxobs;
if _n_=1 then patternID+prxparse('/1{2}?2{2,}/');
do until(last.ptid);
  set have;
  by ptid;
  resseq=cats(resseq,n(res)*(2-(res&amp;lt;20)));
end;
start=1;
call prxnext(patternID, start, -1, resseq, pos, len);
do until(last.ptid);
  set have;
  by ptid;
  ctr=sum(ctr,1);
  if pos&amp;lt;=ctr&amp;lt;pos+len then output;
  if ~last.ptid &amp;amp; ctr=pos+len-1 then call prxnext(patternID, start, -1, resseq, pos, len);
end;
drop patternID resseq start pos len ctr;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 11 Apr 2016 12:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262870#M51413</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-04-11T12:20:04Z</dc:date>
    </item>
    <item>
      <title>Re: consecutive records filter</title>
      <link>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262927#M51433</link>
      <description>@FreelanceReinhard, indeed your solution is very good and helpful.&lt;BR /&gt;</description>
      <pubDate>Mon, 11 Apr 2016 14:53:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/consecutive-records-filter/m-p/262927#M51433</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-04-11T14:53:22Z</dc:date>
    </item>
  </channel>
</rss>

