<?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: how can i merge records conditionally and average dose ? in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619972#M19474</link>
    <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;data have;&lt;BR /&gt;infile cards expandtabs truncover;&lt;BR /&gt;input USUBJID TRT $ (ASTDT ENDDT) (:date9.) DOSE DURATIONONDRUG;&lt;BR /&gt;format ASTDT ENDDT date9.;&lt;BR /&gt;cards;&lt;BR /&gt;102 cab 11-Apr-18 29-Apr-18 20 19&lt;BR /&gt;102 cab 30-Apr-18 30-Apr-18 10 1&lt;BR /&gt;102 cab 1-May-18 1-May-18 5 1&lt;BR /&gt;102 cab 2-May-18 13-May-18 15 12&lt;BR /&gt;102 cab 14-May-18 29-May-18 7.5 16&lt;BR /&gt;203 cab 21-Jun-18 4-Jul-18 60 14&lt;BR /&gt;203 cab 5-Jul-18 19-Jul-18 50 15&lt;BR /&gt;203 cab 20-Jul-18 25-Jul-18 40 6&lt;BR /&gt;203 cab 26-Jul-18 15-Aug-18 30 21&lt;BR /&gt;203 cab 16-Aug-18 12-Sep-18 25 28&lt;BR /&gt;203 cab 13-Sep-18 16-Sep-18 30 4&lt;BR /&gt;203 cab 17-Sep-18 18-Sep-18 25 2&lt;BR /&gt;203 cab 19-Sep-18 27-Sep-18 50 9&lt;BR /&gt;203 cab 28-Sep-18 15-Oct-18 80 18&lt;BR /&gt;203 cab 16-Oct-18 18-Oct-18 80 3&lt;BR /&gt;203 cab 19-Oct-18 22-Oct-18 80 4&lt;BR /&gt;203 cab 23-Oct-18 24-Oct-18 80 2&lt;BR /&gt;203 cab 25-Oct-18 26-Oct-18 50 3&lt;BR /&gt;204 can 20-Nov-18 24-Nov-18 35 5&lt;BR /&gt;;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;can we add condition so it stops merging to next records once the days reached &amp;gt;=7 count ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;in here the last two records&amp;nbsp; for subject 203 has 5 days after adding but that is fine since there is no extra records to merge.&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="forum question - 2.PNG" style="width: 536px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35618iCFED25B15CB4FB00/image-size/large?v=v2&amp;amp;px=999" role="button" title="forum question - 2.PNG" alt="forum question - 2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 25 Jan 2020 01:09:47 GMT</pubDate>
    <dc:creator>kk26</dc:creator>
    <dc:date>2020-01-25T01:09:47Z</dc:date>
    <item>
      <title>how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619265#M19363</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data work.forumq1 ;
  infile datalines dsd dlm='|' truncover;
  input USUBJID TRT :$3. ASTDT :F. ENDDT :F. DOSE DURATION_ON_DRUG ;
  format TRT $3. ASTDT date9. ENDDT date9. ;
  informat TRT $3. ASTDT date9. ENDDT date9. ;
  label USUBJID='USUBJID' TRT='TRT' ASTDT='ASTDT' ENDDT='ENDDT'
    DOSE='DOSE' DURATION_ON_DRUG='DURATION ON DRUG'
  ;
