<?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: I'm stuck on this puzzling requirement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540624#M149170</link>
    <description>&lt;P&gt;If the data is already sorted by ID/FROM_DT then you only need to save the most recent THRU_DT + 180 for each PI, which can be tested against the incoming FROM_DT for the same PI&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	informat FROM_DT date9. THRU_DT date9.;
	format FROM_DT date9. THRU_DT date9.;
	input ID PI FROM_DT THRU_DT;
	datalines;
165074 1699130682 14SEP2017 12NOV2017
165074 1699130682 13NOV2017 11JAN2018
165074 1255743092 27FEB2018 27APR2018
165074 1255743092 28APR2018 26JUN2018
165074 1043257041 27JUN2018 25AUG2018
176368 1720270135 20OCT2017 18DEC2017
176368 1487654356 12MAR2018 10MAY2018
176368 1720270135 11MAY2018 09JUL2018
176368 1987654359 10JUL2018 07SEP2018
run;

data want (drop=_:);
  set have;
  by id pi notsorted;
  if _n_=1 then do;
    _thru_plus_180=.;
    declare hash h ();
      h.definekey('pi');
      h.definedata('_thru_plus_180');
      h.definedone();
  end;
  if h.find()=0 and first.pi=1 and from_dt&amp;lt;=_thru_plus_180 then repeat='Y';
  else repeat=' ';
  if last.pi then h.replace(key:pi,data:thru_dt+180);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;OL&gt;
&lt;LI&gt;The hash object is keyed on the PI values, and the program assumes that no PI is attached to more than one ID.&amp;nbsp; Otherwise you have to modify the above to avoid contaminating tests for one ID with dates from a prior ID, as here:&lt;BR /&gt;change the h.definekey method to:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.definekey('ID','PI');&lt;BR /&gt;and change the h.replace method to&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.replace(key:id,key:pi,data:thru_dt+180);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;The&amp;nbsp; "SET HAVE;&amp;nbsp; BY ID PI NOTSORTED;" statements tell sas to expect the data in groups, not neccessarily sorted.&amp;nbsp; This is simply a tool to generate the dummy variables first.pi and last.pi.&amp;nbsp; So you can test date comparisons every time a PI group starts, and also update the hash object every time you are at the end of a PI group.&lt;/LI&gt;
&lt;/OL&gt;</description>
    <pubDate>Wed, 06 Mar 2019 00:34:27 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2019-03-06T00:34:27Z</dc:date>
    <item>
      <title>I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540539#M149122</link>
      <description>&lt;P&gt;This is a tough one. I don't even know if this is possible.&amp;nbsp; My requirement is to identify any instance where an ID switches from away from his PI, but later switches back to that same PI within 180 days. There can be any number of PI switches in between the original visit and the return visit.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So in the sample data below ID&amp;nbsp;165074 switches from PI 1699130682 to&amp;nbsp;1255743092 to PI&amp;nbsp;1043257041.&amp;nbsp; But he never switches back to any of the earlier visited PIs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;However, ID&amp;nbsp;176368 switches from PI&amp;nbsp;1720270135 on&amp;nbsp;THRU_DT 18DEC2017&amp;nbsp;to PI&amp;nbsp;1487654356, and later switches back to PI&amp;nbsp;1720270135 on FROM_DT 11MAY2018.&amp;nbsp; And since the difference between these two dates is within 180 days I'd want to set an indicator of Recycle_ID=1 for this ID.&amp;nbsp; The difference in days is calculated as (FROM_DT - THRU_DT of the repeat visits).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does anyone have any ideas on how to accomplish this?&amp;nbsp; I can't figure out a way to do this.&amp;nbsp; Any help would be really appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In the example data below I've noted the record that would get the Recycle_Ind set to 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;* Data sorted by ID and FROM_DT&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;PI&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FROM_DT&amp;nbsp; &amp;nbsp;THRU_DT &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;165074 1699130682 14SEP2017 12NOV2017&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;165074 1699130682 13NOV2017 11JAN2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;165074 1255743092 27FEB2018 27APR2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;165074 1255743092 28APR2018 26JUN2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;165074 1043257041 27JUN2018 25AUG2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;176368 1720270135 20OCT2017 &lt;STRONG&gt;18DEC2017&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;176368 1487654356 12MAR2018 10MAY2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;176368 1720270135&lt;/STRONG&gt; &lt;STRONG&gt;11MAY2018&lt;/STRONG&gt; 09JUL2018&amp;nbsp; &amp;lt;= RECYCLE_IND=1 (11MAY18-18DEC17) IS &amp;lt;= 180&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;176368 1987654359 10JUL2018 07SEP2018&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 11:28:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540539#M149122</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T11:28:39Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540550#M149126</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/79805"&gt;@buechler66&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is a problem where then LAG function is useful. It just marks one observation, and you will probably need some further processing to get your output.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	informat FROM_DT date9. THRU_DT date9.;
	format FROM_DT date9. THRU_DT date9.;
	input ID PI FROM_DT THRU_DT;
	datalines;
