<?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: Calculate date intervals using a date as a reference in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288510#M59509</link>
    <description>&lt;P&gt;As with most problems, it is your data structure which makes your problem difficult. &amp;nbsp;For programming purposes it is far easier to program with, and maintain if you use normalised data structures - fixed structure with data in the dataset. &amp;nbsp;This makes your problem very simple:&lt;/P&gt;
&lt;PRE&gt;data have;
  id=123; startdate="12mar2009"d; visit1="13mar2010"d; visit2="02aug2010"d;
  format startdate visit: date9.;
run;

proc transpose data=have out=inter;
  by id startdate;
  var visit:;
run;

data want;
  set inter;
  length month9_15 month21_27 $1;
  retain month9_15 month21_27;
  by id;
  if intnx("month",startdate,9) &amp;lt;= col1 &amp;lt;= intnx("month",startdate,15) then month9_15="Y";
  if intnx("month",startdate,21) &amp;lt;= col1 &amp;lt;= intnx("month",startdate,27) then month21_27="Y";
  if last.id then output;
run;&lt;/PRE&gt;
&lt;P&gt;For example gives you a dataset with one row per subject and a flag for each visit, its then a simple proc means (or you could total in that datastep).&lt;/P&gt;</description>
    <pubDate>Mon, 01 Aug 2016 10:08:05 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2016-08-01T10:08:05Z</dc:date>
    <item>
      <title>Calculate date intervals using a date as a reference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288501#M59506</link>
      <description>&lt;P&gt;I am trying to generate a count of how many total visits there were in a specific time period using the start date as a reference. There are 400+ observations and 100 visits.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Study design&lt;/STRONG&gt;: On the start date, participants were expected to return to the clinic at 12 months ± 3 months, 24 months ± 3 months, and 36 months ± 3 months.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Data&lt;/STRONG&gt;: (example)&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; &amp;nbsp; &amp;nbsp; Startdate &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;visit1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; visit2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;visit3 &amp;nbsp; &amp;nbsp; ... &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;visit100&lt;/P&gt;&lt;P&gt;123 &amp;nbsp; &amp;nbsp;12MAR2009 &amp;nbsp; &amp;nbsp;13MAR2010 &amp;nbsp; &amp;nbsp; 2AUG2010 &amp;nbsp; &amp;nbsp; &amp;nbsp;4JUN2012&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;5DEC2015&amp;nbsp;&lt;/P&gt;&lt;P&gt;456 &amp;nbsp; &amp;nbsp;14&lt;SPAN&gt;MAR2009 &amp;nbsp; &amp;nbsp;16&lt;/SPAN&gt;&lt;SPAN&gt;MAR2010 &amp;nbsp; &amp;nbsp; 4&lt;/SPAN&gt;&lt;SPAN&gt;AUG&lt;/SPAN&gt;&lt;SPAN&gt;201&lt;/SPAN&gt;&lt;SPAN&gt;0 &amp;nbsp; &amp;nbsp; &amp;nbsp;5JUN2012&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;10DEC2015&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;789 &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;16MAR2009 &amp;nbsp; &amp;nbsp;19&lt;/SPAN&gt;&lt;SPAN&gt;MAR2010 &amp;nbsp; &amp;nbsp; 6&lt;/SPAN&gt;&lt;SPAN&gt;AUG&lt;/SPAN&gt;&lt;SPAN&gt;201&lt;/SPAN&gt;&lt;SPAN&gt;0 &amp;nbsp; &amp;nbsp; &amp;nbsp;6JUN2012&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15DEC2015&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I need to know is if a patient has a visit within the range of:&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;9-15 months from the start date for 12 month visit and&lt;/LI&gt;&lt;LI&gt;21-27 months from the start date for 24 month visit and&lt;/LI&gt;&lt;LI&gt;33-39 months from the start date for 36 month visit&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Using the attached set, this is an example of what the reporting table would look like…&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;12 month visits&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;29*&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;24 month visits&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;18&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;&lt;P&gt;36 month visits&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;10&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;*This means that there were 29 visits that occurred between 9-15 months from the start date.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 09:49:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288501#M59506</guid>
      <dc:creator>JKS1</dc:creator>
      <dc:date>2016-08-01T09:49:30Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate date intervals using a date as a reference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288510#M59509</link>
      <description>&lt;P&gt;As with most problems, it is your data structure which makes your problem difficult. &amp;nbsp;For programming purposes it is far easier to program with, and maintain if you use normalised data structures - fixed structure with data in the dataset. &amp;nbsp;This makes your problem very simple:&lt;/P&gt;