datalines4;
102|cab|21285|21303|20|19
102|cab|21304|21304|10|1
102|cab|21305|21305|5|1
102|cab|21306|21317|15|12
102|cab|21318|21333|7.5|16
203|cab|21356|21369|60|14
203|cab|21370|21384|50|15
203|cab|21385|21390|40|6
203|cab|21391|21411|30|21
203|cab|21412|21439|25|28
203|cab|21440|21443|30|4
203|cab|21444|21445|25|2
203|cab|21446|21454|50|9
;;;;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;i have this task to merge records if the duration on drug is &amp;lt;7 days to next record and do average dose while doing that. i tried to use retain and lag function but i am not able to perform this task. the goal is there should not be any records with duration on drug &amp;lt;7 days.&amp;nbsp; can someone help me with this ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;note: these dates should be continuous then perform the function.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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="forum question.PNG" style="width: 534px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35559iD7928427AD642654/image-size/large?v=v2&amp;amp;px=999" role="button" title="forum question.PNG" alt="forum question.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 20:40:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619265#M19363</guid>
      <dc:creator>kk26</dc:creator>
      <dc:date>2020-01-22T20:40:59Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619267#M19364</link>
      <description>&lt;P&gt;Many of us will not open Excel or other Microsoft Office documents because they are a security threat.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide the original data according to these instructions:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-data-AKA-generate/ta-p/258712" target="_blank"&gt;How to create a data step version of your data AKA generate sample data for forums&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 18:38:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619267#M19364</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-01-22T18:38:09Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619298#M19366</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;


data have;
infile cards expandtabs truncover;
input USUBJID	TRT $	(ASTDT	ENDDT) (:date9.)	DOSE	DURATIONONDRUG;
format ASTDT	ENDDT date9.;
cards;
102	cab	11-Apr-18	29-Apr-18	20	19
102	cab	30-Apr-18	30-Apr-18	10	1
102	cab	1-May-18	1-May-18	5	1
102	cab	2-May-18	13-May-18	15	12
102	cab	14-May-18	29-May-18	7.5	16
203	cab	21-Jun-18	4-Jul-18	60	14
203	cab	5-Jul-18	19-Jul-18	50	15
203	cab	20-Jul-18	25-Jul-18	40	6
203	cab	26-Jul-18	15-Aug-18	30	21
203	cab	16-Aug-18	12-Sep-18	25	28
203	cab	13-Sep-18	16-Sep-18	30	4
203	cab	17-Sep-18	18-Sep-18	25	2
203	cab	19-Sep-18	27-Sep-18	50	9
;

data want;
 do until(last.usubjid);
  set have;
  by usubjid;
  if DURATIONONDRUG&amp;lt;7 then do;
   t=sum(t,DURATIONONDRUG);
   continue;
  end;
  DURATIONONDRUG=sum(t,DURATIONONDRUG);
  t=.;
  output;
 end;
 drop t;
run;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jan 2020 20:07:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619298#M19366</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-22T20:07:47Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619316#M19367</link>
      <description>&lt;P&gt;You've described a simple task with 2 components:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Accumulate days on drugs&lt;/LI&gt;
&lt;LI&gt;If current days on drugs &amp;gt; 7 then output and reset the accumulator.&lt;/LI&gt;
&lt;/OL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
infile cards expandtabs truncover;
input USUBJID	TRT $	(ASTDT	ENDDT) (:date9.)	DOSE	DURATIONONDRUG;
format ASTDT	ENDDT date9.;
cards;
102	cab	11-Apr-18	29-Apr-18	20	19
102	cab	30-Apr-18	30-Apr-18	10	1
102	cab	1-May-18	1-May-18	5	1
102	cab	2-May-18	13-May-18	15	12
102	cab	14-May-18	29-May-18	7.5	16
203	cab	21-Jun-18	4-Jul-18	60	14
203	cab	5-Jul-18	19-Jul-18	50	15
203	cab	20-Jul-18	25-Jul-18	40	6
203	cab	26-Jul-18	15-Aug-18	30	21
203	cab	16-Aug-18	12-Sep-18	25	28
203	cab	13-Sep-18	16-Sep-18	30	4
203	cab	17-Sep-18	18-Sep-18	25	2
203	cab	19-Sep-18	27-Sep-18	50	9
;

data want (drop=_:);
  set have;
  by usubjid;
  _accum+durationondrug;
  if durationondrug&amp;gt;=7 ;
  durationondrug=_accum;
  output;
  _accum=0;