165074 1699130682 14SEP2017 12NOV2017
165074 1699130682 13NOV2017 11JAN2018
165074 1255743092 27FEB2018 27APR2018
165074 1255743092 28APR2018 26JUN2018
165074 1043257041 27JUN2018 25AUG2018
176368 1720270135 20OCT2017 18DEC2017
176368 1487654356 12MAR2018 10MAY2018
176368 1720270135 11MAY2018 09JUL2018
176368 1987654359 10JUL2018 07SEP2018
;
run;


* sort to group ID's on PI values;
proc sort data=have out=want; 
	by ID PI FROM_DT;
run;

* pick ID with recurrent PI, not concecutive, but within 180 days;
data w3 (drop=OldPI OldThru); set w1; by ID PI;
	OldPI = lag(PI);
	OldThru = lag(THRU_DT);
	if not first.PI then do;
		if OldThru &amp;lt; (FROM_DT-1) and (FROM_DT - OldThru) &amp;lt; 180 then Recycle_ID = 1;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="want.gif" style="width: 434px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27677i4BBC7162F7138F3D/image-size/large?v=v2&amp;amp;px=999" role="button" title="want.gif" alt="want.gif" /&gt;&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;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 19:40:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540550#M149126</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-03-05T19:40:10Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540615#M149164</link>
      <description>&lt;P&gt;Not obvious indeed. But here is a shot:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID PI (FROM_DT THRU_DT) (:date9.);
format FROM_DT THRU_DT yymmdd10. pi 10.;
datalines;
165074 1699130682 14SEP2017 12NOV2017
165074 1699130682 13NOV2017 11JAN2018
165074 1255743092 27FEB2018 27APR2018
165074 1255743092 28APR2018 26JUN2018
165074 1043257041 27JUN2018 25AUG2018
176368 1720270135 20OCT2017 18DEC2017
176368 1487654356 12MAR2018 10MAY2018
176368 1720270135 11MAY2018 09JUL2018
176368 1987654359 10JUL2018 07SEP2018
;

proc sql;
select 
    *,
    exists (    
        select * 
        from 
            have as b inner join
            have as c 
                on c.id = b.id and c.pi ne b.pi and c.from_dt &amp;lt; b.from_dt
        where   b.id = a.id and 
                b.pi = a.pi and 
                intck("DAY", a.thru_dt, b.from_dt) between 1 and 180 and
                c.thru_dt &amp;gt; a.thru_dt   
        ) as recycle
