<?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: Finding values above a threshold multiple times within a date range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528584#M144289</link>
    <description>&lt;P&gt;Keeping track of the cumulative count of records with value&amp;gt;=16 for each ID is the straightforward part.&amp;nbsp; But every time you encounter such a record, you must compare the current visit_date to the 3rd prior such visit_date, as well as seeing if N16&amp;gt;=4.&amp;nbsp; You can do this with the lag3(visit_date) function, as long as you only execute that function when you have a qualifying record in hand:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input ID      visit_date     value ;
datalines;
1        100              5
1        220              16
1        330              22
1        700              12
1        915              5
1        1120            2
1        1200            17
1        1300            27
1        1500            30
1        1700            35
1        1922            5
2        124              17
2        450              18
2        555              2
2        740              29
2        1000            23
run;

data want (drop=n16);
  set have;
  by id;

  if first.id then n16=0;
  if value&amp;gt;=16 then do;
    n16+1;
    if n16&amp;gt;=4 and lag3(visit_date)&amp;gt;=visit_date-730 then flag=1;
    if flag=1 then n16=-1000000;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The "by id" statement, in tandem with the "set have",&amp;nbsp;generates temporary dummy variables &lt;EM&gt;&lt;STRONG&gt;first.id&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;last.id&lt;/STRONG&gt;&lt;/EM&gt; indicating whether the observation in hand is the first or last one for the current ID value.&amp;nbsp; This provides a way the reset the N16 counter (N of records with value&amp;gt;=16) to zero when starting a new id group.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Then, only when the current record is 16 or more:
