<?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: count number of times an event occurs 3 times within a two week period and capture the date in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/494115#M130152</link>
    <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
    <pubDate>Mon, 10 Sep 2018 13:14:53 GMT</pubDate>
    <dc:creator>Informatician</dc:creator>
    <dc:date>2018-09-10T13:14:53Z</dc:date>
    <item>
      <title>count number of times an event occurs 3 times within a two week period and capture the date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/492842#M129603</link>
      <description>&lt;P&gt;I have provided a deidentified screenshot with dummy&amp;nbsp;values to demonstrate my data. I can't figure out how to identify 3 dates that fall within a 14 day period of time for each patient for a specific medication. Please note that some patients have more than 100 occurrences for a given medication (i.e. &amp;gt;100 dates).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For example, starting with patient 34444862 obs=3-7 (table long) and/or obs=6 (table wide). I would like to capture 27APR2018 (obs=7) as the only time Bupropion occurred three times within a 14 day period.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For patient 34443625 obs=12-26 (table long) and/or obs=11 (table wide). I would like to capture 03APR2018 as the first time 3 Sertralines occurred within 14 days. I also need to capture 06APR2018, 10APR2018, 13APR2018, and 16APR2018 as the next dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried using arrays (table wide) and variations of the dif() function (table long) and I am banging my head against a wall with a looming deadline. Any help you can provide is greatly appreciated! Thank you in advance.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am using SAS 9.4 and I have attached the dummy data.&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-inline" image-alt="Slide1.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23027i0327A734DE2850AA/image-size/large?v=v2&amp;amp;px=999" role="button" title="Slide1.JPG" alt="Slide1.JPG" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Slide2.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/23028i95AE4827527BF110/image-size/large?v=v2&amp;amp;px=999" role="button" title="Slide2.JPG" alt="Slide2.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 05 Sep 2018 23:18:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/492842#M129603</guid>
      <dc:creator>Informatician</dc:creator>
      <dc:date>2018-09-05T23:18:16Z</dc:date>
    </item>
    <item>
      <title>Re: count number of times an event occurs 3 times within a two week period and capture the date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/492990#M129667</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This can be achieved using &lt;A href="https://go.documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lrcon&amp;amp;docsetTarget=n01a08zkzy5igbn173zjz82zsi1s.htm&amp;amp;locale=en" target="_self"&gt;first. &amp;amp; last. processing&lt;/A&gt; in the datastep&lt;BR /&gt;&lt;BR /&gt;I've&amp;nbsp;provided an example using a subset of your long file below&lt;BR /&gt;&lt;BR /&gt;Andy&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;data long ;
		format date date. ;
		infile cards ;
		input 
			patientId 
			treatment : $32.
			date : date. ;
cards; 
34444486	FLUOXETINE	4-Apr-18
34444486	FLUOXETINE	14-Apr-18
34444862	BUPROPION	1-Apr-18
34444862	BUPROPION	4-Apr-18
34444862	BUPROPION	20-Apr-18
34444862	BUPROPION	27-Apr-18
34444862	BUPROPION	27-Apr-18
34443348	OLANZAPINE	9-Apr-18
34443348	OLANZAPINE	10-Apr-18
34443348	OLANZAPINE	29-Apr-18
34443399	TRAZODONE	12-Apr-18
34443625	SERTRALINE	1-Apr-18
34443625	SERTRALINE	2-Apr-18
34443625	SERTRALINE	3-Apr-18
34443625	SERTRALINE	4-Apr-18
34443625	SERTRALINE	5-Apr-18
34443625	SERTRALINE	6-Apr-18
34443625	SERTRALINE	7-Apr-18
34443625	SERTRALINE	8-Apr-18
34443625	SERTRALINE	10-Apr-18
34443625	SERTRALINE	11-Apr-18
34443625	SERTRALINE	12-Apr-18
34443625	SERTRALINE	13-Apr-18
34443625	SERTRALINE	14-Apr-18
34443625	SERTRALINE	15-Apr-18
34443625	SERTRALINE	16-Apr-18
34443874	FLUOXETINE	7-Apr-18
34443874	OLANZAPINE	7-Apr-18
34443232	BUPROPION	6-Apr-18
34443232	BUPROPION	8-Apr-18
34443232	BUPROPION	10-Apr-18
34443232	BUPROPION	11-Apr-18
34443232	BUPROPION	12-Apr-18
34443232	BUPROPION	14-Apr-18
34443232	BUPROPION	15-Apr-18
34443232	BUPROPION	15-Apr-18
34443232	BUPROPION	16-Apr-18
34443232	BUPROPION	17-Apr-18
34443232	BUPROPION	21-Apr-18
34443232	BUPROPION	22-Apr-18
34443232	BUPROPION	29-Apr-18
34443232	BUPROPION	29-Apr-18
;
run ;

