<?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 dates based on different start and stop intervals in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/425800#M104891</link>
    <description>&lt;P&gt;This is a beautiful code and does even more than I had hoped for with my initial question! And yes, you are correct, I need to reframe how I think of my data - i.e. drop the Excel thinking.'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for putting it together so quickly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 08 Jan 2018 16:12:57 GMT</pubDate>
    <dc:creator>sophia_SAS</dc:creator>
    <dc:date>2018-01-08T16:12:57Z</dc:date>
    <item>
      <title>Creating dates based on different start and stop intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/425786#M104889</link>
      <description>&lt;P&gt;Dear SAS users,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you have suggestions on how I can create different start and stop dates based on noncontinuous&amp;nbsp;data points?&amp;nbsp; For example, for each observation I want to create variables mon_start1, mon_end1, mon_start2, mon_end2, mon_start3, mon_end3, etc.&amp;nbsp; I can create mon_start1 and mon_end1, but cannot figure out how to calculate the remaining variables (mon_start2, mon_end2, mon_start3, mon_end3, etc.)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's a sample dataset&amp;nbsp;for months Jan 2015 - Dec 2016 along with the beginning sas code&amp;nbsp;where I created variables mon_start1 and mon_end1.&amp;nbsp; &amp;nbsp;For example, for ID=AA, the start/end variables should be as follows:&lt;/P&gt;&lt;P&gt;mon_start1=1, mon_end1=6,&lt;/P&gt;&lt;P&gt;mon_start2=10, mon_end2=18,&lt;/P&gt;&lt;P&gt;mon_start3=22,&amp;nbsp; mon_end3=24&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; have;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;input&lt;/SPAN&gt; id $ dob &lt;SPAN class="s3"&gt;mmddyy10.&lt;/SPAN&gt; total_mon mon1-mon24;&lt;/P&gt;&lt;P class="p2"&gt;&lt;SPAN class="s2"&gt;format&lt;/SPAN&gt;&lt;SPAN class="s4"&gt; dob &lt;/SPAN&gt;monyy7.&lt;SPAN class="s4"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p3"&gt;datalines&lt;SPAN class="s4"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p1"&gt;AA 01/11/2015 18 1 1 1 1 1 1 . . . 1 1 1 1 1 1 1 1 1 . . . 1 1 1&lt;/P&gt;&lt;P class="p1"&gt;BB 01/29/2015 24 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1&lt;/P&gt;&lt;P class="p1"&gt;CC 08/23/2015 13 . . . . . . . 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 . .&lt;/P&gt;&lt;P class="p1"&gt;DD 02/11/2015 20 . 1 1 1 . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1&lt;/P&gt;&lt;P class="p1"&gt;EE 03/12/2016 8 . . . . . . . . . . . . . . 1 1 1 1 . 1 1 1 1 .&lt;/P&gt;&lt;P class="p1"&gt;;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s1"&gt;&lt;STRONG&gt;data&lt;/STRONG&gt;&lt;/SPAN&gt; have2;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;set&lt;/SPAN&gt; have;&lt;/P&gt;&lt;P class="p1"&gt;start=&lt;SPAN class="s3"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;Mon_Start1=(intck(&lt;SPAN class="s5"&gt;'month'&lt;/SPAN&gt;,&lt;SPAN class="s3"&gt;&lt;STRONG&gt;'01Jan2015'd&lt;/STRONG&gt;&lt;/SPAN&gt;,dob))+&lt;SPAN class="s3"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;array&lt;/SPAN&gt; month mon:;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;do&lt;/SPAN&gt; m=Mon_Start1-start+&lt;SPAN class="s3"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="s2"&gt;to&lt;/SPAN&gt; dim(month) &lt;SPAN class="s2"&gt;while&lt;/SPAN&gt;(month[m]);&lt;/P&gt;&lt;P class="p3"&gt;end&lt;SPAN class="s4"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p4"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;if&lt;/SPAN&gt; m&amp;gt;=&lt;SPAN class="s3"&gt;&lt;STRONG&gt;24&lt;/STRONG&gt;&lt;/SPAN&gt; &lt;SPAN class="s2"&gt;then&lt;/SPAN&gt; Mon_end1=total_mon;&lt;/P&gt;&lt;P class="p1"&gt;&lt;SPAN class="s2"&gt;else&lt;/SPAN&gt; Mon_End1=m-&lt;SPAN class="s3"&gt;&lt;STRONG&gt;1&lt;/STRONG&gt;&lt;/SPAN&gt;;&lt;/P&gt;&lt;P class="p5"&gt;&lt;STRONG&gt;run&lt;/STRONG&gt;&lt;SPAN class="s4"&gt;;&lt;/SPAN&gt;&lt;/P&gt;&lt;P class="p5"&gt;&amp;nbsp;&lt;/P&gt;&lt;P class="p5"&gt;&lt;SPAN class="s4"&gt;Thanks!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2018 15:39:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/425786#M104889</guid>
      <dc:creator>sophia_SAS</dc:creator>
      <dc:date>2018-01-08T15:39:21Z</dc:date>
    </item>
    <item>
      <title>Re: Creating dates based on different start and stop intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/425797#M104890</link>
      <description>&lt;P&gt;Like any question where you have many columns, it is better to drop the "Excel thinking" and normalise your data, i.e. have data going down in observations.&amp;nbsp; Now the below code, loops over the array and creates a normalised data structure with start/stop times, if the data was normalised in the first place it would be even simpler than this.&amp;nbsp; Remember your output report files can be transposed before output and so don't need to look like the data you work with.&lt;/P&gt;
