<?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: Identifying first date of certain number of days used with an allowable gap in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/Identifying-first-date-of-certain-number-of-days-used-with-an/m-p/505343#M15689</link>
    <description>&lt;P&gt;You&amp;nbsp;may want to introduce some windowing variables, then do selection based on their values. In this example, these variables are called within80days and within40days.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select *,

	case
	when -80 &amp;lt;= end_dt - start_dt &amp;lt;= 80
		  then 'Yes'
	else 'No'
	end as within80days,

	case 
        when -40 &amp;lt;= end_dt - start_dt &amp;lt;= 40
	      then 'Yes'
	else 'No'
	end as within40days

	from drug_dates
	;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Reference&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Jack Shostak,&amp;nbsp;&lt;/SPAN&gt;SAS Programming in the Pharmaceutical Industry, 2nd Ed.&lt;/P&gt;</description>
    <pubDate>Wed, 17 Oct 2018 20:50:14 GMT</pubDate>
    <dc:creator>pink_poodle</dc:creator>
    <dc:date>2018-10-17T20:50:14Z</dc:date>
    <item>
      <title>Identifying first date of certain number of days used with an allowable gap</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Identifying-first-date-of-certain-number-of-days-used-with-an/m-p/505325#M15688</link>
      <description>&lt;P&gt;I am working with data on prescription drugs (drug-level data) with start and end dates for prescriptions for large number of individuals. For each individual in drug, say, XYZ, I want to identify the first date when they had filled prescriptions for at least 40 days within 80 days time-frame without a gap of more than 20 days (allowable gap of 20 days). Below is a part of the data.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data drug_dates;
input id $ start_dt end_dt;
informat start_dt yymmdd10.
         end_dt yymmdd10.;
format start_dt yymmdd10.
       end_dt yymmdd10.;
datalines;
A 2017/01/01 2017/01/10
A 2017/01/16 2017/02/04
A 2017/03/02 2017/03/11
A 2017/04/01 2017/04/15
A 2017/05/06 2017/05/15
A 2017/05/26 2017/06/14
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Below is the logic I tried to use. Basically, I expanded all the prescriptions vertically, with each row for each day, and tried to identify the gaps. But I still cannot identify the first date which indicates that they have reached 40 days of use in 80 days without gap of more than 20 days. I am not sure how to do an iterative sum across rows. Thank you!&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data drug_dates1;
set drug_dates;
do each_day=start_dt to end_dt;
output;
end;
format each_day yymmdd10.;
run;

proc sql;
create table all_dates as
select distinct id, min(start_dt) as min_start format yymmdd10., max(end_dt) as max_end format yymmdd10.
from drug_dates
group by id;
quit;

data all_dates1;
set all_dates;
do each_day=min_start to max_end;
output;
end;
format each_day yymmdd10.;
run;

proc sql;
create table combined as
select *
from all_dates1 a left join drug_dates1 b
on a.id=b.id and a.each_day=b.each_day
order by id, each_day;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 20:16:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Identifying-first-date-of-certain-number-of-days-used-with-an/m-p/505325#M15688</guid>
      <dc:creator>Mahip</dc:creator>
      <dc:date>2018-10-17T20:16:17Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying first date of certain number of days used with an allowable gap</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Identifying-first-date-of-certain-number-of-days-used-with-an/m-p/505343#M15689</link>
      <description>&lt;P&gt;You&amp;nbsp;may want to introduce some windowing variables, then do selection based on their values. In this example, these variables are called within80days and within40days.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
	select *,

	case
	when -80 &amp;lt;= end_dt - start_dt &amp;lt;= 80
		  then 'Yes'
	else 'No'
	end as within80days,

	case 
        when -40 &amp;lt;= end_dt - start_dt &amp;lt;= 40
	      then 'Yes'
	else 'No'
	end as within40days

	from drug_dates
	;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;U&gt;Reference&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Jack Shostak,&amp;nbsp;&lt;/SPAN&gt;SAS Programming in the Pharmaceutical Industry, 2nd Ed.&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 20:50:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Identifying-first-date-of-certain-number-of-days-used-with-an/m-p/505343#M15689</guid>
      <dc:creator>pink_poodle</dc:creator>
      <dc:date>2018-10-17T20:50:14Z</dc:date>
    </item>
    <item>
      <title>Re: Identifying first date of certain number of days used with an allowable gap</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/Identifying-first-date-of-certain-number-of-days-used-with-an/m-p/505371#M15690</link>
      <description>&lt;P&gt;Equivalent to&lt;/P&gt;
&lt;PRE&gt;-80 &amp;lt;= end_dt - start_dt &amp;lt;= 80&lt;/PRE&gt;
&lt;P&gt;for symmetric range is&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;abs(end_dt-start_dt) le 80&lt;/PRE&gt;
&lt;P&gt;I often miss negative signs&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 17 Oct 2018 21:47:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/Identifying-first-date-of-certain-number-of-days-used-with-an/m-p/505371#M15690</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2018-10-17T21:47:22Z</dc:date>
    </item>
  </channel>
</rss>