run;
  &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This program assumes that no ID ends with a record having&amp;nbsp; duration_on_drug&amp;lt;7, which would contaminate the accumulator for the next ID.&amp;nbsp; If you want to protect against that, then, just after the BY USUBJID statement add:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if first.usubjid=1 then _accum=0;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 22 Jan 2020 20:47:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619316#M19367</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-22T20:47:53Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619369#M19372</link>
      <description>&lt;P&gt;hi , thank you so much for helping me on this.&amp;nbsp;&lt;/P&gt;&lt;P&gt;still the start and end dates are not coming right , also the dose (need to be average)= sum of dose / days&lt;/P&gt;&lt;P&gt;for example:&lt;/P&gt;&lt;P&gt;&amp;nbsp;i have tried doing this but it did not work. look like i am doing something wrong.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&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="after.PNG" style="width: 553px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35566i8A529284B59ABB2C/image-size/large?v=v2&amp;amp;px=999" role="button" title="after.PNG" alt="after.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;data want;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; do until(last.usubjid);&lt;BR /&gt;set have;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;by usubjid;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if DURATIONONDRUG&amp;lt;7 then do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t=sum(t,DURATIONONDRUG);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; d=sum(d,dose);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; continue;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DURATIONONDRUG=sum(t,DURATIONONDRUG);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dose=sum(d,dose)/DURATIONONDRUG;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t=.;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; d=.;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;drop t d;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 23:12:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619369#M19372</guid>
      <dc:creator>kk26</dc:creator>
      <dc:date>2020-01-22T23:12:39Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619370#M19373</link>
      <description>&lt;P&gt;My apologies. I totally didn't even read the question or attempt to understand properly. Let me take a look a bit later once I am done at work. Pardon me!&lt;/P&gt;</description>
      <pubDate>Wed, 22 Jan 2020 23:15:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619370#M19373</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-22T23:15:49Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619371#M19374</link>
      <description>hi , i am glad that i asked my first question on this forum. i have learn something new today. you are awesome! please check my reply to &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;.</description>
      <pubDate>Wed, 22 Jan 2020 23:21:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619371#M19374</guid>
      <dc:creator>kk26</dc:creator>
      <dc:date>2020-01-22T23:21:16Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619379#M19376</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/263529"&gt;@kk26&lt;/a&gt;&amp;nbsp; Here is a simple and easy two step solution.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. Groupe the &amp;lt;7 as 1's and the rest as 0's as boolean/binary grouping into a temp dataset&lt;/P&gt;
&lt;P&gt;2. sum the group of 1s&lt;/P&gt;
&lt;P&gt;3. Output the summed 1s as collapsed into one and 0's groups as all&lt;/P&gt;
&lt;P&gt;4. Notsorted is an option to group 1's and 0's in varying order, you will notice the logic when you visualize the temp dataset.&lt;/P&gt;
&lt;P&gt;5. Read slowly and carefully. I hope you will grasp the idea&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards expandtabs truncover;
input USUBJID	TRT $	(ASTDT	ENDDT) (:date9.)	DOSE	DURATIONONDRUG;
format ASTDT	ENDDT date9.;
cards;
102	cab	11-Apr-18	29-Apr-18	20	19
102	cab	30-Apr-18	30-Apr-18	10	1
102	cab	1-May-18	1-May-18	5	1
102	cab	2-May-18	13-May-18	15	12
102	cab	14-May-18	29-May-18	7.5	16
203	cab	21-Jun-18	4-Jul-18	60	14
203	cab	5-Jul-18	19-Jul-18	50	15
203	cab	20-Jul-18	25-Jul-18	40	6
203	cab	26-Jul-18	15-Aug-18	30	21
203	cab	16-Aug-18	12-Sep-18	25	28
203	cab	13-Sep-18	16-Sep-18	30	4
203	cab	17-Sep-18	18-Sep-18	25	2
203	cab	19-Sep-18	27-Sep-18	50	9
;

data temp;
 set have;
 by USUBJID ;
 n=DURATIONONDRUG&amp;lt;7;
 if lag(n)=1 and lag(USUBJID)=USUBJID and n=0 or n=1 then n1=1;
 else n1=0;
run;