&lt;PRE&gt;data have;
  id=123; startdate="12mar2009"d; visit1="13mar2010"d; visit2="02aug2010"d;
  format startdate visit: date9.;
run;

proc transpose data=have out=inter;
  by id startdate;
  var visit:;
run;

data want;
  set inter;
  length month9_15 month21_27 $1;
  retain month9_15 month21_27;
  by id;
  if intnx("month",startdate,9) &amp;lt;= col1 &amp;lt;= intnx("month",startdate,15) then month9_15="Y";
  if intnx("month",startdate,21) &amp;lt;= col1 &amp;lt;= intnx("month",startdate,27) then month21_27="Y";
  if last.id then output;
run;&lt;/PRE&gt;
&lt;P&gt;For example gives you a dataset with one row per subject and a flag for each visit, its then a simple proc means (or you could total in that datastep).&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 10:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288510#M59509</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-08-01T10:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate date intervals using a date as a reference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288512#M59511</link>
      <description>&lt;P&gt;First, transpose from wide to long format, then cumulate per ID:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=have out=visits (drop=_NAME_ rename=(COL1=visit));
by ID;
copy startdate;
run;

data want (keep=ID visit12 visit24 visit36);
set visits;
by ID;
retain start visit12 visit24 visit36;
if first.ID
then do
  start = startdate;
  visit12 = 0;
  visit24 = 0;
  visit36 = 0;
end;
if intnx('month',start,9) &amp;lt;= visit &amp;lt; intnx('month',start,16) then visit12 + 1;
if intnx('month',start,21) &amp;lt;= visit &amp;lt; intnx('month',start,28) then visit24 + 1;
if intnx('month',start,33) &amp;lt;= visit &amp;lt; intnx('month',start,40) then visit36 + 1;
if last.ID then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;To get total sums, just don't reset the counters with every group change, and provide only 1 output at EOF.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 10:17:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288512#M59511</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-01T10:17:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate date intervals using a date as a reference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288519#M59514</link>
      <description>Hi - When I attempted to transpose the set, the date value was lost? The start date also only shows up on the first ID and should show up for each visit.</description>
      <pubDate>Mon, 01 Aug 2016 10:48:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288519#M59514</guid>
      <dc:creator>JKS1</dc:creator>
      <dc:date>2016-08-01T10:48:36Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate date intervals using a date as a reference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288582#M59532</link>
      <description>&lt;P&gt;Believe it or not, the programming will be easy.&amp;nbsp; The hard part is defining the solution.&amp;nbsp; For example, with a start date of 12MAR2009, what time period should the 9-15 month interval include?&amp;nbsp; Do you want to count days rather than months to define the interval?&amp;nbsp; (Consider what should happen if the start date were 30MAY2009 and there is no such thing as 30FEB2010.)&amp;nbsp; If a person has multiple visits within that interval, should all the person's visits be counted or just 1?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Once those things are defined, running through an array in a DATA step will be easy.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 13:44:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288582#M59532</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-08-01T13:44:55Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate date intervals using a date as a reference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288597#M59533</link>
      <description>Hi Astounding, I was able to program this in STATA using this code:&lt;BR /&gt;replace visit36mon=1 if (vs1-artstartdate)&amp;gt;=1005 &amp;amp; (vs1-artstartdate)&amp;lt;=1185 &amp;amp; (vs1~=.) |&lt;BR /&gt;&lt;BR /&gt;--&amp;gt; This program counts any visit occurring with a date (by day) range as =1.</description>
      <pubDate>Mon, 01 Aug 2016 14:29:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288597#M59533</guid>
      <dc:creator>JKS1</dc:creator>
      <dc:date>2016-08-01T14:29:06Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate date intervals using a date as a reference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288604#M59535</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/97082"&gt;@JKS1&lt;/a&gt; wrote:&lt;BR /&gt;Hi - When I attempted to transpose the set, the date value was lost? The start date also only shows up on the first ID and should show up for each visit.&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You probably meant that the FORMAT was lost. That's not a big issue, you can assign the format later when you need to display values.&lt;/P&gt;
