<?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: creating variable based on observation from multiple variables within the time frame in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583518#M166112</link>
    <description>&lt;P&gt;Yes, thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Attached is the date I have.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ultimately, I would like to create variable: Course_of_treatment and if possible, drugs_within_COT.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Date Therapy&lt;BR /&gt;1 10/1/2011&amp;nbsp; &amp;nbsp;drug1&lt;BR /&gt;1 10/16/2012 drug1&lt;BR /&gt;1 10/30/2012 drug2&lt;BR /&gt;1 10/30/2012 drug2&lt;BR /&gt;1 11/13/2012 drug3&lt;BR /&gt;1 11/27/2012 drug2&lt;BR /&gt;1 12/11/2012 drug2&lt;BR /&gt;1 12/11/2012 drug3&lt;BR /&gt;1 12/18/2012 drug2&lt;BR /&gt;1 12/24/2012 drug3&lt;BR /&gt;1 12/31/2012 drug2&lt;BR /&gt;1 1/8/2013 drug3&lt;BR /&gt;1 1/15/2013 drug2&lt;BR /&gt;1 1/22/2013 drug2&lt;BR /&gt;1 1/29/2013 drug3&lt;BR /&gt;1 2/5/2013 drug3&lt;BR /&gt;1 2/12/2013 drug1&lt;BR /&gt;1 2/19/2013 drug4&lt;BR /&gt;1 2/26/2013 drug4&lt;BR /&gt;1 3/5/2013 drug4&lt;/P&gt;</description>
    <pubDate>Fri, 23 Aug 2019 15:41:59 GMT</pubDate>
    <dc:creator>yongheelee1212</dc:creator>
    <dc:date>2019-08-23T15:41:59Z</dc:date>
    <item>
      <title>creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583492#M166107</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Dear SAS users,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am new to longitudinal data analysis and especially when it comes to manipulating date related issue.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have fairly complicated issue, and any tip along the way is much appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data that contains ID, date, and therapy, and my ultimate goal is to create course of treatment.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Course of treatment inclement with certain condition:&lt;/P&gt;&lt;P&gt;1. if drug that is not part of previous COT is observed and&lt;/P&gt;&lt;P&gt;2. the drug is observed within 30 days&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So for instance, drug2 is observed at 10/16/2012 and observed twice within 30 days. So, this is the new course of treatment.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then all_drug within that course_of_treatment would be all drugs observed during 30 days from first day of course_of_treatment,&amp;nbsp;&lt;/P&gt;&lt;P&gt;which would be drug 2 and 3.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Course_of_treatment continues until drug4 is observed.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any comment would be great and thank you!&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output I want looks as below&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="Untitled picture.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31978i96040D65C1F2D8A4/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled picture.png" alt="Untitled picture.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 14:48:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583492#M166107</guid>
      <dc:creator>yongheelee1212</dc:creator>
      <dc:date>2019-08-23T14:48:03Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583507#M166109</link>
      <description>&lt;P&gt;Which variables there do you start with and which do you need to calculate?&lt;/P&gt;