proc sort ;
	by patientId treatment date ;
run  ;

data alerts ;
	retain 
		treatmentCount 
		firstDate ;
	set long;
	by patientId treatment date ;
	/* first.treatment is set to 1 when it's the first occurance of a new treatment or first occurance of a new patientID */
	if first.treatment or treatmentCount=3 then do ;
		treatmentCount=1 ;
		firstDate=date ;
		put treatmentCount= patientId= treatment= firstDate= date. date= ;

	end ;
	/* If it is not the first occurance of a treatment or patientID */
	else do ;
		put treatmentCount= patientId= treatment= firstDate= date. date= ;
		/* Is the current treatment date within 14 days of the first treatment date */
		if date-firstDate&amp;lt;14 then do ;
			/* increase treatmentCounter */
			treatmentCount=treatmentCount+1 ;
			/* If is the 3rd treatment within 14 days */
			if treatmentCount=3 then do ;
				put "&amp;gt;&amp;gt;&amp;gt;" treatmentCount= patientId= treatment= firstDate= date. date= ;
				output ;
			end ;
		end ;
		else
			treatmentCount=1 ;
			firstDate=date ;
	end ;


			
run ;&lt;/PRE&gt;</description>
      <pubDate>Thu, 06 Sep 2018 12:54:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/492990#M129667</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2018-09-06T12:54:50Z</dc:date>
    </item>
    <item>
      <title>Re: count number of times an event occurs 3 times within a two week period and capture the date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/492999#M129669</link>
      <description>Sorry, just realized there's a situation that this doesn't work for.</description>
      <pubDate>Thu, 06 Sep 2018 13:09:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/492999#M129669</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2018-09-06T13:09:37Z</dc:date>
    </item>
    <item>
      <title>Re: count number of times an event occurs 3 times within a two week period and capture the date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/493022#M129676</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's updated code, that I believe will&amp;nbsp;solve the&amp;nbsp;problem. I'm making use of BY group processing (Last./First.) referenced in the earlier post and the &lt;A href="https://go.documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=lefunctionsref&amp;amp;docsetTarget=n0l66p5oqex1f2n1quuopdvtcjqb.htm&amp;amp;locale=en" target="_self"&gt;LAG function&lt;/A&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;In the code below I added a check variable which I manually set to 1 for records that meet your required output, as I understand it.&lt;BR /&gt;&lt;BR /&gt;Test the code&amp;nbsp;and please confirm if it solves your&amp;nbsp;question&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data long ;
		format date date. ;
		infile cards ;
		input 
			patientId 
			treatment : $32.
			date : date. 
			check $ ;