&lt;OL&gt;
&lt;LI&gt;Increment N16&lt;/LI&gt;
&lt;LI&gt;if both N16&amp;gt;=4&amp;nbsp; (4 or more for an ID) and the 3rd prior visit date is within 730 days, set flag=1.&lt;/LI&gt;
&lt;LI&gt;But once flag=1 reset N16 to an impossibly negative value, so it will never again attain a value of 4 or more for the current id.&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;The&amp;nbsp;tests of &lt;EM&gt;&lt;STRONG&gt;N16&amp;gt;=4&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;lag3(visit_date)&amp;gt;=visit_date-730&lt;/STRONG&gt;&lt;/EM&gt;, must be inside the do-group.&amp;nbsp; That is because the lag3 function is not a "look back" like in excel. It is a 3-deep queue updater where the queue contents are retained across observations.&amp;nbsp; And the queue must be updated only when you have value&amp;gt;=16.&lt;/LI&gt;
&lt;/OL&gt;</description>
    <pubDate>Sun, 20 Jan 2019 00:53:14 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2019-01-20T00:53:14Z</dc:date>
    <item>
      <title>Finding values above a threshold multiple times within a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528578#M144286</link>
      <description>&lt;P&gt;Hi!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am&amp;nbsp;using a long dataset that is set up as shown below. I am trying to find the FIRST instance&amp;nbsp;where the "value" variable is &amp;gt;=16 at least 4 times within a given 730-day period. These 4 occurences of value&amp;gt;=16 do not necessarily need to be consecutive, just occuring within 730 days of each other. I would also like to generate another variable that is the date of the 4th occurence of value&amp;gt;=16. The visit_date variable is in the form of days after a certain date that is 0, so they are formatted as continuous numbers.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the example below I would like it to identify for id=1 that visit_date=1700 is the 4th visit within a 730-day period where value&amp;gt;=16. And I'd like the&amp;nbsp;visit_date of 1700 for this person to be output.&amp;nbsp;For id=2 it would NOT flag anything since there are 4 instances of value&amp;gt;=16, but these do not occur within a 730 day window (1000-124&amp;gt;730).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp;visit_date &amp;nbsp; &amp;nbsp; value&amp;nbsp;&lt;/P&gt;&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;220 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;16&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;330 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;700 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;915 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1120 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1200 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;17&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1300 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;27&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1500 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1700 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;35&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1922 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;124 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;17&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;450&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 18&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;555&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;740&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 29&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 23&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have tried using an approach of first.id but haven't been able to get anything close to what I want. I'm not sure if an array of some form may be better for this! I'm working in SAS 9.4.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks for any help!&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Jan 2019 23:17:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528578#M144286</guid>
      <dc:creator>bgosiker</dc:creator>
      <dc:date>2019-01-19T23:17:38Z</dc:date>
    </item>
    <item>
      <title>Re: Finding values above a threshold multiple times within a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528580#M144287</link>
      <description>&lt;P&gt;Can you please post what you've tried so far?&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/256379"&gt;@bgosiker&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am&amp;nbsp;using a long dataset that is set up as shown below. I am trying to find the FIRST instance&amp;nbsp;where the "value" variable is &amp;gt;=16 at least 4 times within a given 730-day period. These 4 occurences of value&amp;gt;=16 do not necessarily need to be consecutive, just occuring within 730 days of each other. I would also like to generate another variable that is the date of the 4th occurence of value&amp;gt;=16. The visit_date variable is in the form of days after a certain date that is 0, so they are formatted as continuous numbers.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the example below I would like it to identify for id=1 that visit_date=1700 is the 4th visit within a 730-day period where value&amp;gt;=16. And I'd like the&amp;nbsp;visit_date of 1700 for this person to be output.&amp;nbsp;For id=2 it would NOT flag anything since there are 4 instances of value&amp;gt;=16, but these do not occur within a 730 day window (1000-124&amp;gt;730).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp;visit_date &amp;nbsp; &amp;nbsp; value&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;100 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;220 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;16&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;330 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;22&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;700 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;915 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1120 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1200 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;17&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1300 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;27&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1500 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;30&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1700 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;35&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1922 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;124 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;17&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;450&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 18&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;555&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;740&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 29&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;1000&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 23&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;I have tried using an approach of first.id but haven't been able to get anything close to what I want. I'm not sure if an array of some form may be better for this! I'm working in SAS 9.4.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Thanks for any help!&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 19 Jan 2019 23:27:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528580#M144287</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-01-19T23:27:34Z</dc:date>
    </item>
    <item>
      <title>Re: Finding values above a threshold multiple times within a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528582#M144288</link>
      <description>&lt;P&gt;I've tried first creating an indicator for when value&amp;gt;=16 and then trying to do a count of that variable but haven't been able to finish the data step to identify the 4 consecutive value&amp;gt;=16 within the period I want.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data visit1; set visit; 
     if value&amp;gt;=16 then value_gt=1; 
     if value&amp;lt;16 then value_gt=0; 
run; 

proc sort data=work.visit1; 
    by id visit_date value; 
run; 

data visit1; set visit1; 
    by id visit_date value; 
    if first.value then value_count=1
        value_count + value_gt; 
   
   

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 19 Jan 2019 23:51:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528582#M144288</guid>
      <dc:creator>bgosiker</dc:creator>
      <dc:date>2019-01-19T23:51:05Z</dc:date>
    </item>
    <item>
      <title>Re: Finding values above a threshold multiple times within a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528584#M144289</link>
      <description>&lt;P&gt;Keeping track of the cumulative count of records with value&amp;gt;=16 for each ID is the straightforward part.&amp;nbsp; But every time you encounter such a record, you must compare the current visit_date to the 3rd prior such visit_date, as well as seeing if N16&amp;gt;=4.&amp;nbsp; You can do this with the lag3(visit_date) function, as long as you only execute that function when you have a qualifying record in hand:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have; 
input ID      visit_date     value ;
datalines;
1        100              5
1        220              16
1        330              22
1        700              12
1        915              5
1        1120            2
1        1200            17
1        1300            27
1        1500            30
1        1700            35
1        1922            5
2        124              17
2        450              18
2        555              2
2        740              29
2        1000            23
run;