data want;
 set temp;
 by USUBJID n1 notsorted;
 retain dt;
 if first.n1 and n1 then do;
  dt=astdt;
  s=0;
  s1=0;
 end;
 s+DURATIONONDRUG;
 s1+dose;
 if last.n1 or n1=0;
  if n1 then do;
   astdt=dt;
   DURATIONONDRUG=s;
   ndays=intck('day',astdt,ENDDT)+1;
   dose=s1/ndays;
  end;
drop n: s: dt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Jan 2020 00:14:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619379#M19376</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-23T00:14:53Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619387#M19379</link>
      <description>hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;,&lt;BR /&gt;this is amazing, it worked. i am trying to understand your logic and i will try and improve my own programming. thank you so much!</description>
      <pubDate>Thu, 23 Jan 2020 00:48:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619387#M19379</guid>
      <dc:creator>kk26</dc:creator>
      <dc:date>2020-01-23T00:48:26Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619972#M19474</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE class=" language-sas"&gt;data have;&lt;BR /&gt;infile cards expandtabs truncover;&lt;BR /&gt;input USUBJID TRT $ (ASTDT ENDDT) (:date9.) DOSE DURATIONONDRUG;&lt;BR /&gt;format ASTDT ENDDT date9.;&lt;BR /&gt;cards;&lt;BR /&gt;102 cab 11-Apr-18 29-Apr-18 20 19&lt;BR /&gt;102 cab 30-Apr-18 30-Apr-18 10 1&lt;BR /&gt;102 cab 1-May-18 1-May-18 5 1&lt;BR /&gt;102 cab 2-May-18 13-May-18 15 12&lt;BR /&gt;102 cab 14-May-18 29-May-18 7.5 16&lt;BR /&gt;203 cab 21-Jun-18 4-Jul-18 60 14&lt;BR /&gt;203 cab 5-Jul-18 19-Jul-18 50 15&lt;BR /&gt;203 cab 20-Jul-18 25-Jul-18 40 6&lt;BR /&gt;203 cab 26-Jul-18 15-Aug-18 30 21&lt;BR /&gt;203 cab 16-Aug-18 12-Sep-18 25 28&lt;BR /&gt;203 cab 13-Sep-18 16-Sep-18 30 4&lt;BR /&gt;203 cab 17-Sep-18 18-Sep-18 25 2&lt;BR /&gt;203 cab 19-Sep-18 27-Sep-18 50 9&lt;BR /&gt;203 cab 28-Sep-18 15-Oct-18 80 18&lt;BR /&gt;203 cab 16-Oct-18 18-Oct-18 80 3&lt;BR /&gt;203 cab 19-Oct-18 22-Oct-18 80 4&lt;BR /&gt;203 cab 23-Oct-18 24-Oct-18 80 2&lt;BR /&gt;203 cab 25-Oct-18 26-Oct-18 50 3&lt;BR /&gt;204 can 20-Nov-18 24-Nov-18 35 5&lt;BR /&gt;;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;can we add condition so it stops merging to next records once the days reached &amp;gt;=7 count ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;in here the last two records&amp;nbsp; for subject 203 has 5 days after adding but that is fine since there is no extra records to merge.&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="forum question - 2.PNG" style="width: 536px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35618iCFED25B15CB4FB00/image-size/large?v=v2&amp;amp;px=999" role="button" title="forum question - 2.PNG" alt="forum question - 2.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jan 2020 01:09:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619972#M19474</guid>
      <dc:creator>kk26</dc:creator>
      <dc:date>2020-01-25T01:09:47Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619973#M19475</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/263529"&gt;@kk26&lt;/a&gt;&amp;nbsp; about to doze off, as i'm very tired. Can i take a look right in the morning(Eastern time US) if it's not too urgent? I'm just knackered. I could barely open my eyes&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jan 2020 01:19:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619973#M19475</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-25T01:19:01Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619974#M19476</link>
      <description>hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt; ,&lt;BR /&gt;whenever you have time! thank you so much!</description>
      <pubDate>Sat, 25 Jan 2020 01:37:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/619974#M19476</guid>
      <dc:creator>kk26</dc:creator>
      <dc:date>2020-01-25T01:37:37Z</dc:date>
    </item>
    <item>
      <title>Re: how can i merge records conditionally and average dose ?</title>
      <link>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/620023#M19481</link>
      <description>&lt;P&gt;HI&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/263529"&gt;@kk26&lt;/a&gt;&amp;nbsp; First off, It appears the values in your latest HAVE(Before) and WANT(After) are not consistent. For example, the ENDT value in before 26-Oct-18&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;203 cab 25-Oct-18 26-Oct-18 50 3&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;And in the after equivalent it is 27-Oct-18. Also new USSubjid in &lt;STRONG&gt;before&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;
