<?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: Counting days before and after an event in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/901071#M356107</link>
    <description>&lt;P&gt;Thank you, this works out! Even though only for those patient_id that only have one newuse=1, if more than one, it counts form the last one on.&lt;/P&gt;</description>
    <pubDate>Wed, 01 Nov 2023 14:02:09 GMT</pubDate>
    <dc:creator>Tamino</dc:creator>
    <dc:date>2023-11-01T14:02:09Z</dc:date>
    <item>
      <title>Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900129#M355747</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with claims of medication, each line / observation is one claim. I have the patient_ID, the date of the claim, and the medication. Now I created a variable, which defines the event (claim of a specific medication x, newuse=1). Here is a sample dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE width="364"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="80"&gt;patient_ID&lt;/TD&gt;
&lt;TD width="80"&gt;date_claim&lt;/TD&gt;
&lt;TD width="124"&gt;medication&lt;/TD&gt;
&lt;TD width="80"&gt;newuse&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;17.06.2016&lt;/TD&gt;
&lt;TD&gt;Medication&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;TD&gt;30.08.2019&lt;/TD&gt;
&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;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;12.10.2020&lt;/TD&gt;
&lt;TD&gt;Medication&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;12.10.2020&lt;/TD&gt;
&lt;TD&gt;Medication&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;20.11.2020&lt;/TD&gt;
&lt;TD&gt;Medication&lt;/TD&gt;
&lt;TD&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;3&lt;/TD&gt;
&lt;TD&gt;20.11.2020&lt;/TD&gt;
&lt;TD&gt;Medication X&lt;/TD&gt;
&lt;TD&gt;1&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I would like to create a new variable, which counts the number of days before and after this claim, i.e. counting number of days backwards and forward from this event (newuse=1) for each patient_ID. How can I do this / which command do I have to apply?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I would be very happy for your help, thank you in advance.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 13:37:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900129#M355747</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2023-10-26T13:37:08Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900134#M355751</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/414097"&gt;@Tamino&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset with claims of medication, each line / observation is one claim. I have the patient_ID, the date of the claim, and the medication. Now I created a variable, which defines the event (claim of a specific medication x, newuse=1). Here is a sample dataset:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now I would like to create a new variable, which counts the number of days before and after this claim, i.e. counting number of days backwards and forward from this event (newuse=1) for each patient_ID. How can I do this / which command do I have to apply?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Counting number of days backwards until what? Counting number of days forward until what? How do we know when to stop counting forwards or backwards?&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 14:03:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900134#M355751</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2023-10-26T14:03:40Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900136#M355753</link>
      <description>&lt;P&gt;So there can only be one newuse=1 per patient_ID?&lt;/P&gt;
&lt;P&gt;I can see to strategies:&lt;/P&gt;
&lt;P&gt;- Join the table with itself on patiend_ID (the "left" tabel in the you is sub-setted with newuse=1). Then calculate the difference between the claim date and the newuse=1 date.&lt;/P&gt;
&lt;P&gt;- Sort the table so that newuse=1 comes first for each Patiend_ID. The in a data step retain in a new variable the newuse=1 data, and then calculate the date difference for each observation.&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 14:08:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900136#M355753</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2023-10-26T14:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900142#M355754</link>
      <description>&lt;P&gt;You will make things a lot easier for us and yourself if you present your input data as a data step, e.g.:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards delimiter='|';
  length patient_ID date_claim 8 medication $20 newuse 8;
  informat date_claim ddmmyy10.;
  format date_claim ddmmyy10.;
  input patient_ID date_claim medication newuse;
cards;
1|17.06.2016|Medication|0
1|30.08.2019|Medication|0
1|30.08.2019|Medication|0
1|23.09.2019|Medication|0
1|23.09.2019|Medication|0
1|23.09.2019|Medication|0
1|23.09.2019|Medication|0
1|24.10.2019|Medication X|1
1|22.11.2019|Medication|0
1|03.03.2020|Medication|0
1|15.07.2020|Medication|0
1|02.03.2021|Medication|0
1|15.11.2021|Medication|0
2|27.01.2017|Medication|0
2|24.08.2018|Medication|0
2|14.09.2018|Medication|0
2|24.09.2018|Medication|0
2|19.10.2018|Medication|0
2|16.11.2018|Medication|0
2|01.02.2019|Medication|0
2|01.02.2019|Medication X|1
2|08.04.2019|Medication|0
2|06.05.2019|Medication|0
2|04.11.2019|Medication|0
2|04.11.2019|Medication|0
2|27.03.2020|Medication|0
2|21.06.2021|Medication|0
2|14.12.2021|Medication|0
3|25.06.2018|Medication|0
3|27.11.2018|Medication|0
3|16.04.2019|Medication|0
3|15.04.2020|Medication|0
3|15.04.2020|Medication|0
3|15.04.2020|Medication|0
3|24.04.2020|Medication|0
3|12.10.2020|Medication|0
3|12.10.2020|Medication|0
3|20.11.2020|Medication|0
3|20.11.2020|Medication X|1
;run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And then present the data you want in the same way.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 14:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900142#M355754</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2023-10-26T14:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900149#M355758</link>
      <description>&lt;P&gt;What I understood is to calculate the number of days before and after the claim_date per patient_id. If so, the below code might give you the solution.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input @1 patient_ID @5 date_claim:ddmmyy10. @17 medication $17-28	@29 newuse;