from have as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;                     ID          PI     FROM_DT     THRU_DT   recycle
               ------------------------------------------------------
                 165074  1699130682  2017-09-14  2017-11-12         0
                 165074  1699130682  2017-11-13  2018-01-11         0
                 165074  1255743092  2018-02-27  2018-04-27         0
                 165074  1255743092  2018-04-28  2018-06-26         0
                 165074  1043257041  2018-06-27  2018-08-25         0
                 176368  1720270135  2017-10-20  2017-12-18         1
                 176368  1487654356  2018-03-12  2018-05-10         0
                 176368  1720270135  2018-05-11  2018-07-09         0
                 176368  1987654359  2018-07-10  2018-09-07         0
&lt;/PRE&gt;
&lt;P&gt;&lt;U&gt;Would require testing with a complete range of possible cases.&lt;/U&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Mar 2019 23:10:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540615#M149164</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-03-05T23:10:21Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540624#M149170</link>
      <description>&lt;P&gt;If the data is already sorted by ID/FROM_DT then you only need to save the most recent THRU_DT + 180 for each PI, which can be tested against the incoming FROM_DT for the same PI&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	informat FROM_DT date9. THRU_DT date9.;
	format FROM_DT date9. THRU_DT date9.;
	input ID PI FROM_DT THRU_DT;
	datalines;
165074 1699130682 14SEP2017 12NOV2017
165074 1699130682 13NOV2017 11JAN2018
165074 1255743092 27FEB2018 27APR2018
165074 1255743092 28APR2018 26JUN2018
165074 1043257041 27JUN2018 25AUG2018
176368 1720270135 20OCT2017 18DEC2017
176368 1487654356 12MAR2018 10MAY2018
176368 1720270135 11MAY2018 09JUL2018
176368 1987654359 10JUL2018 07SEP2018
run;

data want (drop=_:);
  set have;
  by id pi notsorted;
  if _n_=1 then do;
    _thru_plus_180=.;
    declare hash h ();
      h.definekey('pi');
      h.definedata('_thru_plus_180');
      h.definedone();
  end;
  if h.find()=0 and first.pi=1 and from_dt&amp;lt;=_thru_plus_180 then repeat='Y';
  else repeat=' ';
  if last.pi then h.replace(key:pi,data:thru_dt+180);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;OL&gt;
&lt;LI&gt;The hash object is keyed on the PI values, and the program assumes that no PI is attached to more than one ID.&amp;nbsp; Otherwise you have to modify the above to avoid contaminating tests for one ID with dates from a prior ID, as here:&lt;BR /&gt;change the h.definekey method to:&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.definekey('ID','PI');&lt;BR /&gt;and change the h.replace method to&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; h.replace(key:id,key:pi,data:thru_dt+180);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;The&amp;nbsp; "SET HAVE;&amp;nbsp; BY ID PI NOTSORTED;" statements tell sas to expect the data in groups, not neccessarily sorted.&amp;nbsp; This is simply a tool to generate the dummy variables first.pi and last.pi.&amp;nbsp; So you can test date comparisons every time a PI group starts, and also update the hash object every time you are at the end of a PI group.&lt;/LI&gt;
&lt;/OL&gt;</description>
      <pubDate>Wed, 06 Mar 2019 00:34:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540624#M149170</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-03-06T00:34:27Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540723#M149215</link>
      <description>As i understand this requirement, you are seeking ID which return to a PI within 6mths having tried an alternative PI in those 6mths.&lt;BR /&gt;Looks like an ID might do this more than once with 2 more PI&lt;BR /&gt;1 A JAN&lt;BR /&gt;1 A FEB&lt;BR /&gt;1 B MAR&lt;BR /&gt;1 C APR&lt;BR /&gt;1 A MAY&lt;BR /&gt;1 B JUN&lt;BR /&gt;The ID=1 has returned to both A and to B between Jan and Jun but might return to C in the next 4 months.&lt;BR /&gt;What is the structure of the result required?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 06 Mar 2019 10:19:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540723#M149215</guid>
      <dc:creator>Peter_C</dc:creator>
      <dc:date>2019-03-06T10:19:56Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540726#M149216</link>
      <description>Each time an ID switches away from and then back to a previous PI within&lt;BR /&gt;180 days of the last visit, I need to mark the current record with&lt;BR /&gt;Recycle_Ind=1.  In your example 2 records would qualify.  Does this make&lt;BR /&gt;better sense?&lt;BR /&gt;&lt;BR /&gt;1 A JAN&lt;BR /&gt;1 A FEB&lt;BR /&gt;1 B MAR&lt;BR /&gt;1 C APR&lt;BR /&gt;1 A MAY RECYCLE=1&lt;BR /&gt;1 B JUN  RECYCLE=1&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 06 Mar 2019 10:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540726#M149216</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T10:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540734#M149219</link>
      <description>I will try this out today and reply back. Thank you.</description>
      <pubDate>Wed, 06 Mar 2019 11:30:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540734#M149219</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T11:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540735#M149220</link>
      <description>This also looks like it will work. I'll try this out today too. Thank you.</description>
      <pubDate>Wed, 06 Mar 2019 11:31:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540735#M149220</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T11:31:52Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540737#M149221</link>
      <description>This looks interesting, but I have no idea what it's doing. This is the first time I've ever seen this 'hash' concept. I'm afraid this is over my head.  &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;</description>
      <pubDate>Wed, 06 Mar 2019 11:34:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540737#M149221</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T11:34:23Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540811#M149235</link>
      <description>&lt;P&gt;Unfortunately this logic didn't work.&amp;nbsp; I've included new example data and you can see that it's flagging too many rows with Recycle_Ind=1 .&amp;nbsp; It has to do with this line of code:&amp;nbsp;&amp;nbsp;if &lt;STRONG&gt;OldThru &amp;lt; (FROM_DT-1)&lt;/STRONG&gt; and (FROM_DT - OldThru) &amp;lt; 180 then Recycle_ID = 1;&lt;/P&gt;