&lt;LI-CODE lang="sas"&gt;204 can 20-Nov-18 24-Nov-18 35 5&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;and the equivalent in after is 301. Though trivial, I failed to notice until I was wondering how SAS can get basic math wrong. So, please going forward verify the details &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Secondly, I have a feeling, AFTER for&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;204 can 20-Nov-18 24-Nov-18 35 5&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;should ideally be the average dose 7 in place of 35 &lt;U&gt;&lt;EM&gt;to be in consistent with the logic to apply for&amp;nbsp; the previous i.e.&lt;/EM&gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;203 cab 23-Oct-18 24-Oct-18 80 2
203 cab 25-Oct-18 27-Oct-18 50 3&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;resulting in&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;203	cab	23OCT2018	27OCT2018	26	5&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So assuming the above makes sense, the following solution would meet it&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards expandtabs truncover;
input USUBJID TRT $ (ASTDT ENDDT) (:date9.) DOSE DURATIONONDRUG;
format ASTDT ENDDT date9.;
cards;
102 cab 11-Apr-18 29-Apr-18 20 19
102 cab 30-Apr-18 30-Apr-18 10 1
102 cab 1-May-18 1-May-18 5 1
102 cab 2-May-18 13-May-18 15 12
102 cab 14-May-18 29-May-18 7.5 16
203 cab 21-Jun-18 4-Jul-18 60 14
203 cab 5-Jul-18 19-Jul-18 50 15
203 cab 20-Jul-18 25-Jul-18 40 6
203 cab 26-Jul-18 15-Aug-18 30 21
203 cab 16-Aug-18 12-Sep-18 25 28
203 cab 13-Sep-18 16-Sep-18 30 4
203 cab 17-Sep-18 18-Sep-18 25 2
203 cab 19-Sep-18 27-Sep-18 50 9
203 cab 28-Sep-18 15-Oct-18 80 18
203 cab 16-Oct-18 18-Oct-18 80 3
203 cab 19-Oct-18 22-Oct-18 80 4
203 cab 23-Oct-18 24-Oct-18 80 2
203 cab 25-Oct-18 27-Oct-18 50 3
204 can 20-Nov-18 24-Nov-18 35 5
;


data want;
 do until(last.USUBJID);
  set have;
  by USUBJID;
  if DURATIONONDRUG&amp;lt;7  then do;
   if dt=. then   dt=astdt;
   s=sum(s,DURATIONONDRUG);
   s1=sum(s1,dose);
   if s&amp;gt;=7 or last.USUBJID then do;
    astdt=dt;
    ndays=intck('day',astdt,ENDDT)+1;
    dose=s1/ndays;
	DURATIONONDRUG=s;
	output;
	call missing(of s:,dt);
   end;
   continue;
  end;
  else if s and dt then do;
   s=sum(s,DURATIONONDRUG);
   s1=sum(s1,dose);
   astdt=dt;
   DURATIONONDRUG=s;
   ndays=intck('day',astdt,ENDDT)+1;
   dose=s1/ndays;
   call missing(of s:,dt);
  end;
  output;
 end;
 drop s: dt ndays;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Best Regards!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 25 Jan 2020 19:23:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/how-can-i-merge-records-conditionally-and-average-dose/m-p/620023#M19481</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-01-25T19:23:24Z</dc:date>
    </item>
  </channel>
</rss>

