<?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: Keep based on gaps in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779221#M248147</link>
    <description>Transaction 6 is 4th row in the 'want' dataset. Transaction 7 is rolled up into the 4th row because there wasn't a gap of &amp;gt;30 days.</description>
    <pubDate>Mon, 08 Nov 2021 21:07:37 GMT</pubDate>
    <dc:creator>A_Swoosh</dc:creator>
    <dc:date>2021-11-08T21:07:37Z</dc:date>
    <item>
      <title>Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779179#M248132</link>
      <description>&lt;P&gt;I misspoke on my previous ask so I created a new posting to better characterize my question.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset below as follows:&lt;/P&gt;
&lt;PRE&gt;data have1;
  infile cards truncover expandtabs;
  input STORE_ID $ customer_id $ transaction_id $ transaction_date :date9.;
  format transaction_date date8.;
  cards;
A0001 aa 1 22jan2010  
A0001 aa 2 23jan2010  
A0001 ay 3 24jan2010  
A0001 ab 4 11mar2010  
A0001 cc 5 11jun2011  
A0001 dd 6 12jun2012  
A0001 ee 7 13jul2012  
A0001 ee 8 10aug2012  
A0001 ef 9 10aug2012  
A0001 ff 10 10aug2012  
A0001 ff 11 07dec2012  
A0001 gg 12 22dec2012  
A0001 ds 13 01jan2013  
A0002 fz 1 07dec2012  
A0002 gb 2 22dec2012  
A0002 dw 3 01jan2013  
A0003 mg 1 22dec2012  
A0003 sf 2 01jan2013  
;&lt;/PRE&gt;
&lt;P&gt;I want to accomplish two tasks:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;I want to essentially create an open and close period for each store and treat gaps of 30 days in the transaction date as a closure flagged by an indicator (closure=1 for example).&lt;BR /&gt;
&lt;UL&gt;
&lt;LI&gt;Basically, I want to create a dataset that has opened and closed based on transaction_date and gaps of &amp;gt;30 days are treated as closures.&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;I want to also generate a closure=1 if the date falls before a date (e.g., 31-DEC-2013)&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;The code below I have generated gives me relevant information but I have trouble with the first and last rows so perhaps I've gone about this incorrectly. Any suggestions would be helpful. Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have1;
  by store_id;
  difdate=ifn(first.store_id,.,dif(transaction_date));
  if first.store_id or transaction_date&amp;gt;cutoff then do;
    cutoff=transaction_date+30;
  end;
 last_transaction=lag(transaction_date);
if first.store_id then last_transaction=.;
if last.store_id then do;
if transaction_date le '31-DEC-2013'd then closure=1; 
end;
if difdate gt 30 then closure=1;
  retain cutoff;
  format cutoff last_transaction date9.;
 run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Nov 2021 18:11:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779179#M248132</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-11-08T18:11:45Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779200#M248136</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think you can adjust below code to get exactly what you want.&lt;/P&gt;
&lt;P&gt;It uses the "look ahead one observation"-trick with firstobs=2.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort data=have1;
 by STORE_ID transaction_date ;
run;

data want(drop = STORE_ID2 transaction_date2);
 set have1;
 set have1(firstobs=2 
           drop = customer_id transaction_id
           rename = (STORE_ID = STORE_ID2 transaction_date = transaction_date2) );
 if STORE_ID ^= STORE_ID2 then do; STORE_ID2=''; transaction_date2=.; end; 
 gap = INTCK('DAY',transaction_date,transaction_date2,'CONTINUOUS');
 closure = 0;
 if gap &amp;gt; 30 then closure=1;
run;
/* end of program */&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 18:50:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779200#M248136</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-11-08T18:50:09Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779202#M248137</link>
      <description>&lt;P&gt;I still don't know how to change this information to construct open/close dates using the gaps. This information provides the gap periods &amp;gt; 30 days; however, my main question still persists--how to construct open and close dates with the closure=1 periods too.&lt;/P&gt;
&lt;P&gt;I want to essentially create for each store_id, open and close dates.&lt;/P&gt;
&lt;P&gt;So, for example,&amp;nbsp; store1 has 6 rows; 4 for the gaps but also the from Jan 22-Jan 24; and again from Dec 7-Jan1 2013&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 19:16:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779202#M248137</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-11-08T19:16:59Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779203#M248138</link>
      <description>Can you post what you want your data to look like? Basically edit your `have1` data set with the CARDS statement to show us exactly what you want?</description>
      <pubDate>Mon, 08 Nov 2021 19:19:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779203#M248138</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-11-08T19:19:01Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779205#M248139</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/197542"&gt;@A_Swoosh&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I was just about to write the exact same thing as &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/281770"&gt;@maguiremq&lt;/a&gt;&amp;nbsp;.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Create a data step with &lt;FONT face="courier new,courier"&gt;data want;&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;cards (datalines);&lt;/FONT&gt; statements to show us what your &lt;EM&gt;want&lt;/EM&gt; dataset looks like which should be the result of processing your &lt;EM&gt;have&lt;/EM&gt; dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Koen&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 19:27:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779205#M248139</guid>
      <dc:creator>sbxkoenk</dc:creator>
      <dc:date>2021-11-08T19:27:40Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779209#M248141</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/281770"&gt;@maguiremq&lt;/a&gt;&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  infile cards truncover expandtabs;
  input STORE_ID $ open :date9. close :date9. closed $;
  format open close date8.;
  cards;