&lt;P&gt;This assumes that there will only be one day's difference between the current FROM_DT and the previous THRU_DT, when in fact there could be a larger gap. I extended the gap by more than 1 day for the records that are marked as incorrect below.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have any thoughts on how to correct this I'd love to get this working.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the input data below the following records are identified:&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;U&gt;ID&amp;nbsp; &amp;nbsp; &amp;nbsp;PI&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;FROM_DT&amp;nbsp; &amp;nbsp;THRU_DT&lt;/U&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;123456 1234567890 14SEP2017 12NOV2017&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 1234567890 15NOV2017 11JAN2018 &lt;STRONG&gt;INCORRECTLY&lt;/STRONG&gt; RECYCLE=1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 27FEB2018 27APR2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 28APR2018 26JUN2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 27JUN2018 25AUG2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 14SEP2017 12FEB2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 4345678901 20FEB2018 25FEB2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 12MAR2018 08MAY2018 RECYCLE=1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 11MAY2018 12MAY2018 &lt;STRONG&gt;INCORRECTLY&lt;/STRONG&gt; RECYCLE=1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 4345678901 13MAY2018 09JUL2018 RECYCLE=1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 10JUL2018 07SEP2018 RECYCLE=1&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;data have;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;informat FROM_DT date9. THRU_DT date9.;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;format FROM_DT date9. THRU_DT date9.;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;input ID PI FROM_DT THRU_DT;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;datalines;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 1234567890 14SEP2017 12NOV2017&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 1234567890 15NOV2017 11JAN2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 27FEB2018 27APR2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 28APR2018 26JUN2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 27JUN2018 25AUG2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 14SEP2017 12FEB2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 4345678901 20FEB2018 25FEB2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 12MAR2018 08MAY2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 11MAY2018 12MAY2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 4345678901 13MAY2018 09JUL2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 10JUL2018 07SEP2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;* sort to group ID's on PI values;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;proc sort data=have out=want; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;by ID PI FROM_DT;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;* pick ID with recurrent PI, not concecutive, but within 180 days;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;data w3 (drop=OldPI OldThru); set wANT; by ID PI;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;OldPI = lag(PI);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;OldThru = lag(THRU_DT);&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;if not first.PI then do;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;if &lt;STRONG&gt;OldThru &amp;lt; (FROM_DT-1)&lt;/STRONG&gt; and (FROM_DT - OldThru) &amp;lt; 180 then Recycle_ID = 1;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;end;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;run;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 15:46:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540811#M149235</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T15:46:01Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540814#M149236</link>
      <description>&lt;P&gt;Hi. Unfortunately this isn't working either when I tested it with this second set of test records.&amp;nbsp; Here's what I'm expecting to be flagged based on the requirement.&amp;nbsp; If you run the code below it is flagging differently.&amp;nbsp; Any suggestions based on this?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;123456 1234567890 14SEP2017 12NOV2017&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 1234567890 15NOV2017 11JAN2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 27FEB2018 27APR2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 28APR2018 26JUN2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 27JUN2018 25AUG2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 14SEP2017 12FEB2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 4345678901 20FEB2018 25FEB2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;234567 3234567890 12MAR2018 08MAY2018&amp;nbsp; &amp;nbsp; &amp;nbsp;Recycle_Ind=1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 11MAY2018 12MAY2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;234567 4345678901 13MAY2018 09JUL2018&amp;nbsp; &amp;nbsp; &amp;nbsp;Recycle_Ind=1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;234567 3234567890 10JUL2018 07SEP2018&amp;nbsp; &amp;nbsp; &amp;nbsp;Recycle_Ind=1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input ID PI (FROM_DT THRU_DT) (:date9.);&lt;BR /&gt;format FROM_DT THRU_DT yymmdd10. pi 10.;&lt;BR /&gt;datalines;&lt;BR /&gt;123456 1234567890 14SEP2017 12NOV2017&lt;BR /&gt;123456 1234567890 15NOV2017 11JAN2018&lt;BR /&gt;123456 2345678901 27FEB2018 27APR2018&lt;BR /&gt;123456 2345678901 28APR2018 26JUN2018&lt;BR /&gt;123456 2345678901 27JUN2018 25AUG2018&lt;BR /&gt;234567 3234567890 14SEP2017 12FEB2018&lt;BR /&gt;234567 4345678901 20FEB2018 25FEB2018&lt;BR /&gt;234567 3234567890 12MAR2018 08MAY2018&lt;BR /&gt;234567 3234567890 11MAY2018 12MAY2018&lt;BR /&gt;234567 4345678901 13MAY2018 09JUL2018&lt;BR /&gt;234567 3234567890 10JUL2018 07SEP2018&lt;BR /&gt;;&lt;/P&gt;