&lt;PRE&gt;data have;
  input id $ dob mmddyy10. total_mon mon1-mon24;
  format dob monyy7.;
datalines;
AA 01/11/2015 18 1 1 1 1 1 1 . . . 1 1 1 1 1 1 1 1 1 . . . 1 1 1
BB 01/29/2015 24 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
CC 08/23/2015 13 . . . . . . . 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 . .
DD 02/11/2015 20 . 1 1 1 . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
EE 03/12/2016 8 . . . . . . . . . . . . . . 1 1 1 1 . 1 1 1 1 .
;
run;

data want (keep=id dob start_month end_month);
  set have;
  array mon{24};
  length start_month end_month $20;
  do i=1 to 24;
    if mon{i}=1 and start_month="" then start_month=vname(mon{i});
    else if (mon{i}=. and end_month="" and start_month ne "") then do;
      end_month=vname(mon{i-1});
      output;
      end_month="";
      start_month="";
    end;
    else if i=24 and end_month="" and start_month ne "" then do;
      end_month=vname(mon{i});
      output;
    end;
 end;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 08 Jan 2018 15:58:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/425797#M104890</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-01-08T15:58:16Z</dc:date>
    </item>
    <item>
      <title>Re: Creating dates based on different start and stop intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/425800#M104891</link>
      <description>&lt;P&gt;This is a beautiful code and does even more than I had hoped for with my initial question! And yes, you are correct, I need to reframe how I think of my data - i.e. drop the Excel thinking.'&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for putting it together so quickly.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2018 16:12:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/425800#M104891</guid>
      <dc:creator>sophia_SAS</dc:creator>
      <dc:date>2018-01-08T16:12:57Z</dc:date>
    </item>
    <item>
      <title>Re: Creating dates based on different start and stop intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/425826#M104905</link>
      <description>&lt;P&gt;I wouldn't be so quick in deciding that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;'s code does everything you want to accomplish. I added two example records where the code fails to work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Of course, I'm not suggesting that my code is perfect either, but it doesn't require restructuring your data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  input id $ dob mmddyy10. total_mon mon1-mon24;
  format dob monyy7.;
  datalines;