format date_claim date11.;
cards; 
1	17.06.2016	Medication	0
1	30.08.2019	Medication	0
1	30.08.2019	Medication	0
1	23.09.2019	Medication	0
1	23.09.2019	Medication	0
1	23.09.2019	Medication	0
1	23.09.2019	Medication	0
1	24.10.2019	Medication X1
1	22.11.2019	Medication	0
1	03.03.2020	Medication	0
1	15.07.2020	Medication	0
1	02.03.2021	Medication	0
1	15.11.2021	Medication	0
2	27.01.2017	Medication	0
2	24.08.2018	Medication	0
2	14.09.2018	Medication	0
2	24.09.2018	Medication	0
2	19.10.2018	Medication	0
2	16.11.2018	Medication	0
2	01.02.2019	Medication	0
2	01.02.2019	Medication X1
2	08.04.2019	Medication	0
2	06.05.2019	Medication	0
2	04.11.2019	Medication	0
2	04.11.2019	Medication	0
2	27.03.2020	Medication	0
2	21.06.2021	Medication	0
2	14.12.2021	Medication	0
3	25.06.2018	Medication	0
3	27.11.2018	Medication	0
3	16.04.2019	Medication	0
3	15.04.2020	Medication	0
3	15.04.2020	Medication	0
3	15.04.2020	Medication	0
3	24.04.2020	Medication	0
3	12.10.2020	Medication	0
3	12.10.2020	Medication	0
3	20.11.2020	Medication	0
3	20.11.2020	Medication X1
;
proc print; run; 

proc sql;
	create table want as
		select*, min(date_claim) as start format=date11., max(date_claim) as end format=date11.,
			case newuse when 1 then date_claim-min(date_claim) else . end as Days_Before_Claim,
			case newuse when 1 then max(date_claim)-date_claim else . end as Days_After_Claim
			from have
		group by patient_id
	order by patient_id, date_claim, newuse;
quit; 
		
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 26 Oct 2023 14:57:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900149#M355758</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-10-26T14:57:01Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900265#M355801</link>
      <description>&lt;P&gt;Assuming the data are sorted by patient_id/date_claim, you can read each ID in two passes.&amp;nbsp; In the first pass establish and retain the start_date and end_date variables (i.e. the earliest and latest date_claim values).&amp;nbsp; In the second pass, read only then newuse=1 cases and calculate the number of days prior and after each newuse=1 case:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  infile cards delimiter='|';
  length patient_ID date_claim 8 medication $20 newuse 8;
  informat date_claim ddmmyy10.;
  format date_claim ddmmyy10.;
  input patient_ID date_claim medication newuse;
cards;
1|17.06.2016|Medication|0
1|30.08.2019|Medication|0
1|30.08.2019|Medication|0
1|23.09.2019|Medication|0
1|23.09.2019|Medication|0
1|23.09.2019|Medication|0
1|23.09.2019|Medication|0
1|24.10.2019|Medication X|1
1|22.11.2019|Medication|0
1|03.03.2020|Medication|0
1|15.07.2020|Medication|0
1|02.03.2021|Medication|0
1|15.11.2021|Medication|0
2|27.01.2017|Medication|0
2|24.08.2018|Medication|0
2|14.09.2018|Medication|0
2|24.09.2018|Medication|0
2|19.10.2018|Medication|0
2|16.11.2018|Medication|0
2|01.02.2019|Medication|0
2|01.02.2019|Medication X|1
2|08.04.2019|Medication|0
2|06.05.2019|Medication|0
2|04.11.2019|Medication|0
2|04.11.2019|Medication|0
2|27.03.2020|Medication|0
2|21.06.2021|Medication|0
2|14.12.2021|Medication|0
3|25.06.2018|Medication|0
3|27.11.2018|Medication|0
3|16.04.2019|Medication|0
3|15.04.2020|Medication|0
3|15.04.2020|Medication|0
3|15.04.2020|Medication|0
3|24.04.2020|Medication|0
3|12.10.2020|Medication|0
3|12.10.2020|Medication|0
3|20.11.2020|Medication|0
3|20.11.2020|Medication X|1
run;

data want;
  set have (in=first_pass)
      have (in=second_pass where=(newuse=1)) ;
  by patient_id;
  retain start_date end_date;
  if first.patient_id then start_date=date_claim;
  if first_pass then end_date=date_claim;
  format start_date end_date ddmmyy10.;

  if second_pass;
  days_before= date_claim-start_date;
  days_after = end_date-date_claim;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This works no matter how many NEWUSE cases a patient might have.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 27 Oct 2023 03:36:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900265#M355801</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-10-27T03:36:31Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900311#M355820</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*