&lt;P&gt;proc sql;&lt;BR /&gt;select &lt;BR /&gt;*,&lt;BR /&gt;exists ( &lt;BR /&gt;select * &lt;BR /&gt;from &lt;BR /&gt;have as b inner join&lt;BR /&gt;have as c &lt;BR /&gt;on c.id = b.id and c.pi ne b.pi and c.from_dt &amp;lt; b.from_dt&lt;BR /&gt;where b.id = a.id and &lt;BR /&gt;b.pi = a.pi and &lt;BR /&gt;intck("DAY", a.thru_dt, b.from_dt) between 1 and 180 and&lt;BR /&gt;c.thru_dt &amp;gt; a.thru_dt &lt;BR /&gt;) as recycle&lt;BR /&gt;from have as a;&lt;BR /&gt;quit;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 15:58:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540814#M149236</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T15:58:27Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540817#M149237</link>
      <description>Wow!  This code is working even with my new set of test data that broke the prior two solution offerings.  Unfortunately the program's assumption that "no PI is attached to more than one ID".  This indeed can happen in my data so I guess this breaks the proposed solution. &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;&lt;BR /&gt;Not understanding this type of programming (eg. Hash) I can't begin to alter this program.  Is there anyway this solution can be coded in more traditional SAS datasteps?</description>
      <pubDate>Wed, 06 Mar 2019 16:09:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540817#M149237</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T16:09:02Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540833#M149239</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/79805"&gt;@buechler66&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am sorry it did'nt work, that was because I assumed that intervals were consecutive, which caused gaps between same values of PI to be marked also. The following code marks a recycle only if there has been (at least) one other PI value in between.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