AA 01/11/2015 18 1 1 1 1 1 1 . . . 1 1 1 1 1 1 1 1 1 . . . 1 1 1
BB 01/29/2015 24 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
CC 08/23/2015 13 . . . . . . . 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 . .
DD 02/11/2015 20 . 1 1 1 . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
EE 03/12/2016 8 . . . . . . . . . . . . . . 1 1 1 1 . 1 1 1 1 .
FF 03/12/2016 8 . . . . . . . . . . . . . . . . . . . . . . . 1
GG 03/12/2016 8 . . . . . . . . . . . . . . . . . . . . . . . .
;

data want (drop=i start_month period);
  set have;
  array month{24} mon1-mon24;
  array mon_start(3);
  array mon_end(3);
  period=1;
  call missing(start_month);
  do i=1 to 24;
    if month{i}=1 then do;
      if missing(start_month) then do;
        start_month=i;
        mon_start{period}=i;
      end;
      if i eq 24 then mon_end{period}=i;
    end;
    else if missing(month{i}) and missing(mon_end{period}) and not missing(start_month) then do;
      mon_end{period}=i-1;
      period+1;
      call missing(start_month);
    end;
 end;
run;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jan 2018 17:14:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/425826#M104905</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-01-08T17:14:18Z</dc:date>
    </item>
    <item>
      <title>Re: Creating dates based on different start and stop intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/426083#M104962</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options missing='x';
data have;
  input id $ dob mmddyy10. total_mon mon1-mon24;
  format dob monyy7.;
  datalines;
AA 01/11/2015 18 1 1 1 1 1 1 . . . 1 1 1 1 1 1 1 1 1 . . . 1 1 1
BB 01/29/2015 24 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
CC 08/23/2015 13 . . . . . . . 1 1 1 1 1 1 1 1 1 . . 1 1 1 1 . .
DD 02/11/2015 20 . 1 1 1 . . . 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
EE 03/12/2016 8 . . . . . . . . . . . . . . 1 1 1 1 . 1 1 1 1 .
FF 03/12/2016 8 . . . . . . . . . . . . . . . . . . . . . . . 1
GG 03/12/2016 8 . . . . . . . . . . . . . . . . . . . . . . . .
;

data want;
 set have;
 temp=cats(of mon1-mon24);
 pid=prxparse('/1+/');
 s=1;e=length(temp);
 call prxnext(pid,s,e,temp,p,l);
 do while(p&amp;gt;0);
  start=p;end=p+l-1; output;
  call prxnext(pid,s,e,temp,p,l);
 end;
drop temp pid s e p l;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 09 Jan 2018 13:41:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/426083#M104962</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2018-01-09T13:41:04Z</dc:date>
    </item>
    <item>
      <title>Re: Creating dates based on different start and stop intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/426200#M104998</link>
      <description>Hi Art297,&lt;BR /&gt;&lt;BR /&gt;Thanks for responding to my inquiry! You are right in that the earlier code did not catch your two additional IDs - i.e. FF and GG. In my dataset while I never have any GG instances, I do have instances similar to FF (i.e. only 1 data point in the last month). For these examples, I added in a dummy month variable (month25).&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thank you for carefully considering this question and your excellent code. I'm now trying to figure out which to use given my current data structure and plan forward.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 09 Jan 2018 18:59:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/426200#M104998</guid>
      <dc:creator>sophia_SAS</dc:creator>
      <dc:date>2018-01-09T18:59:43Z</dc:date>
    </item>
    <item>
      <title>Re: Creating dates based on different start and stop intervals</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/426206#M105000</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;'s code works well too and provides the result in a long form like the one that&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/45151"&gt;@RW9&lt;/a&gt;&amp;nbsp;proposed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jan 2018 19:18:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Creating-dates-based-on-different-start-and-stop-intervals/m-p/426206#M105000</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-01-09T19:18:32Z</dc:date>
    </item>
  </channel>
</rss>

