<?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 Average of observations between multiple dates based on an indicator in SAS Studio</title>
    <link>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404125#M3721</link>
    <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set of patients who use a medications with different dose at different times. The patients may also experience an event (i.e., hospital readmission) at multiple times. Here is a part of the data (for patient #1):&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data have;
input id date drug_dose indicator;
datalines;
1 2008-01-17 7.5  0
1 2008-01-18 14.7 0
1 2008-01-19 21.5 0
1 2008-01-20 27.3 0
1 2008-01-21 32.1 0
1 2008-01-22 35.9 0
1 2008-01-23 38.6 0
1 2008-01-24 40.6 0
1 2008-01-25 41.8 0
1 2008-01-26 42.6 0
1 2008-02-06 0.0  1 
1 2008-03-01 129.4 0
1 2008-03-02 107.7 0
1 2008-03-03 86.2  0
1 2008-03-04 34.4  0
1 2008-03-05 9.1   0
1 2008-03-19 0.0   1
;&lt;/LI-CODE&gt;&lt;P&gt;where indicator 0 and 1 represents "no event" and "event", respectively. Now, I want to measure the ratio of (total cumulative dose used between each two events) and (the total time spanned between these two events). Something like this:&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data want;
input id avg_dose time_start time_stop;
datalines;
1 15.13 2008-1-17 2008-2-6 
1 13.585 2008-2-7 2008-3-5
1 0.0 2008-3-6 2008-3-19
;&lt;/LI-CODE&gt;&lt;P&gt;For example,&amp;nbsp;15.13 = 302.6/20,&amp;nbsp; and 302.6 is the total cumulative dose used from the beginning (2008-1-17) and the time of the first event (2008-2-6), and 20 is the total number of days spanned over between the foregoing two dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea/thought is really appreciated!&lt;/P&gt;</description>
    <pubDate>Fri, 13 Oct 2017 20:42:51 GMT</pubDate>
    <dc:creator>Alireza_Boloori</dc:creator>
    <dc:date>2017-10-13T20:42:51Z</dc:date>
    <item>
      <title>Average of observations between multiple dates based on an indicator</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404125#M3721</link>
      <description>&lt;P&gt;Hello everyone,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set of patients who use a medications with different dose at different times. The patients may also experience an event (i.e., hospital readmission) at multiple times. Here is a part of the data (for patient #1):&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data have;
input id date drug_dose indicator;
datalines;
1 2008-01-17 7.5  0
1 2008-01-18 14.7 0
1 2008-01-19 21.5 0
1 2008-01-20 27.3 0
1 2008-01-21 32.1 0
1 2008-01-22 35.9 0
1 2008-01-23 38.6 0
1 2008-01-24 40.6 0
1 2008-01-25 41.8 0
1 2008-01-26 42.6 0
1 2008-02-06 0.0  1 
1 2008-03-01 129.4 0
1 2008-03-02 107.7 0
1 2008-03-03 86.2  0
1 2008-03-04 34.4  0
1 2008-03-05 9.1   0
1 2008-03-19 0.0   1
;&lt;/LI-CODE&gt;&lt;P&gt;where indicator 0 and 1 represents "no event" and "event", respectively. Now, I want to measure the ratio of (total cumulative dose used between each two events) and (the total time spanned between these two events). Something like this:&lt;/P&gt;&lt;LI-CODE lang="sas"&gt;data want;
input id avg_dose time_start time_stop;
datalines;
1 15.13 2008-1-17 2008-2-6 
1 13.585 2008-2-7 2008-3-5
1 0.0 2008-3-6 2008-3-19
;&lt;/LI-CODE&gt;&lt;P&gt;For example,&amp;nbsp;15.13 = 302.6/20,&amp;nbsp; and 302.6 is the total cumulative dose used from the beginning (2008-1-17) and the time of the first event (2008-2-6), and 20 is the total number of days spanned over between the foregoing two dates.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any idea/thought is really appreciated!&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 20:42:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404125#M3721</guid>
      <dc:creator>Alireza_Boloori</dc:creator>
      <dc:date>2017-10-13T20:42:51Z</dc:date>
    </item>
    <item>
      <title>Re: Average of observations between multiple dates based on an indicator</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404162#M3723</link>
      <description>&lt;P&gt;What is the rule that indicates the second series end on 2008-3-5?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input id avg_dose time_start time_stop;
datalines;
1 15.13 2008-1-17 2008-2-6 
1 13.585 2008-2-7 2008-3-5
1 0.0 2008-3-6 2008-3-19
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I would start with something like:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id date;
   retain date_start cum_dose;
   if first.id then date_start=date;
   if indicator=0 then cum_dose+drug_dose;
   if indicator=1 then do;
      date_stop = date;
      avg_dose= cum_dose/(date_stop - date_start);
      output;
      date_start=date+1;
   end;
   format date_stop  date_start yymmdd10.;
   drop date indicator cum_dose drug_dose;
run;&lt;/PRE&gt;
&lt;P&gt;but that doesn't work for your stop at 2008-3-5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 22:01:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404162#M3723</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2017-10-13T22:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: Average of observations between multiple dates based on an indicator</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404163#M3724</link>
      <description>&lt;P&gt;Adding a link back to your original question that has more details.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-specific-dates/m-p/404117" target="_blank"&gt;https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-specific-dates/m-p/404117&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Oct 2017 22:04:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404163#M3724</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-10-13T22:04:34Z</dc:date>
    </item>
    <item>
      <title>Re: Average of observations between multiple dates based on an indicator</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404187#M3726</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13884"&gt;@ballardw&lt;/a&gt;&amp;nbsp;Thank you very much! Yes, you're right. That was my mistake in writing the original code. There are a couple of points though, and I'd be thankful if you can help me as well:&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Your code only considers the last event (that's because I only provided data for patient 1). However, there are patients that use medications after an event. In that case, the "date_stop" will be the last day of using that medication.&lt;/LI&gt;&lt;LI&gt;I didn't write all of my variables in the original code (for the sake of simplicity). When I run your code for a data set (with all of my variables in it), all those variables will get "." instead of their characters/numerics.&amp;nbsp;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;I was wondering how I can address these two points as well. Once again, thanks for your time and help!&lt;/P&gt;</description>
      <pubDate>Sat, 14 Oct 2017 02:00:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404187#M3726</guid>
      <dc:creator>Alireza_Boloori</dc:creator>
      <dc:date>2017-10-14T02:00:20Z</dc:date>
    </item>
    <item>
      <title>Re: Average of observations between multiple dates based on an indicator</title>
      <link>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404213#M3727</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id date : yymmdd10. drug_dose indicator;
format  date yymmdd10.;
datalines;
1 2008-01-17 7.5  0
1 2008-01-18 14.7 0
1 2008-01-19 21.5 0
1 2008-01-20 27.3 0
1 2008-01-21 32.1 0
1 2008-01-22 35.9 0
1 2008-01-23 38.6 0
1 2008-01-24 40.6 0
1 2008-01-25 41.8 0
1 2008-01-26 42.6 0
1 2008-02-06 0.0  1 
1 2008-03-01 129.4 0
1 2008-03-02 107.7 0
1 2008-03-03 86.2  0
1 2008-03-04 34.4  0
1 2008-03-05 9.1   0
1 2008-03-19 0.0   1
;

data have;
 set have;
 by id;
 if first.id or (indicator=0 and lag(indicator)=1) then group+1;
 run;
 proc sql;
 select id,min(date) as start format=yymmdd10.,max(date) as stop format=yymmdd10.,
  sum(drug_dose) as sum,
  calculated sum/(calculated stop-calculated start) as ratio
  from have
   group by id,group;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 14 Oct 2017 12:26:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Studio/Average-of-observations-between-multiple-dates-based-on-an/m-p/404213#M3727</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-10-14T12:26:21Z</dc:date>
    </item>
  </channel>
</rss>