informat FROM_DT date9. THRU_DT date9.;
format FROM_DT date9. THRU_DT date9.;
input ID PI FROM_DT THRU_DT;
datalines;
123456 1234567890 14SEP2017 12NOV2017
123456 1234567890 15NOV2017 11JAN2018
123456 2345678901 27FEB2018 27APR2018
123456 2345678901 28APR2018 26JUN2018
123456 2345678901 27JUN2018 25AUG2018
234567 3234567890 14SEP2017 12FEB2018
234567 4345678901 20FEB2018 25FEB2018
234567 3234567890 12MAR2018 08MAY2018
234567 3234567890 11MAY2018 12MAY2018
234567 4345678901 13MAY2018 09JUL2018
234567 3234567890 10JUL2018 07SEP2018
;
run;

* Just to make sure input is in concecutive order by ID FROM_DT;
proc sort data=have out=w1;
	by ID FROM_DT;
run;

* Add a sequence number; 
data w2; set w1;
	Seq = _N_;
run;

* sort to group ID's on PI values;
proc sort data=w2 out=w3; 
	by ID PI FROM_DT;
run;

* Pick ID with recurrent PI, not consecutive, but within 180 days;
* Use sequence number to suppress gaps between same PID's;
data w4 (drop=OldPI OldThru OldSeq Seq); set w3; by ID PI;
	OldPI = lag(PI);
	OldThru = lag(THRU_DT);
	OldSeq = lag(Seq);

	if not first.PI and Seq ne (OldSeq+1) then do;
			if OldThru &amp;lt; (FROM_DT-1) and (FROM_DT - OldThru) &amp;lt; 180 then Recycle_ID = 1;
	end;
run;

* Sort to restore input order;
proc sort data=w4 out=want;
	by ID FROM_DT;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-left" image-alt="want.gif" style="width: 439px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/27706i8827179B41CF2938/image-size/large?v=v2&amp;amp;px=999" role="button" title="want.gif" alt="want.gif" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 17:07:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540833#M149239</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-03-06T17:07:05Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540839#M149243</link>
      <description>&lt;P&gt;Sorry I have had to use a hash to get this to work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID PI (FROM_DT THRU_DT) (:date9.);
format FROM_DT THRU_DT yymmdd10. pi 10.;
datalines;
123456 1234567890 14SEP2017 12NOV2017
123456 1234567890 15NOV2017 11JAN2018
123456 2345678901 27FEB2018 27APR2018
123456 2345678901 28APR2018 26JUN2018
123456 2345678901 27JUN2018 25AUG2018
234567 3234567890 14SEP2017 12FEB2018
234567 4345678901 20FEB2018 25FEB2018
234567 3234567890 12MAR2018 08MAY2018
234567 3234567890 11MAY2018 12MAY2018
234567 4345678901 13MAY2018 09JUL2018
234567 3234567890 10JUL2018 07SEP2018
165074 1699130682 14SEP2017 12NOV2017
165074 1699130682 13NOV2017 11JAN2018
165074 1255743092 27FEB2018 27APR2018
165074 1255743092 28APR2018 26JUN2018
165074 1043257041 27JUN2018 25AUG2018
176368 1720270135 20OCT2017 18DEC2017
176368 1487654356 12MAR2018 10MAY2018
176368 1720270135 11MAY2018 09JUL2018
176368 1987654359 10JUL2018 07SEP2018
;

proc sort data = work.have
			out = work.havesrt;
by id from_dt;
run;