cards; 
00000001    TEST        1-Apr-18  0
00000001    TEST        5-Apr-18  0
00000001    TEST        15-Apr-18 0
00000001    TEST        16-Apr-18 1
00000001    TEST        30-Apr-18 0
00000001    TEST        05-May-18 0 
00000001    TEST        15-May-18 0
00000001    TEST        18-May-18 1
34444486	FLUOXETINE	4-Apr-18  0
34444486	FLUOXETINE	14-Apr-18 0
34444862	BUPROPION	1-Apr-18  0
34444862	BUPROPION	4-Apr-18  0
34444862	BUPROPION	20-Apr-18 0
34444862	BUPROPION	27-Apr-18 0
34444862	BUPROPION	27-Apr-18 1
34443348	OLANZAPINE	9-Apr-18  0
34443348	OLANZAPINE	10-Apr-18 0
34443348	OLANZAPINE	29-Apr-18 0
34443399	TRAZODONE	12-Apr-18 0
34443625	SERTRALINE	1-Apr-18  0
34443625	SERTRALINE	2-Apr-18  0
34443625	SERTRALINE	3-Apr-18  1
34443625	SERTRALINE	4-Apr-18  0  
34443625	SERTRALINE	5-Apr-18  0
34443625	SERTRALINE	6-Apr-18  1
34443625	SERTRALINE	7-Apr-18  0
34443625	SERTRALINE	8-Apr-18  0
34443625	SERTRALINE	10-Apr-18 1
34443625	SERTRALINE	11-Apr-18 0
34443625	SERTRALINE	12-Apr-18 0 
34443625	SERTRALINE	13-Apr-18 1
34443625	SERTRALINE	14-Apr-18 0 
34443625	SERTRALINE	15-Apr-18 0
34443625	SERTRALINE	16-Apr-18 1
34443874	FLUOXETINE	7-Apr-18  0
34443874	OLANZAPINE	7-Apr-18  0
34443232	BUPROPION	6-Apr-18  0
34443232	BUPROPION	8-Apr-18  0
34443232	BUPROPION	10-Apr-18 1
34443232	BUPROPION	11-Apr-18 0
34443232	BUPROPION	12-Apr-18 0 
34443232	BUPROPION	14-Apr-18 1
34443232	BUPROPION	15-Apr-18 0
34443232	BUPROPION	15-Apr-18 0
34443232	BUPROPION	16-Apr-18 1
34443232	BUPROPION	17-Apr-18 0
34443232	BUPROPION	21-Apr-18 0
34443232	BUPROPION	22-Apr-18 1
34443232	BUPROPION	29-Apr-18 0 
34443232	BUPROPION	29-Apr-18 0
;
run ;

proc sort ;
	by patientId treatment date ;
run  ;

data alerts ;
	retain 
		treatmentCount 
		firstDate ;
	set long ;
	*put "S : " treatmentCount= patientId= treatment= firstDate= date. date= ;
	patientIdL2=lag2(patientID) ;
	treatmentL2=lag2(treatment) ;
	dateL2     =lag2(date) ;
	patientIdL1=lag1(patientID) ;
	treatmentL1=lag1(treatment) ;
	dateL1     =lag1(date) ;
	by patientId treatment date ;
	/* first.treatment is set to 1 when it's the first occurance of a new treatment or first occurance of a new patientID */
	if first.treatment or treatmentCount=3 then do ;
		treatmentCount=1 ;
		firstDate=date ;

	end ;
	/* If it is not the first occurance of a treatment or patientID */
	else do ;
		/* Is the current treatment date within 14 days of the first treatment date */
		if date-firstDate&amp;lt;14 then do ;
			*put "&amp;lt;14" date 8. firstdate 8. ;
			/* increase treatmentCounter */
			treatmentCount=treatmentCount+1 ;
			/* If it is the 3rd treatment within 14 days */
			if treatmentCount=3 then do ;
				*put "&amp;gt;&amp;gt;&amp;gt;" treatmentCount= patientId= treatment= firstDate= date. date= ;
				output ;
			end ;
		end ;
		if date-firstDate&amp;gt;=14 then do ;
			*put "&amp;gt;=14" date 8. firstdate 8. ;
			*put dateL1= date. dateL2= date. ;
			if patientIdL2=patientId and 
				treatmentL2=treatment and 
				date-dateL2&amp;lt;14 then do ;
				treatmentCount=2 ;
				firstDate=dateL2 ;
			end ;
			else if patientIdL1=patientId and 
				treatmentL1=treatment and 
				date-dateL1&amp;lt;14 then do ;
				treatmentCount=2 ;
				firstDate=dateL1 ;
			end ;
		end ;
	end ;
	*put "E : " treatmentCount= patientId= treatment= firstDate= date. date= ;


			
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 06 Sep 2018 14:06:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/493022#M129676</guid>
      <dc:creator>AMSAS</dc:creator>
      <dc:date>2018-09-06T14:06:24Z</dc:date>
    </item>
    <item>
      <title>Re: count number of times an event occurs 3 times within a two week period and capture the date</title>
      <link>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/494115#M130152</link>
      <description>&lt;P&gt;Thank you!&lt;/P&gt;</description>
      <pubDate>Mon, 10 Sep 2018 13:14:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/count-number-of-times-an-event-occurs-3-times-within-a-two-week/m-p/494115#M130152</guid>
      <dc:creator>Informatician</dc:creator>
      <dc:date>2018-09-10T13:14:53Z</dc:date>
    </item>
  </channel>
</rss>