A0001 22jan2010 24jan2010 1
A0001 11mar2010 11mar2010 1
A0001 11jun2011 11jun2011 1
A0001 12jun2012 13jul2012 1
A0001 13jul2012 10aug2012 1
A0001 07dec2012 01jan2013 0
A0002 07dec2012 01jan2013 0
A0003 22dec2012 01jan2013 0
;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The last two and the final one for the first store are not defined as closed because the data goes all the way to the end of the study period. So, anything before '31-DEC-2012'd would be defined as a closure, and anything with a gap &amp;gt;30 days would be defined as a closure.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 21:30:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779209#M248141</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-11-08T21:30:04Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779219#M248146</link>
      <description>&lt;P&gt;Maybe I'm just getting lost at this point, but why are transaction number 6 and 7 for store_id A0001 not in your `want` data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for posting your `want` data, by the way.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 21:01:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779219#M248146</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-11-08T21:01:36Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779221#M248147</link>
      <description>Transaction 6 is 4th row in the 'want' dataset. Transaction 7 is rolled up into the 4th row because there wasn't a gap of &amp;gt;30 days.</description>
      <pubDate>Mon, 08 Nov 2021 21:07:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779221#M248147</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-11-08T21:07:37Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779225#M248148</link>
      <description>&lt;PRE&gt;STORE_ID 	transaction_id 	transaction_date 	lag_tx_date 	diff
A0001 	1 	22JAN2010 	. 	.
A0001 	2 	23JAN2010 	22JAN2010 	1
A0001 	3 	24JAN2010 	23JAN2010 	1
A0001 	4 	11MAR2010 	24JAN2010 	46
A0001 	5 	11JUN2011 	11MAR2010 	457
A0001 	6 	12JUN2012 	11JUN2011 	367
A0001 	7 	13JUL2012 	12JUN2012 	31
A0001 	8 	10AUG2012 	13JUL2012 	28
A0001 	9 	10AUG2012 	10AUG2012 	0
A0001 	10 	10AUG2012 	10AUG2012 	0
A0001 	11 	07DEC2012 	10AUG2012 	119
A0001 	12 	22DEC2012 	07DEC2012 	15
A0001 	13 	01JAN2013 	22DEC2012 	10&lt;/PRE&gt;
&lt;P&gt;Okay, so why is 6 not it's own record? And 7, too? Am I misunderstanding something? I'm using the INTCK function like &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 21:14:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779225#M248148</guid>
      <dc:creator>maguiremq</dc:creator>
      <dc:date>2021-11-08T21:14:48Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779227#M248149</link>
      <description>&lt;P&gt;Gaps of &amp;gt;30 days or more indicates a closure. &lt;BR /&gt;So for the first store_id:&lt;BR /&gt;Open: Jan 22-2010; Close: Jan 24-2010&lt;BR /&gt;Open: Mar-11-2010; Close: Mar-11-2010&lt;BR /&gt;Open: Jun-11-2011; Close: Jun-11-2010&lt;BR /&gt;Open: Jun 12-2012; Close: Jul-13-2012&lt;BR /&gt;Open: Jul-13-2012; Close: Aug-10-2012&lt;BR /&gt;Open: Dec-7-2012; Close Jan 1-2013&lt;BR /&gt;&lt;BR /&gt;I stand corrected--I missed a row.&lt;/P&gt;</description>
      <pubDate>Mon, 08 Nov 2021 21:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779227#M248149</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-11-08T21:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779255#M248156</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/281770"&gt;@maguiremq&lt;/a&gt; --I saw your website and know you work with claims data and this is an example test case for it.&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/60547"&gt;@sbxkoenk&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is what I've come up with so far:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
 set have1;
 by store_id transaction_date;
 if first.store_id or dif(transaction_date) gt 30 then n+1;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I now want to generate open date using the first of the group and last as the close date, but if only one in the group then it's both open and close.&lt;/P&gt;</description>
      <pubDate>Tue, 09 Nov 2021 02:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779255#M248156</guid>
      <dc:creator>A_Swoosh</dc:creator>
      <dc:date>2021-11-09T02:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: Keep based on gaps</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779298#M248162</link>
      <description>&lt;P&gt;I think you are quite close. Try this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data groups;                                                
  set have1;                                                
  by store_id;                                              
  if not first.store_id and dif(transaction_date)&amp;gt;30 then   
    group+1;                                                
run;                                                        
data want;                                                  
  do until(last.group);                                     
    set groups;                                             
    by store_id group;                                      
    if first.group then                                     
      open=transaction_date;                                
    end;                                                    
  close=transaction_date;                                   
  closed=close&amp;lt;'31DEC2012'd;                                
  format open close date8.;                                 
  keep store_id open close closed;                          
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Nov 2021 11:34:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Keep-based-on-gaps/m-p/779298#M248162</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2021-11-09T11:34:54Z</dc:date>
    </item>
  </channel>
</rss>