data want (drop=n16);
  set have;
  by id;

  if first.id then n16=0;
  if value&amp;gt;=16 then do;
    n16+1;
    if n16&amp;gt;=4 and lag3(visit_date)&amp;gt;=visit_date-730 then flag=1;
    if flag=1 then n16=-1000000;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;The "by id" statement, in tandem with the "set have",&amp;nbsp;generates temporary dummy variables &lt;EM&gt;&lt;STRONG&gt;first.id&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;last.id&lt;/STRONG&gt;&lt;/EM&gt; indicating whether the observation in hand is the first or last one for the current ID value.&amp;nbsp; This provides a way the reset the N16 counter (N of records with value&amp;gt;=16) to zero when starting a new id group.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Then, only when the current record is 16 or more:
&lt;OL&gt;
&lt;LI&gt;Increment N16&lt;/LI&gt;
&lt;LI&gt;if both N16&amp;gt;=4&amp;nbsp; (4 or more for an ID) and the 3rd prior visit date is within 730 days, set flag=1.&lt;/LI&gt;
&lt;LI&gt;But once flag=1 reset N16 to an impossibly negative value, so it will never again attain a value of 4 or more for the current id.&lt;/LI&gt;
&lt;/OL&gt;
&lt;/LI&gt;
&lt;LI&gt;The&amp;nbsp;tests of &lt;EM&gt;&lt;STRONG&gt;N16&amp;gt;=4&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt; and &lt;EM&gt;&lt;STRONG&gt;lag3(visit_date)&amp;gt;=visit_date-730&lt;/STRONG&gt;&lt;/EM&gt;, must be inside the do-group.&amp;nbsp; That is because the lag3 function is not a "look back" like in excel. It is a 3-deep queue updater where the queue contents are retained across observations.&amp;nbsp; And the queue must be updated only when you have value&amp;gt;=16.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Sun, 20 Jan 2019 00:53:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528584#M144289</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-20T00:53:14Z</dc:date>
    </item>
    <item>
      <title>Re: Finding values above a threshold multiple times within a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528585#M144290</link>
      <description>&lt;P&gt;Here is one way.&amp;nbsp; Note that it will output all observations where there are four or more large values within 730 days.&lt;/P&gt;
&lt;P&gt;It is interesting because it actually calls the LAG3() function conditionally so that it only lags the visit dates for the days with high values.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
 set have ;
 by id visit_date;
 row+1;
 if first.id then n=0;
 if value &amp;gt;= 16 then do;
   n+1;
   lag_date = lag3(visit_date);
   if n=4 then do;
     if lag_date + 730 &amp;gt;= visit_date then output;
     else n=3;
   end;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 20 Jan 2019 01:01:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528585#M144290</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-01-20T01:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: Finding values above a threshold multiple times within a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528748#M144343</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another possibility is to use a DoW loop (with a counter), a WHERE clause, and the DIF function instead of the LAG function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  do _N_=1 by 1 until(last.id);
    set have;
    where value&amp;gt;=16;
    by id;
    if dif3(visit_date)&amp;lt;=730 and _N_&amp;gt;=4 then
      output;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Jan 2019 13:04:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528748#M144343</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2019-01-21T13:04:14Z</dc:date>
    </item>
    <item>
      <title>Re: Finding values above a threshold multiple times within a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528823#M144366</link>
      <description>&lt;P&gt;But this logic generates ALL qualifying events.&amp;nbsp; I believe the OP only wanted the first such event.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Jan 2019 17:16:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/528823#M144366</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-01-21T17:16:06Z</dc:date>
    </item>
    <item>
      <title>Re: Finding values above a threshold multiple times within a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/529271#M144567</link>
      <description>This looks like it did the trick! I really appreciate it!</description>
      <pubDate>Tue, 22 Jan 2019 21:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Finding-values-above-a-threshold-multiple-times-within-a-date/m-p/529271#M144567</guid>
      <dc:creator>bgosiker</dc:creator>
      <dc:date>2019-01-22T21:25:34Z</dc:date>
    </item>
  </channel>
</rss>