Can you post that new variable you want to create?
If you don't have a big table ,could try the following code,
otherwise,need to resort to Hash Table.
*/
data have;
  infile cards delimiter='|';
  length patient_ID date_claim 8 medication $20 newuse 8;
  informat date_claim ddmmyy10.;
  format date_claim ddmmyy10.;
  input patient_ID date_claim medication newuse;
cards;
1|17.06.2016|Medication|0
1|30.08.2019|Medication|0
1|30.08.2019|Medication|0
1|23.09.2019|Medication|0
1|23.09.2019|Medication|0
1|23.09.2019|Medication|0
1|23.09.2019|Medication|0
1|24.10.2019|Medication X|1
1|22.11.2019|Medication|0
1|03.03.2020|Medication|0
1|15.07.2020|Medication|0
1|02.03.2021|Medication|0
1|15.11.2021|Medication|0
2|27.01.2017|Medication|0
2|24.08.2018|Medication|0
2|14.09.2018|Medication|0
2|24.09.2018|Medication|0
2|19.10.2018|Medication|0
2|16.11.2018|Medication|0
2|01.02.2019|Medication|0
2|01.02.2019|Medication X|1
2|08.04.2019|Medication|0
2|06.05.2019|Medication|0
2|04.11.2019|Medication|0
2|04.11.2019|Medication|0
2|27.03.2020|Medication|0
2|21.06.2021|Medication|0
2|14.12.2021|Medication|0
3|25.06.2018|Medication|0
3|27.11.2018|Medication|0
3|16.04.2019|Medication|0
3|15.04.2020|Medication|0
3|15.04.2020|Medication|0
3|15.04.2020|Medication|0
3|24.04.2020|Medication|0
3|12.10.2020|Medication|0
3|12.10.2020|Medication|0
3|20.11.2020|Medication|0
3|20.11.2020|Medication X|1
;
proc sql;
create table want as
select *,case when newuse=1 then  count(distinct date_claim)-1 else 0 end as want
 from have 
  group by patient_ID
   order by patient_ID,date_claim;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 27 Oct 2023 11:34:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900311#M355820</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2023-10-27T11:34:11Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900906#M356038</link>
      <description>&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It counts the days from first claim to newuse=1 and days from newuse=1 to the last claim of a patient_id. I would like to know the number of days betweet each single claim and newuse=1. Is this also possible?&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 13:12:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900906#M356038</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2023-10-31T13:12:46Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900908#M356039</link>
      <description>&lt;P&gt;There are also patient_id with more than one newuse=1, that's the problem when sorting the table by newuse so that it is on top.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I didn't get quite well your first suggested strategy.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 13:16:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900908#M356039</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2023-10-31T13:16:11Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900911#M356040</link>
      <description>&lt;P&gt;Counting days between date_claim where newuse=1 and each other claim (date_claim) for each patient (patient_id). So from date_claim of newuse=1 backward for each claim until first claim of a patient_id and forward for each claim until last claim of a patient_id&lt;/P&gt;</description>
      <pubDate>Tue, 31 Oct 2023 13:40:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900911#M356040</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2023-10-31T13:40:13Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900942#M356059</link>
      <description>&lt;P&gt;Possible.&lt;BR /&gt;I would create a temporary column with Medication Date value per PATIENT_ID and subtract DATE_CLAIM from Medication date.&amp;nbsp; I accomplish it in 2 sql steps, but I believe there are ways to calculate it in less steps and more efficient ways.&amp;nbsp;&lt;BR /&gt;Days after claims get a negative (-) number, if you want only positive numbers, then could handle it using a simple arithmetic operations..&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Eg: A little modification to my previous code;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;proc sql;
	create table want as
		select*, min(date_claim) as start format=date11., max(date_claim) as end format=date11.,
			case newuse when 1 then date_claim-min(date_claim) else . end as Days_Before_Claim,
			case newuse when 1 then max(date_claim)-date_claim else . end as Days_After_Claim,
			case newuse when 1 then date_claim else . end as Medication_date
			from have
		group by patient_id
	order by patient_id, date_claim, newuse;
quit; 

proc sql;
	create table want1 as
		select*, max(medication_date)-date_claim as days_count
			from want
		group by patient_id
	order by patient_id, date_claim;
quit; &lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Oct 2023 17:39:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/900942#M356059</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-10-31T17:39:38Z</dc:date>
    </item>
    <item>
      <title>Re: Counting days before and after an event</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/901071#M356107</link>
      <description>&lt;P&gt;Thank you, this works out! Even though only for those patient_id that only have one newuse=1, if more than one, it counts form the last one on.&lt;/P&gt;</description>
      <pubDate>Wed, 01 Nov 2023 14:02:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-days-before-and-after-an-event/m-p/901071#M356107</guid>
      <dc:creator>Tamino</dc:creator>
      <dc:date>2023-11-01T14:02:09Z</dc:date>
    </item>
  </channel>
</rss>

