<?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: Calculating number of days for each month in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333894#M75304</link>
    <description>&lt;P&gt;Here's a minor modificatino of the above, shortening the program a litte:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input task :$5. start_date :date9.  number_of_days;
  format start_date date9.;
datalines;
task1 01feb2017 48
task2 15mar2017 60
run;

data need (keep=task start_date number_of_days monthname monthdays);
  set have;
  do d=start_date by 0 until(d&amp;gt;=start_date+number_of_days);
    monthname=left(put(d,yymon7.));
    limit =min(start_date+number_of_days,intnx('month',d,0,'end')+1);
    monthdays=limit-d;
    d=intnx('month',d,1,'beg');
    output;
  end;
run;
proc transpose data=need out=want ;
  by task start_date number_of_days notsorted;
  var monthdays;
  id monthname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 17 Feb 2017 19:21:31 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2017-02-17T19:21:31Z</dc:date>
    <item>
      <title>Calculating number of days for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333820#M75295</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am working to bucket the given number of days for each task to current and consecutive months from the given start date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Source -&lt;/P&gt;
&lt;P&gt;Task &amp;nbsp; &amp;nbsp; &amp;nbsp; start_date &amp;nbsp; &amp;nbsp; &amp;nbsp; number_of_days&lt;/P&gt;
&lt;P&gt;Task1 &amp;nbsp; &amp;nbsp; &amp;nbsp;01Feb2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 48&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Task2 &amp;nbsp; &amp;nbsp; &amp;nbsp;15Mar2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 60&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because Feb2017 has 28 days in that month, number_of_days given is 48, so 48 - 28 = 20. Those 20 should fall into next month March2017&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;output needed -&lt;/P&gt;
&lt;P&gt;Task &amp;nbsp; &amp;nbsp;start_Date &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Feb2017 Mar2017 Apr2017&lt;/P&gt;
&lt;P&gt;Task1 &amp;nbsp;01Feb2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; 28 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;/P&gt;
&lt;P&gt;Task2 &amp;nbsp;15Mar2017 &amp;nbsp; &amp;nbsp; &amp;nbsp; 0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;15 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 45&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any suggestions are greatly appreciated !&lt;/P&gt;</description>
      <pubDate>Fri, 17 Feb 2017 16:00:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333820#M75295</guid>
      <dc:creator>nash_sas</dc:creator>
      <dc:date>2017-02-17T16:00:35Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating number of days for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333836#M75297</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below is what I'd do, but you'll probably get better solutions from others. I'm assuming that ending up with 45 days in April201 is an error, but the basic approach&amp;nbsp;is to expand the observations to cover the necessary months and then transpose. You could probably do something with arrays, but I think this will be more dynamic. Depending on the real set of data, you might need to tweak it a bit, but it should get you started.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&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 task $ start_date :date9. number_of_days;
    format start_date date9.;
datalines;
Task1 01Feb2017 48
Task2 15Mar2017 60
;

data want;
    set have;
    /* Calculate the end date based on the number of days. */
    end_date = start_date + number_of_days;
    /* Iterate over how many months the span covers. */
    do i = 1 to (intck('month', start_date, end_date) + 1);
        /* For every month, determine the first and last days. */
        fdom = intnx('month', start_date, i - 1, 'B');
        ldom = intnx('month', start_date, i - 1, 'E');
        /* Use these to calculate different date spans. */
        full_month_days = ldom - fdom + 1;
        part_month_days = min(full_month_days, end_date - fdom, ldom - start_date + 1); 
        /* Determine the number of days in the final segment. */
        days = min(full_month_days, part_month_days);
        /* Give the observation a name for transposition. */
        name = substr(put(fdom, date9.), 3);
        output;
    end; 
    format end_date fdom ldom date9.;
run;

proc transpose data = want out = want2;
    by task start_date number_of_days;
    var days;
    id name;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Feb 2017 16:40:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333836#M75297</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-02-17T16:40:37Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating number of days for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333894#M75304</link>
      <description>&lt;P&gt;Here's a minor modificatino of the above, shortening the program a litte:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  input task :$5. start_date :date9.  number_of_days;
  format start_date date9.;
datalines;
task1 01feb2017 48
task2 15mar2017 60
run;

data need (keep=task start_date number_of_days monthname monthdays);
  set have;
  do d=start_date by 0 until(d&amp;gt;=start_date+number_of_days);
    monthname=left(put(d,yymon7.));
    limit =min(start_date+number_of_days,intnx('month',d,0,'end')+1);
    monthdays=limit-d;
    d=intnx('month',d,1,'beg');
    output;
  end;
run;
proc transpose data=need out=want ;
  by task start_date number_of_days notsorted;
  var monthdays;
  id monthname;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 17 Feb 2017 19:21:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333894#M75304</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-17T19:21:31Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating number of days for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333902#M75308</link>
      <description>&lt;P&gt;The only concern I'd have with alternate solution is excluding the year in the transposed variable name. It might not matter, but if the number of days is ever more than a year or cross into a new year, you might end up with duplicates that won't transpose or ambiguous column names.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Feb 2017 19:16:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333902#M75308</guid>
      <dc:creator>collinelliot</dc:creator>
      <dc:date>2017-02-17T19:16:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating number of days for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333904#M75309</link>
      <description>&lt;P&gt;&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/93752"&gt;@collinelliot&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;The only concern I'd have with alternate solution is excluding the year in the transposed variable name. It might not matter, but if the number of days is ever more than a year or cross into a new year, you might end up with duplicates that won't transpose or ambiguous column names.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/93752"&gt;@collinelliot&lt;/a&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Good point.&amp;nbsp; To address that issue, I editted the date format inserted into monthname from MONNAME8.&amp;nbsp; to YYMON7.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;THX&lt;/P&gt;</description>
      <pubDate>Fri, 17 Feb 2017 19:23:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/333904#M75309</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-02-17T19:23:14Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating number of days for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/334034#M75375</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Task  $     start_date   : date9.    number_of_days;
format start_date date7.;
cards;
Task1      01Feb2017         48
Task2      15Mar2017         60
;
run;
data temp;
 set have;
 do i=0 to number_of_days-1;
  date=start_date+i;output;
 end;
 keep task start_date date;
run;
proc freq data=temp noprint;
 table task*start_date*date/out=temp1 list nopercent nocum ;
 format date monyy5.;
run;
proc transpose data=temp1 out=temp2(drop=_:);
by task start_date;
var count;
id date;
run;
proc stdize data=temp2 out=want missing=0 reponly;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 18 Feb 2017 06:25:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/334034#M75375</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-02-18T06:25:32Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating number of days for each month</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/335379#M75896</link>
      <description>&lt;P&gt;Thank you everyone for your prompt solution.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Feb 2017 19:10:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-number-of-days-for-each-month/m-p/335379#M75896</guid>
      <dc:creator>nash_sas</dc:creator>
      <dc:date>2017-02-23T19:10:42Z</dc:date>
    </item>
  </channel>
</rss>

