<?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 How to Count Dates within Certain Range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742714#M232376</link>
    <description>&lt;P&gt;I am trying to look at different lines of data and count how many times a line falls within 30 days of previous lines for the same id and code. I have been trying to do this with a data step but haven't been able to figure it out and am open to any solutions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my starting data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id code date :date9.;
format date date9.;
datalines;
4350 1 '01JUN2021'd
4350 1 '13JUN2021'd
4456 3 '27JUL2021'd
4456 4 '27NOV2021'd
1234 1 '01JAN2021'd
1234 1 '10JAN2021'd
1234 1 '21JAN2021'd
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is what I'm trying to generate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id code date :date9. count;
format date date9.;
datalines;
4350 1 '01JUN2021'd 0
4350 1 '13JUN2021'd 1
4456 3 '27JUL2021'd 0
4456 4 '27NOV2021'd 0
1234 1 '01JAN2021'd 0
1234 1 '10JAN2021'd 1
1234 1 '21JAN2021'd 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 20 May 2021 17:19:33 GMT</pubDate>
    <dc:creator>A_SAS_Man</dc:creator>
    <dc:date>2021-05-20T17:19:33Z</dc:date>
    <item>
      <title>How to Count Dates within Certain Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742714#M232376</link>
      <description>&lt;P&gt;I am trying to look at different lines of data and count how many times a line falls within 30 days of previous lines for the same id and code. I have been trying to do this with a data step but haven't been able to figure it out and am open to any solutions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is my starting data.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id code date :date9.;
format date date9.;
datalines;
4350 1 '01JUN2021'd
4350 1 '13JUN2021'd
4456 3 '27JUL2021'd
4456 4 '27NOV2021'd
1234 1 '01JAN2021'd
1234 1 '10JAN2021'd
1234 1 '21JAN2021'd
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Here is what I'm trying to generate.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id code date :date9. count;
format date date9.;
datalines;
4350 1 '01JUN2021'd 0
4350 1 '13JUN2021'd 1
4456 3 '27JUL2021'd 0
4456 4 '27NOV2021'd 0
1234 1 '01JAN2021'd 0
1234 1 '10JAN2021'd 1
1234 1 '21JAN2021'd 2
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 17:19:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742714#M232376</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2021-05-20T17:19:33Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count Dates within Certain Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742721#M232381</link>
      <description>&lt;P&gt;In a data step, use BY processing and LAG to compare with the previous value.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id notsorted code;
if not first.code and date - lag(date) le 30
then count + 1;
else count = 0;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The SUM statement (count + 1) implies an automatic RETAIN.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 17:30:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742721#M232381</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-20T17:30:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count Dates within Certain Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742729#M232385</link>
      <description>&lt;P&gt;You can save yourself a bit of typing for the data lines in your code:&lt;/P&gt;
&lt;PRE&gt;data have;
input id code date :date9.;
format date date9.;
datalines;
4350 1 01JUN2021
4350 1 13JUN2021
4456 3 27JUL2021
4456 4 27NOV2021
1234 1 01JAN2021
1234 1 10JAN2021
1234 1 21JAN2021
;
run;&lt;/PRE&gt;
&lt;P&gt;the quotes and d are needed to define a literal date in code and not needed, and potentially can cause errors when reading the data.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 17:46:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742729#M232385</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-05-20T17:46:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count Dates within Certain Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742732#M232387</link>
      <description>&lt;P&gt;This is very close, but it seems to "overcount" in some instances. See below modified example to demonstrate.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id code date :date9.;
format date date9.;
datalines;
4350 1 '01JUN2021'd
4350 1 '13JUN2021'd
4456 3 '27JUL2021'd
4456 4 '27NOV2021'd
1234 1 '01JAN2021'd
1234 1 '10JAN2021'd
1234 1 '21JAN2021'd
1234 1 '15FEB2021'd
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Your code would produce this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data code_output;
input id code date :date9. count;
format date date9.;
datalines;
4350 1 '01JUN2021'd 0
4350 1 '13JUN2021'd 1
4456 3 '27JUL2021'd 0
4456 4 '27NOV2021'd 0
1234 1 '01JAN2021'd 0
1234 1 '10JAN2021'd 1
1234 1 '21JAN2021'd 2
1234 1 '15FEB2021'd 3
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;What I'm trying to get is this:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id code date :date9. count;
format date date9.;
datalines;
4350 1 '01JUN2021'd 0
4350 1 '13JUN2021'd 1
4456 3 '27JUL2021'd 0
4456 4 '27NOV2021'd 0
1234 1 '01JAN2021'd 0
1234 1 '10JAN2021'd 1
1234 1 '21JAN2021'd 2
1234 1 '15FEB2021'd 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;This is because the last line is only within 30 days of one of the other lines with matching codes and ids.&lt;/P&gt;</description>
      <pubDate>Thu, 20 May 2021 17:48:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742732#M232387</guid>
      <dc:creator>A_SAS_Man</dc:creator>
      <dc:date>2021-05-20T17:48:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to Count Dates within Certain Range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742739#M232393</link>
      <description>&lt;P&gt;So if I interpret you right, we need to keep a starting date for the group, and start a new group when we go past 30 days of that:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set have;
by id notsorted code;
retain start_date;
l_date = lag(date);
if first.code then start_date = date;
if not first.code and date - l_date le 30
then do;
  if date - start_date le 30
  then count + 1;
  else do;
    count = 1;
    start_date = l_date;
  end;
end;
else count = 0;
drop l_date start_date;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 20 May 2021 18:02:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-Count-Dates-within-Certain-Range/m-p/742739#M232393</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2021-05-20T18:02:20Z</dc:date>
    </item>
  </channel>
</rss>