&lt;P&gt;Since you are looking for the sums of visits, anyway, you won't need it at all in intermediate datasets.&lt;/P&gt;
&lt;P&gt;And the second issue is resolved by keeping the startdate value in RETAINed variable, as I did in my data step.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 14:37:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288604#M59535</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2016-08-01T14:37:08Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate date intervals using a date as a reference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288612#M59538</link>
      <description>&lt;P&gt;Perfect.&amp;nbsp; You can do the same in SAS.&amp;nbsp; It will make a difference, though, whether your DATE variables are actually numeric SAS dates, or whether they are character strings.&amp;nbsp; If they are numeric SAS dates (the preferred method for many reasons):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array visits {100} visit1-visit100;&lt;/P&gt;
&lt;P&gt;_12_mo_visits=0;&lt;/P&gt;
&lt;P&gt;_24_mo_visits=0;&lt;/P&gt;
&lt;P&gt;_36_mo_visits=0;&lt;/P&gt;
&lt;P&gt;do _n_=1 to 100;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if startdate + 1005 &amp;lt;= visits{_n_} &amp;lt;= startdate + 1185 then _36_mo_visits + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; * Add 2 more statements here, for the 12-month and 24-month counts;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;That gives you 3 new variables in the data set, with counts per observation:&amp;nbsp; how many visits in each of the ranges.&amp;nbsp; You can add them up easily:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc means data=want sum;&lt;/P&gt;
&lt;P&gt;var _12_mo_visits _24_mo_visits _36_mo_visits;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or&amp;nbsp; you can count patients with each level of # of visits instead:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; tables _12_mo_visits _24_mo_visits _36_mo_visits;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good luck.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;*** EDITED:&amp;nbsp; If your date variables are actually character strings rather than numeric SAS dates, only one piece of the program needs to change:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if startdate + 1005 &amp;lt;= &lt;FONT color="#00FF00"&gt;input(&lt;/FONT&gt;visits{_n_}&lt;FONT color="#00FF00"&gt;, date9.)&lt;/FONT&gt; &amp;lt;= startdate + 1185 then _36_mo_visits + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The same would apply to STARTDATE, depending on whether that is a numeric SAS date vs. a character string.&lt;/P&gt;</description>
      <pubDate>Mon, 01 Aug 2016 14:55:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288612#M59538</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-08-01T14:55:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate date intervals using a date as a reference</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288808#M59612</link>
      <description>&lt;PRE&gt;
I think no need proc transpose.
Change Kurt's code :


data want (keep=ID visit12 visit24 visit36);
set visits;
array x{*} visit: ;

  _visit12 = 0;
  _visit24 = 0;
  _visit36 = 0;

do i=1 to dim(x);
if intnx('month',start,9) &amp;lt;= x{i}&amp;lt; intnx('month',start,16) then _visit12 + 1;
if intnx('month',start,21) &amp;lt;= x{i} &amp;lt; intnx('month',start,28) then _visit24 + 1;
if intnx('month',start,33) &amp;lt;= x{i} &amp;lt; intnx('month',start,40) then _visit36 + 1;
end;
run;


&lt;/PRE&gt;</description>
      <pubDate>Tue, 02 Aug 2016 04:11:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculate-date-intervals-using-a-date-as-a-reference/m-p/288808#M59612</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-08-02T04:11:37Z</dc:date>
    </item>
  </channel>
</rss>

