<?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: Days in a given month represented by a date range in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Days-in-a-given-month-represented-by-a-date-range/m-p/804194#M316654</link>
    <description>&lt;P&gt;The methods in your link look like they should work.&amp;nbsp; I'm not sure how you translated the program for your application, but here is a way that should be OK:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data days;
   set drug;
   do date = startdate to enddate;
      output;
   end;
run;

proc freq data=days;
   tables id * date / output out=counts;
   format date monyy7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Fri, 25 Mar 2022 22:23:24 GMT</pubDate>
    <dc:creator>Astounding</dc:creator>
    <dc:date>2022-03-25T22:23:24Z</dc:date>
    <item>
      <title>Days in a given month represented by a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-a-given-month-represented-by-a-date-range/m-p/804166#M316643</link>
      <description>&lt;P&gt;Hello, I am trying to create a series of count variables for two years (24 variables January19-December19 and January20-December20) that reflect the number of days in each month from a given date range. For example, consider:&lt;/P&gt;&lt;PRE&gt;data WORK.DRUG(label='Drug Episode');
   infile datalines;
   input enrolid:$8. startdate:mmddyy10. enddate:mmddyy10.;
 datalines;
 12344 02/02/2019 03/04/2019
 32456 03/18/2019 04/17/2019
 21424 04/26/2019 05/26/2019
 37625 05/25/2019 06/25/2019
 34635 12/28/2019 01/28/2020
 ;;;;&lt;/PRE&gt;&lt;P&gt;For enrolid=12344, the result I want is: January19=0, February19=26, March19=4, April19=0...etc. I have two years of data, so dates may occasionally cross years (eg last line of sample code)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have tried to follow the advice from a previous post (&lt;A href="https://communities.sas.com/t5/SAS-Programming/Get-Number-of-days-in-each-month-between-two-dates/td-p/548014" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Get-Number-of-days-in-each-month-between-two-dates/td-p/548014&lt;/A&gt;) by converting my dates to SAS date constants and running a couple of the suggested options, but get the error message my variables are uninitialized, so I think perhaps this code is for specific one-time inputs rather than a list of variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate any insight. I'm using 9.4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2022 19:16:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-a-given-month-represented-by-a-date-range/m-p/804166#M316643</guid>
      <dc:creator>jkem</dc:creator>
      <dc:date>2022-03-25T19:16:30Z</dc:date>
    </item>
    <item>
      <title>Re: Days in a given month represented by a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-a-given-month-represented-by-a-date-range/m-p/804194#M316654</link>
      <description>&lt;P&gt;The methods in your link look like they should work.&amp;nbsp; I'm not sure how you translated the program for your application, but here is a way that should be OK:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data days;
   set drug;
   do date = startdate to enddate;
      output;
   end;
run;

proc freq data=days;
   tables id * date / output out=counts;
   format date monyy7.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 25 Mar 2022 22:23:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-a-given-month-represented-by-a-date-range/m-p/804194#M316654</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2022-03-25T22:23:24Z</dc:date>
    </item>
    <item>
      <title>Re: Days in a given month represented by a date range</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Days-in-a-given-month-represented-by-a-date-range/m-p/804198#M316657</link>
      <description>&lt;P&gt;You did not very clearly specify what the output should look like. Here is my stab.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data WORK.DRUG(label='Drug Episode');
   infile datalines;
   input enrolid:$8. startdate:mmddyy10. enddate:mmddyy10.;
   format  startdate mmddyy10. enddate mmddyy10.;
 datalines;
 12344 02/02/2019 03/04/2019
 32456 03/18/2019 04/17/2019
 21424 04/26/2019 05/26/2019
 37625 05/25/2019 06/25/2019
 34635 12/28/2019 01/28/2020
 11111 12/28/2019 04/13/2020
 ;

 data need;
    set work.drug;
    m='01Jan2019'd;
    do until (m= '01Dec2020'd);
       mtext= put(m,monyy7.);
       /* month well before start date*/
       If intck('month',m,intnx('month',startdate,0,'B')) &amp;gt; 0 then days=0;
       /* month the same as the start date*/
       else if m=intnx('month',startdate,0,'B')  then 
         days=intck('day',startdate,intnx('month',startdate,0,'E'));
       /* end of period in the month*/
       else if m=intnx('month',enddate,0,'B') then days= day(enddate);
       /* month after the end of the period*/
       else if intck('month',m,intnx('month',enddate,0,'E')) &amp;lt; 0 then days=0;
       /* month completely within interval*/
       else days= day(intnx('month',m,0,'E'));
       output;
       call missing(days);
       m= intnx('month',m,1,'b');
    end;
    format m monyy.;
run;

proc transpose data=need out=want(drop=_name_)
   ;
   by notsorted enrolid startdate enddate  ;
   var days;
   id mtext;
run;&lt;/PRE&gt;
&lt;P&gt;Your data did not include anything with a start end period longer than a month, or describe the result intended. I added one at the end of the data set so you can see if that is proper result.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the comments for the different comparisons as to which each interval is setting the value for the Days variable.&lt;/P&gt;
&lt;P&gt;There may be something slicker, especially if your intervals ALWAYS are basically one month and you state that to be the case.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note that I used 4-digit years. There is NO good that comes from using two digit years. Also made all of the variable names the same length.&lt;/P&gt;
&lt;P&gt;It may be that "wide" data really isn't needed. How do you expect to use the resulting set? Normally placing actual data, the month/year, into a Variable is poor practice. Also sorts of the variable names result in awkward behavior.&lt;/P&gt;</description>
      <pubDate>Fri, 25 Mar 2022 23:34:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Days-in-a-given-month-represented-by-a-date-range/m-p/804198#M316657</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2022-03-25T23:34:14Z</dc:date>
    </item>
  </channel>
</rss>