&lt;P&gt;Can you please provide sample data as text, especially if want example code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You would use a SQL merge here because you can merge on a range then to find occurrences within 30 days. You would first merge the dataset with itself to determine that. Then once you find the counts, you could create episodes/groups. Then use CATX() to combine them to find the course of treatment over time. You'll also want to think about how exactly you want that output data set to look like.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285348"&gt;@yongheelee1212&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dear SAS users,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am new to longitudinal data analysis and especially when it comes to manipulating date related issue.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have fairly complicated issue, and any tip along the way is much appreciated.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a data that contains ID, date, and therapy, and my ultimate goal is to create course of treatment.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Course of treatment inclement with certain condition:&lt;/P&gt;
&lt;P&gt;1. if drug that is not part of previous COT is observed and&lt;/P&gt;
&lt;P&gt;2. the drug is observed within 30 days&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for instance, drug2 is observed at 10/16/2012 and observed twice within 30 days. So, this is the new course of treatment.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then all_drug within that course_of_treatment would be all drugs observed during 30 days from first day of course_of_treatment,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which would be drug 2 and 3.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Course_of_treatment continues until drug4 is observed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any comment would be great and thank you!&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The output I want looks as below&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="Untitled picture.png" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/31978i96040D65C1F2D8A4/image-size/large?v=v2&amp;amp;px=999" role="button" title="Untitled picture.png" alt="Untitled picture.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 15:12:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583507#M166109</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-23T15:12:40Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583518#M166112</link>
      <description>&lt;P&gt;Yes, thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Attached is the date I have.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Ultimately, I would like to create variable: Course_of_treatment and if possible, drugs_within_COT.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID Date Therapy&lt;BR /&gt;1 10/1/2011&amp;nbsp; &amp;nbsp;drug1&lt;BR /&gt;1 10/16/2012 drug1&lt;BR /&gt;1 10/30/2012 drug2&lt;BR /&gt;1 10/30/2012 drug2&lt;BR /&gt;1 11/13/2012 drug3&lt;BR /&gt;1 11/27/2012 drug2&lt;BR /&gt;1 12/11/2012 drug2&lt;BR /&gt;1 12/11/2012 drug3&lt;BR /&gt;1 12/18/2012 drug2&lt;BR /&gt;1 12/24/2012 drug3&lt;BR /&gt;1 12/31/2012 drug2&lt;BR /&gt;1 1/8/2013 drug3&lt;BR /&gt;1 1/15/2013 drug2&lt;BR /&gt;1 1/22/2013 drug2&lt;BR /&gt;1 1/29/2013 drug3&lt;BR /&gt;1 2/5/2013 drug3&lt;BR /&gt;1 2/12/2013 drug1&lt;BR /&gt;1 2/19/2013 drug4&lt;BR /&gt;1 2/26/2013 drug4&lt;BR /&gt;1 3/5/2013 drug4&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 15:41:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583518#M166112</guid>
      <dc:creator>yongheelee1212</dc:creator>
      <dc:date>2019-08-23T15:41:59Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583523#M166116</link>
      <description>&lt;P&gt;Why isn't drug1 the first COT for ID1?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately I likely don't have enough time today to wade into a problem this complex but this should get you (or anyone else started at least &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;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
informat ID $1. date mmddyy10. therapy $8.;
format date date9.;
input ID $ Date Therapy $;
cards;
1 10/1/2011   drug1
1 10/16/2012 drug1
1 10/30/2012 drug2
1 10/30/2012 drug2
1 11/13/2012 drug3
1 11/27/2012 drug2
1 12/11/2012 drug2
1 12/11/2012 drug3
1 12/18/2012 drug2
1 12/24/2012 drug3
1 12/31/2012 drug2
1 1/8/2013 drug3
1 1/15/2013 drug2
1 1/22/2013 drug2
1 1/29/2013 drug3
1 2/5/2013 drug3
1 2/12/2013 drug1
1 2/19/2013 drug4
1 2/26/2013 drug4
1 3/5/2013 drug4
2 10/1/2011   drug1
2 10/16/2012 drug1
2 10/30/2012 drug2
2 10/30/2012 drug2
2 11/13/2012 drug3
2 11/27/2012 drug2
2 12/11/2012 drug2
2 12/11/2012 drug3
2 12/18/2012 drug2
2 12/24/2012 drug3
3 12/31/2012 drug2
3 1/8/2013 drug3
3 1/15/2013 drug2
3 1/22/2013 drug2
3 1/29/2013 drug3
3 2/5/2013 drug3
3 2/12/2013 drug1
3 2/19/2013 drug4
3 2/26/2013 drug4
3 3/5/2013 drug4
;;;;

proc sql;
create table counts as
select *, 
	(select count(*) as num 
	from have as h1_x 
	where h1_x.id=h1.id 
	and h1.therapy = h1_x.therapy 
	and h1_x.date between h1.date and (h1.date+30))
		as count
from have as h1
order by 1, 3, 2;
quit;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285348"&gt;@yongheelee1212&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Yes, thank you.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Attached is the date I have.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Ultimately, I would like to create variable: Course_of_treatment and if possible, drugs_within_COT.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;ID Date Therapy&lt;BR /&gt;1 10/1/2011&amp;nbsp; &amp;nbsp;drug1&lt;BR /&gt;1 10/16/2012 drug1&lt;BR /&gt;1 10/30/2012 drug2&lt;BR /&gt;1 10/30/2012 drug2&lt;BR /&gt;1 11/13/2012 drug3&lt;BR /&gt;1 11/27/2012 drug2&lt;BR /&gt;1 12/11/2012 drug2&lt;BR /&gt;1 12/11/2012 drug3&lt;BR /&gt;1 12/18/2012 drug2&lt;BR /&gt;1 12/24/2012 drug3&lt;BR /&gt;1 12/31/2012 drug2&lt;BR /&gt;1 1/8/2013 drug3&lt;BR /&gt;1 1/15/2013 drug2&lt;BR /&gt;1 1/22/2013 drug2&lt;BR /&gt;1 1/29/2013 drug3&lt;BR /&gt;1 2/5/2013 drug3&lt;BR /&gt;1 2/12/2013 drug1&lt;BR /&gt;1 2/19/2013 drug4&lt;BR /&gt;1 2/26/2013 drug4&lt;BR /&gt;1 3/5/2013 drug4&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 15:59:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583523#M166116</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-23T15:59:23Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583528#M166118</link>
      <description>&lt;P&gt;thank you so much!&lt;/P&gt;&lt;P&gt;drug1, the first observation is not COT1 because one of the criteria is that it should've been given at least twice within 30 days.&amp;nbsp;&lt;/P&gt;&lt;P&gt;First one was given at 10/1/2011 and the next one is at 10/16/2012. So it doesn't meet the criteria.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;much appreciated for your help with start of the approach!&lt;/P&gt;&lt;P&gt;time to do some research and dig into the problem!&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 16:05:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583528#M166118</guid>
      <dc:creator>yongheelee1212</dc:creator>
      <dc:date>2019-08-23T16:05:07Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583531#M166119</link>
      <description>I know that this question has been asked and answered on here many times. If you do some searching you should find better answers that are much closer to what you need than this.</description>
      <pubDate>Fri, 23 Aug 2019 16:08:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583531#M166119</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-23T16:08:43Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583588#M166136</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285348"&gt;@yongheelee1212&lt;/a&gt;&amp;nbsp; Looks like you are new to SAS communities. So hearty welcome!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One request is, you could explain your requirement in a more comprehensible detail for anyone to understand and attempt.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;I have a data that contains ID, date, and therapy,&lt;/EM&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Requirement&amp;nbsp;&lt;SPAN&gt;goal is to create course of treatment.&amp;nbsp; &lt;/SPAN&gt;&lt;/EM&gt;so that is COT variable, so what's in with count_30days and&amp;nbsp; part_of_previous_COT variable???&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Objective : To&lt;SPAN&gt;&amp;nbsp;increment&amp;nbsp;Course of treatment values with certain condition&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;1. if drug that is not part of &lt;FONT color="#FF0000"&gt;&lt;EM&gt;&lt;STRONG&gt;previous COT/*what is this first? How is this computed or derived?*/&lt;/STRONG&gt; &lt;/EM&gt;&lt;/FONT&gt;is observed and&lt;/P&gt;
&lt;P&gt;2. the drug is observed within 30 days&amp;nbsp;&lt;STRONG&gt;&lt;FONT color="#FF0000"&gt;&lt;EM&gt; /*within 30 days of what????from when/where? */&lt;/EM&gt;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000000"&gt;Please explain the logic relating to sample OUTPUT(variables) how is it derived? The flow should be convenient to grasp even for some dumb folks like me. Thank you!&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 19:16:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583588#M166136</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-23T19:16:58Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583610#M166146</link>
      <description>&lt;P&gt;&lt;A href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205" target="_self"&gt;Dear novinosrin&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold" style="box-sizing: inherit;"&gt;My apology if my requirements were ambiguous. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold" style="box-sizing: inherit;"&gt;TO answer your question, &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="login-bold" style="box-sizing: inherit;"&gt;"&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Objective : To&lt;SPAN style="box-sizing: inherit; line-height: 1.2;"&gt;&amp;nbsp;increment&amp;nbsp;Course of treatment values with certain condition&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;1. if drug that is not part of &lt;FONT color="#ff0000"&gt;&lt;EM&gt;previous COT/*what is this first? How is this computed or derived?*/ &lt;/EM&gt;&lt;/FONT&gt;is observed and&lt;/P&gt;&lt;P&gt;2. the drug is observed within 30 days&amp;nbsp;&lt;FONT color="#ff0000"&gt;&lt;EM&gt; /*within 30 days of what????from when/where? */&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;"&lt;/P&gt;&lt;P&gt;Course of Treatment is defined if the therapy given is not part of the previous course of treatment and if the drug is given at least twice in 30 days.&amp;nbsp;So for instance, at the beginning, course of treatment is none. so first requirement is already satisfied but the drug has to be given at least twice in 30 days. Drug 1 from first and second observation would not be first course of treatment because it does not meet the second requirement.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So as you can see from the top picture, drug 2(10/30/2012) would be beginning of course of treatment, hence cot1. The drugs observed from 10/30/2012 to 11/30/2012 is drug 2 and drug 3, hence drugs associated with cot1 would be drug2 and drug3).&amp;nbsp;&lt;/P&gt;&lt;P&gt;cot1 continues until new drug is observed consecutively in 30 days, which is drug4 on 3/5/2013. hence, cot2 starts on 3/5/2013.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope that explains better, let me know if you have more question.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So far, I tried to do this,&amp;nbsp;&lt;/P&gt;&lt;P&gt;1. create Count_30_days (which shows number of drug given within 30 day of the date that drug is given.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So my intended strategy is to do:&lt;/P&gt;&lt;P&gt;*** identify whether the therapy given on specific date is part of COT***&lt;/P&gt;&lt;P&gt;part_of_therapy = yes&amp;nbsp;&lt;/P&gt;&lt;P&gt;if (therapy in(therapy) BETWEEN first.date of COT) and first.date of COT+30ays&lt;/P&gt;&lt;P&gt;*** then based on that do something like ***&lt;/P&gt;&lt;P&gt;course of treatment +1&amp;nbsp;&lt;/P&gt;&lt;P&gt;if&amp;nbsp;&lt;/P&gt;&lt;P&gt;course_30_days&amp;gt;=2 (happens at least twice in 30 days) and part_of_therapy=yes (if the therapy is part of the treatment).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To do this, I think I need to know how to identify observations in a variable within specific date range, but that's something I just couldn't figure out no matter what.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 21:18:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583610#M166146</guid>
      <dc:creator>yongheelee1212</dc:creator>
      <dc:date>2019-08-23T21:18:14Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583612#M166147</link>
      <description>&lt;P&gt;Thank you for the response!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I actually tried spending many hours looking up SAS communities and other help websites. I was able to find similar looking questions but not as same as mine.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So my intended strategy is to do:&lt;/P&gt;&lt;P&gt;*** identify whether the therapy given on specific date is part of COT***&lt;/P&gt;&lt;P&gt;part_of_therapy = yes&amp;nbsp;&lt;/P&gt;&lt;P&gt;if (therapy in(therapy) BETWEEN first.date of COT) and first.date of COT+30ays&lt;/P&gt;&lt;P&gt;*** then based on that do something like ***&lt;/P&gt;&lt;P&gt;course of treatment +1&amp;nbsp;&lt;/P&gt;&lt;P&gt;if&amp;nbsp;&lt;/P&gt;&lt;P&gt;course_30_days&amp;gt;=2 (happens at least twice in 30 days) and part_of_therapy=yes (if the therapy is part of the treatment).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;To do this, I think I need to know &lt;STRONG&gt;how to identify observations in a variable within specific date range and perhaps create variable based on observations from other variable in certain timeframe&lt;/STRONG&gt;, but that's something I just couldn't figure out no matter what.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 21:21:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583612#M166147</guid>
      <dc:creator>yongheelee1212</dc:creator>
      <dc:date>2019-08-23T21:21:08Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583613#M166148</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285348"&gt;@yongheelee1212&lt;/a&gt;&amp;nbsp; &amp;nbsp;Nothing to apologize. You didn't know. Sorry, I'm about to run to catch my Bus to get home. I'll see if I can get online from home and attempt from home.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 21:21:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583613#M166148</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-23T21:21:16Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583614#M166149</link>
      <description>&lt;P&gt;Thank you!&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you have any other suggestions on how I should approach this, I am open to all ideas!&amp;nbsp;&lt;/P&gt;&lt;P&gt;have a wonderful Friday!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 21:23:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583614#M166149</guid>
      <dc:creator>yongheelee1212</dc:creator>
      <dc:date>2019-08-23T21:23:52Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583617#M166151</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285348"&gt;@yongheelee1212&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To do this, I think I need to know &lt;STRONG&gt;how to identify observations in a variable within specific date range and perhaps create variable based on observations from other variable in certain timeframe&lt;/STRONG&gt;, but that's something I just couldn't figure out no matter what.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The in line sql above shows how to look up data based on a range.&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 21:38:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583617#M166151</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-23T21:38:52Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583642#M166161</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285348"&gt;@yongheelee1212&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Something I am missing in ....&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Course of Treatment is defined if the therapy given is not part of the previous course of treatment and if the drug is given at least twice in 30 days.&amp;nbsp;So for instance, at the beginning, course of treatment is none. so first requirement is already satisfied but the drug has to be given at least twice in 30 days. Drug 1 from first and second observation would not be first course of treatment because it does not meet the second requirement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So as you can see from the top picture, drug 2(10/30/2012) would be beginning of course of treatment, hence cot1. The drugs observed from 10/30/2012 to 11/30/2012 is drug 2 and drug 3, hence drugs associated with cot1 would be drug2 and drug3).&amp;nbsp;&lt;/P&gt;
&lt;P&gt;cot1 continues until new drug is observed consecutively in 30 days, which is drug4 on 3/5/2013. hence, cot2 starts on 3/5/2013.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;COT1 starts with drug1 on 10/16/2012 continues with drug2 and drug3 with 30-day gaps through 2/5/2013. On 2/12/2013 drug1 comes back. Drug4 joins with drug1 on 2/19/2013.&lt;/P&gt;
&lt;P&gt;Questions are:&lt;/P&gt;
&lt;P&gt;[1] Hence, COT1 joining with drug4 is deemed to be a new sequence. Is this right?&lt;/P&gt;
&lt;P&gt;[2] why do you say Cot2 starts on 3/5/2013 when Drug4 was started earlier on 2/19/2013?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2019 09:20:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583642#M166161</guid>
      <dc:creator>KachiM</dc:creator>
      <dc:date>2019-08-24T09:20:04Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583706#M166189</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285348"&gt;@yongheelee1212&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me try to pen my understanding in points and see the flow.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My observation&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let's start by&amp;nbsp; reading&amp;nbsp; record(obs)&amp;nbsp; 1.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. First off, Date column has the dates when a drug or sometimes more than a 1 drug administered?&lt;/P&gt;
&lt;P&gt;2. Point 1 would ideally make sense for the Course_of_treatment &lt;EM&gt;&lt;STRONG&gt;sequence&lt;/STRONG&gt; &lt;/EM&gt;to start right from record 1 or in other words from the date of the &lt;STRONG&gt;&lt;EM&gt;start of treatment&lt;/EM&gt;&lt;/STRONG&gt;, which you have blank.&lt;/P&gt;
&lt;P&gt;3. Drug1 of obs 1 will of course not be part of previous treatment because that's the start.&lt;/P&gt;
&lt;P&gt;4.&lt;STRONG&gt; Look up(Look ahead&lt;/STRONG&gt; in this case) 30 days from 10/1/2011&lt;STRONG&gt;(inclusive)&lt;/STRONG&gt; to (10/1/2011 +30 days) i.e date to&amp;nbsp; intnx('days',date,30)&amp;nbsp; &amp;nbsp;precisely. This should be done for each record within a patient ID i.e BY GROUP&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;a.id =b.id&lt;/STRONG&gt; and &lt;STRONG&gt;a.date&amp;lt;=b.date&amp;lt;=intnx('days',a.date,30)&lt;/STRONG&gt; in a would be SQL syntax and get all the drugs.&lt;/P&gt;
&lt;P&gt;5. Since &lt;STRONG&gt;&amp;lt;=&amp;nbsp;&lt;/STRONG&gt;makes it inclusive, if&amp;nbsp; &lt;STRONG&gt;a.date&amp;lt;=b.date&amp;lt;=intnx('days',a.date,30)&amp;nbsp;&lt;/STRONG&gt;is found true 2 times(twice) or more then conditional check 1 is satisfied. Then if &lt;EM&gt;&lt;STRONG&gt;any&lt;/STRONG&gt; &lt;/EM&gt;of the Drug found true happens to be part of the previous treatment, club all of the found &lt;STRONG&gt;unique&lt;/STRONG&gt; drugs to the previous treatment to which it is found.&lt;/P&gt;
&lt;P&gt;6. Repeat the same process for every record until last.patient_id i.e last record of the BY GROUP patient id.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;7. Follow same suit for all Patients or Patient ID aka exclusive BY GROUP.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Does this make sense closer to your&amp;nbsp; need?.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Part Logic for count_30 days of a drug for each observation --&amp;gt;&lt;/P&gt;
&lt;P&gt;Lets do some initial findings using SQL applying the LOOK UP to get the dates and see if the Drug was found twice or more. This will help us understand whether our thought process in applying the logic for LOOK UP is right or not&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID Date :mmddyy10. Therapy $;
format date mmddyy10.;
cards;
1 10/1/2011   drug1
1 10/16/2012 drug1
1 10/30/2012 drug2
1 10/30/2012 drug2
1 11/13/2012 drug3
1 11/27/2012 drug2
1 12/11/2012 drug2
1 12/11/2012 drug3
1 12/18/2012 drug2
1 12/24/2012 drug3
1 12/31/2012 drug2
1 1/8/2013 drug3
1 1/15/2013 drug2
1 1/22/2013 drug2
1 1/29/2013 drug3
1 2/5/2013 drug3
1 2/12/2013 drug1
1 2/19/2013 drug4
1 2/26/2013 drug4
1 3/5/2013 drug4
;
/*To Test the look up by remvoe the duplicate dates for the same therapy to 
know if our expected count is right. of course later in our solution code, 
we can include the full data.
Example 1 10/30/2012 drug2
1 10/30/2012 drug2  occurs twice in have 
*/

proc sort data=have out=_have nodupkey;
by id therapy date;
run;

/*please notice the below in the code*/
/*on a.id =b.id and a.date&amp;lt;=b.date&amp;lt;=intnx('days',a.date,30)*/
/*a.therapy=b.therapy as c_to_count.This is boolean expression resulting in 1,0's*/
/*If you sum(c_to_count), you would get count */

proc sql;
create table Count_30 as
select a.*,a.date as start_date format=mmddyy10.,intnx('days',a.date,30) as end_date format=mmddyy10., a.therapy=b.therapy as c_to_count
from _have a left join _have b
on a.id =b.id and a.date&amp;lt;=b.date&amp;lt;=intnx('days',a.date,30)
group by a.id,a.date,a.Therapy
order by a.id,a.date,b.date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now, run the above, look at the results. If this study gets us the right direction, we can apply whether the found Drugs within the 30 period was part of the existing course_of_treatment. That will involve a a look up in all_drug_30 and club these if true or increment a new one.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All these can be done neatly using Hash for a final complete solution. But the above&amp;nbsp; initial study is just&amp;nbsp; to see if our understanding of the logic is in the same page if that&amp;nbsp; helps. In essence, I get the feeling that while I am not understanding well or taking time, the coding part is merely gonna take 5 or 10 mins.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2019 20:39:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583706#M166189</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-24T20:39:10Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583804#M166231</link>
      <description>&lt;P&gt;Dear datasp.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;1, you are correct.&amp;nbsp;&lt;/P&gt;&lt;P&gt;2. my apology. you are right, cot2 should start on&amp;nbsp;&lt;SPAN&gt;2/19/2013,, there was an mismatch in original picture and data in text I presented&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Aug 2019 02:18:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583804#M166231</guid>
      <dc:creator>yongheelee1212</dc:creator>
      <dc:date>2019-08-26T02:18:21Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583806#M166233</link>
      <description>&lt;P&gt;Thank you for taking your step with me!! Very much appreciated for your time&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Let's start by&amp;nbsp; reading&amp;nbsp; record(obs)&amp;nbsp; 1.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;First off, Date column has the dates when a drug or sometimes more than a 1 drug administered?&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Yes!&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Point 1 would ideally make sense for the Course_of_treatment&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;sequence&lt;/EM&gt;&lt;/STRONG&gt;to start right from record 1 or in other words from the date of the&amp;nbsp;&lt;EM&gt;&lt;STRONG&gt;start of treatment&lt;/STRONG&gt;&lt;/EM&gt;, which you have blank.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Yes!&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Drug1 of obs 1 will of course not be part of previous treatment because that's the start.&lt;/LI&gt;&lt;LI&gt;&lt;STRONG&gt;Look up(Look ahead&lt;/STRONG&gt;in this case) 30 days from 10/1/2011&lt;STRONG&gt;(inclusive)&lt;/STRONG&gt;&amp;nbsp;to (10/1/2011 +30 days) i.e date to&amp;nbsp; intnx('days',date,30)&amp;nbsp; &amp;nbsp;precisely. This should be done for each record within a patient ID i.e BY GROUP&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;STRONG&gt;a.id =b.id&lt;/STRONG&gt;&amp;nbsp;and&amp;nbsp;&lt;STRONG&gt;a.date&amp;lt;=b.date&amp;lt;=intnx('days',a.date,30)&lt;/STRONG&gt;&amp;nbsp;in a would be SQL syntax and get all the drugs.&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Since&amp;nbsp;&lt;STRONG&gt;&amp;lt;=&amp;nbsp;&lt;/STRONG&gt;makes it inclusive, if&amp;nbsp;&amp;nbsp;&lt;STRONG&gt;a.date&amp;lt;=b.date&amp;lt;=intnx('days',a.date,30)&amp;nbsp;&lt;/STRONG&gt;is found true 2 times(twice) or more then conditional check 1 is satisfied. Then if&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;any&lt;/EM&gt;&lt;/STRONG&gt;of the Drug found true happens to be part of the previous treatment, club all of the found&amp;nbsp;&lt;STRONG&gt;unique&lt;/STRONG&gt;drugs to the previous treatment to which it is found.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Yes, if the drug is found true 2 times or more then conditional check 1 is satisfied (drug is given twice in 30 days, which &lt;FONT color="#FF0000"&gt;marks the start of the COT).&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;I am not quite sure what this part means “&lt;/STRONG&gt;Then if&amp;nbsp;&lt;STRONG&gt;&lt;EM&gt;any&lt;/EM&gt;&lt;/STRONG&gt;&lt;EM&gt;&amp;nbsp;&lt;/EM&gt;of the Drug found true happens to be part of the previous treatment, club all of the found&amp;nbsp;&lt;STRONG&gt;unique&lt;/STRONG&gt;&amp;nbsp;drugs to the previous treatment to which it is found”.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;If any of the drug found within 30 days of start of treatment, that drug is part of COT. &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;So, for instance, based on your data set, &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;From 10/30/2012 to 11/29/2012, drug2 is found twice. &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Thus, 10/30/2012 is the start of COT1. &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Any drug found from 10/30/2012 – 11/29/2012 is COT1 (drug2, drug3).&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Then,&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;02/19/2013, drug 4 is given. &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Drug 4 is not part of COT1 (drug2, drug3). &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;From 02/19/2013 – 03/18/2013, drug 4 is given at least twice. &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Thus, 02/19/2013 is start of COT2. &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;Any drug found from 02/19/2013 – 03/18/2013 is COT2 (drug4). &lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt;Repeat the same process for every record until last.patient_id i.e last record of the BY GROUP patient id.&amp;nbsp;&lt;/LI&gt;&lt;LI&gt;Follow same suit for all Patients or Patient ID aka exclusive BY GROUP.&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Does this make sense closer to your&amp;nbsp; need?.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;I ran the code, and yes it is to the right direction!! sum(c_to_count) does provide count of drug given twice from the date! thank you.&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;Thank you so much. I replied with bold letter to your steps. I hope this makes more sense.&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Part Logic for count_30 days of a drug for each observation --&amp;gt;&lt;/P&gt;&lt;P&gt;Lets do some initial findings using SQL applying the LOOK UP to get the dates and see if the Drug was found twice or more. This will help us understand whether our thought process in applying the logic for LOOK UP is right or not&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Aug 2019 03:01:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583806#M166233</guid>
      <dc:creator>yongheelee1212</dc:creator>
      <dc:date>2019-08-26T03:01:40Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583994#M166289</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285348"&gt;@yongheelee1212&lt;/a&gt;&amp;nbsp; &amp;nbsp;Little tired after not so good sleep over the weekend,&amp;nbsp; and so some attention to detail may miss accuracy, but test and let me know the discrepancies, I'll fix it. Do feel free, some warm water and tea should get me charged up again.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input ID Date :mmddyy10. Therapy $;
format date mmddyy10.;
cards;
1 10/1/2011   drug1
1 10/16/2012 drug1
1 10/30/2012 drug2
1 10/30/2012 drug2
1 11/13/2012 drug3
1 11/27/2012 drug2
1 12/11/2012 drug2
1 12/11/2012 drug3
1 12/18/2012 drug2
1 12/24/2012 drug3
1 12/31/2012 drug2
1 1/8/2013 drug3
1 1/15/2013 drug2
1 1/22/2013 drug2
1 1/29/2013 drug3
1 2/5/2013 drug3
1 2/12/2013 drug1
1 2/19/2013 drug4
1 2/26/2013 drug4
1 3/5/2013 drug4
;


data want ;
if _n_=1 then do;
   dcl hash H (ordered: "A") ;
   h.definekey  ('_Therapy',"_Date") ;
   h.definedata ('_Therapy',"_Date") ;
   h.definedone () ;
   dcl hash H1 () ;
   h1.definekey  ('_Therapy') ;
   h1.definedata ('course_of_treatment') ;
   h1.definedone () ;
   dcl hiter hi('h');
end;
do _n_=1 by 1 until(last.id);
 set have;
 by id date;
 _therapy=therapy;
 _date=date;
/*Load therapy and date as keys to the look up table for a given ID*/
 rc=h.add();
end;
/*Intitialize course_of_treatment with a value of 1 i.e
start of treatment*/
course_of_treatment=1;
do _n_=1 to _n_;
 set have;
 call missing(count,_f);
/*Look up to get the count*/
 do while(hi.next()=0);
  if date&amp;lt;=_date&amp;lt;=intnx('days',date,30) then 
  count=sum(count,_therapy=therapy);
/*Check if it is part of previous treatment and flag it*/
  if h1.find()=0 then _f=1;
 end;
/*If it satisfies both conditions, incrememnt course_of_treatment by 1 */
 if count&amp;gt;=2 and _f=1 then course_of_treatment+1;
 do while(hi.next()=0);
/*Load all the therapies associated with course_of_treatment
 to facilitate check for next iteration*/
  rc1=h1.add();
 end;
 output;
end;
/*Save memory space by clearing the contents of Hash*/
h.clear(); h1.clear();
drop rc: _:;
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>Mon, 26 Aug 2019 17:19:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/583994#M166289</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-26T17:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/585023#M166751</link>
      <description>&lt;P&gt;Dear&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;thank you so much for the help.&amp;nbsp;&lt;/P&gt;&lt;P&gt;With your help and previous post (which is also assisted by you), I was able to find the answer&lt;/P&gt;&lt;P&gt;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/How-to-identify-a-change-across-observations-and-flag-it-to/td-p/425362" target="_blank"&gt;https://communities.sas.com/t5/General-SAS-Programming/How-to-identify-a-change-across-observations-and-flag-it-to/td-p/425362&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 18:15:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/585023#M166751</guid>
      <dc:creator>yongheelee1212</dc:creator>
      <dc:date>2019-08-29T18:15:21Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/585059#M166772</link>
      <description>&lt;P&gt;Okay cool. We are glad. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Have fun! and take care!&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 19:30:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/585059#M166772</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-08-29T19:30:09Z</dc:date>
    </item>
    <item>
      <title>Re: creating variable based on observation from multiple variables within the time frame</title>
      <link>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/585088#M166788</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/285348"&gt;@yongheelee1212&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Dear&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;thank you so much for the help.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;With your help and previous post (which is also assisted by you), I was able to find the answer&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/General-SAS-Programming/How-to-identify-a-change-across-observations-and-flag-it-to/td-p/425362" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/General-SAS-Programming/How-to-identify-a-change-across-observations-and-flag-it-to/td-p/425362&lt;/A&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Please mark the question as solved and note the appropriate solution.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Aug 2019 20:43:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/creating-variable-based-on-observation-from-multiple-variables/m-p/585088#M166788</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-08-29T20:43:20Z</dc:date>
    </item>
  </channel>
</rss>