data work.want(drop = Save_DT Save_PI irc rc);
declare hash Prev(ordered:'a');
 prev.definekey('Save_DT');
 prev.definedata('Save_PI');
 prev.definedone();
declare hiter iprev('prev');
format 	save_pi 10.
		save_dt yymmdd10.;			

do until (eof);
	/*** Go through the information for a customer one at a time ***/
	do until (last.id);
		set work.havesrt end = eof;
		by id; * lets us look at only one customer;
		call missing(Recycle_Ind); * make sure this doesn't carry over between rows;
		irc=iprev.last(); *get the most recent information for the customer;
		/*** The most recent information must exist and must have a different PI ***/
		if irc = 0 and Save_PI ne PI then do until(irc or From_DT - Save_DT gt 180);
			if Save_PI eq PI then do; 
				Recycle_Ind = 1;
				leave;
				end;
			irc=iprev.prev();
		end;
		output;
		rc=prev.add(key:Thru_dt, data:PI);
	end;


	/*** I am still quite new to hash and haven't figured out a
	better way of clearing a hash if a hiter is pointing at it other
	than to force the hiter to not look at it and then clear it ***/
	irc=iprev.first();
	irc=iprev.prev();
	/*** Get rid of all the information in the hash to save space and mean that future
	id's aren't looking at a different id's worth of data ***/
	rc=prev.clear();
end;
stop;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Mar 2019 17:32:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540839#M149243</guid>
      <dc:creator>DanielLangley</dc:creator>
      <dc:date>2019-03-06T17:32:17Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540844#M149245</link>
      <description>Wow. This does appear to work.  I'm going to do some testing and I'll follow up.  &lt;BR /&gt;&lt;BR /&gt;May I ask, why does the code still contain the condition "OLD_THRU &amp;lt; (CLM_FROM_DT-1)"?  I'm curious why this condition is still necessary?</description>
      <pubDate>Wed, 06 Mar 2019 17:39:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540844#M149245</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T17:39:04Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540849#M149248</link>
      <description>Thanks so much for sharing. It definitely appears to work, but I just can't&lt;BR /&gt;understand it - and therefore don't think I should introduce this code into&lt;BR /&gt;our environment - since I won't be able to troubleshoot or enhance it later&lt;BR /&gt;on.&lt;BR /&gt;&lt;BR /&gt;Thanks again for trying to help. I really do appreciate your time. I wish I&lt;BR /&gt;code code like you do.&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Wed, 06 Mar 2019 17:46:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540849#M149248</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T17:46:22Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540872#M149256</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/79805"&gt;@buechler66&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I just forgot to remove it, because it does no harm. It had a purpose in my first attempt, where I thought the intervals were consecutive, so a gap meant that there was an interval with another PI in between. But the check on sequence number makes it obsolete.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 18:45:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540872#M149256</guid>
      <dc:creator>ErikLund_Jensen</dc:creator>
      <dc:date>2019-03-06T18:45:58Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540913#M149273</link>
      <description>&lt;P&gt;Please explain&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;123456 1234567890 14SEP2017 12NOV2017&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 1234567890 15NOV2017 11JAN2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 27FEB2018 27APR2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 28APR2018 26JUN2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;123456 2345678901 27JUN2018 25AUG2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 3234567890 14SEP2017 &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;12FEB2018&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 4345678901 20FEB2018 25FEB2018&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;234567 3234567890 12MAR2018 08MAY2018&amp;nbsp; &amp;nbsp; &amp;nbsp;Recycle_Ind=1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;234567 &lt;FONT color="#333333"&gt;&lt;STRONG&gt;3234567890&lt;/STRONG&gt;&lt;/FONT&gt; &lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;11MAY2018&lt;/FONT&gt;&lt;/STRONG&gt; 12MAY2018&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;FONT color="#FF0000"&gt;WHY? 11MAY2018 - 12FEB2018 &amp;lt; 180 days&lt;/FONT&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;234567 4345678901 13MAY2018 09JUL2018&amp;nbsp; &amp;nbsp; &amp;nbsp;Recycle_Ind=1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;&lt;STRONG&gt;234567 3234567890 10JUL2018 07SEP2018&amp;nbsp; &amp;nbsp; &amp;nbsp;Recycle_Ind=1&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 20:06:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540913#M149273</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2019-03-06T20:06:11Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540923#M149277</link>
      <description>&lt;P&gt;I do really recommend trying to learn hash objects as they are very flexible. This solution might be easier to understand but it does have a limit on the number of rows per ID, currently 1000 as in the array definition. You might need to check if that holds or increase the number if necessary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID PI (FROM_DT THRU_DT) (:date9.);
format FROM_DT THRU_DT yymmdd10. pi 10.;
datalines;
123456 1234567890 14SEP2017 12NOV2017
123456 1234567890 15NOV2017 11JAN2018
123456 2345678901 27FEB2018 27APR2018
123456 2345678901 28APR2018 26JUN2018
123456 2345678901 27JUN2018 25AUG2018
234567 3234567890 14SEP2017 12FEB2018
234567 4345678901 20FEB2018 25FEB2018
234567 3234567890 12MAR2018 08MAY2018
234567 3234567890 11MAY2018 12MAY2018
234567 4345678901 13MAY2018 09JUL2018
234567 3234567890 10JUL2018 07SEP2018
165074 1699130682 14SEP2017 12NOV2017
165074 1699130682 13NOV2017 11JAN2018
165074 1255743092 27FEB2018 27APR2018
165074 1255743092 28APR2018 26JUN2018
165074 1043257041 27JUN2018 25AUG2018
176368 1720270135 20OCT2017 18DEC2017
176368 1487654356 12MAR2018 10MAY2018
176368 1720270135 11MAY2018 09JUL2018
176368 1987654359 10JUL2018 07SEP2018
;

proc sort data = work.have
			out = work.havesrt;
by id from_dt;
run;

data work.want(drop=i savedpicount);
retain SavedPiCount;
set work.havesrt;
by id;

array comparison[2,1000] _temporary_;
call missing(Recycle_Ind);

/* First ID cannot be recycle so don't bother checking it just add it to the array */
if first.id then SavedPiCount = 0;
/* All other ID's must be checked against those already in the array.
First check that the current Pi is different to the previous Pi
then check if any other Pi's in the past 180 days are the same as the current Pi */
else if PI ne comparison[2,SavedPiCount] then do i = SavedPiCount to 1 by -1;
	if comparison[2,i] = PI and from_dt - comparison[1,i] lt 180 then do;
		Recycle_Ind = 1;
		leave;
		end;
end;

/* Once the logic has been checked for this Pi then add it to the array */
SavedPiCount = sum(SavedPiCount,1);
comparison[1,SavedPiCount] = Thru_dt;
comparison[2,SavedPiCount] = PI;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 06 Mar 2019 21:01:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540923#M149277</guid>
      <dc:creator>DanielLangley</dc:creator>
      <dc:date>2019-03-06T21:01:21Z</dc:date>
    </item>
    <item>
      <title>Re: I'm stuck on this puzzling requirement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540940#M149290</link>
      <description>Hi. It's because in that case the PI was visited on two consecutive visits in a row. The requirement is Recycle_Ind=1 when the ID visits different PI in between visits.  Since the ID 234567 visited PI 3234567890 two consecutive times the second record should not get marked as a recycled.  Sorry if I wasn't clear on that.  I do really appreciate your time in providing a solution.  You have helped me so often in the past.</description>
      <pubDate>Wed, 06 Mar 2019 21:50:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/I-m-stuck-on-this-puzzling-requirement/m-p/540940#M149290</guid>
      <dc:creator>buechler66</dc:creator>
      <dc:date>2019-03-06T21:50:16Z</dc:date>
    </item>
  </channel>
</rss>

