<?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: Count days in month arrays in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623486#M183573</link>
    <description>&lt;P&gt;A few ideas here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myfile1;
input clientID $ (startDt EndDt)(:date9.);
format startDt EndDt date9.;
cards;
1111 03Oct2011 21Nov2011
1111 10Dec2011 11Jan2012
2222 15Jan2012 21MAR2012
2222 03May2012 24Jun2012
3333 03Nov2011 21Dec2011
;
run;

data step1;
set myfile1;
do while (startdt lt enddt);
	thismonth = put(startdt,yymmn6.);
	days = intck('day',startdt,min(intnx('month',startdt,0,'e'),enddt))+1;
	output;
	startdt = intnx('month',startdt,1,'b');
end;
drop enddt;
run;

proc sort data=step1;
by clientid thismonth;
run;

proc transpose data=step1 out=want(drop=_name_) prefix=month_;
by clientid;
id thismonth;
var days;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if you are concerned about missing values in want:&lt;/P&gt;
&lt;P&gt;(must have SAS ETS for proc timeseries)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc timeseries data=step1 out=step2;
by clientid;
id startdt interval=month start='01OCT2011'd end='30JUN2012'd setmissing=0;
var days;
run;

proc transpose data=step2 out=want(drop=_name_) prefix=month_;
by clientid;
id startdt;
var days;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Mon, 10 Feb 2020 03:34:41 GMT</pubDate>
    <dc:creator>unison</dc:creator>
    <dc:date>2020-02-10T03:34:41Z</dc:date>
    <item>
      <title>Count days in month arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623474#M183567</link>
      <description>&lt;P&gt;Hi ,&lt;/P&gt;
&lt;P&gt;I have a span file and want to create month array in which each month counts the number of days a client stayed in hospital.&lt;/P&gt;
&lt;P&gt;Thanks in advance !&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is the sample file:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myfile1;
input clientID $ (startDt EndDt)(:date9.);
format startDt EndDt date9.;
cards;
1111 03Oct2011 21Nov2011
1111 10Dec2011 11Jan2012
2222 15Jan2012 21MAR2012
2222 03May2012 24Jun2012
3333 03Nov2011 21Dec2011
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I want the outcome file like this:&amp;nbsp; DayCount1 represents the earliest month in the StartDt, and&amp;nbsp;DayCount9 represents the last month of&amp;nbsp;&lt;/P&gt;
&lt;P&gt;the EndDt.&lt;/P&gt;
&lt;P&gt;%let FirstMo= %sysfunc(intck(month,"01Sep2011"d,"01Oct2011"d));&lt;BR /&gt;%let LastMo= %sysfunc(intck(month,"01Oct2011"d,"01Jul2012"d));&lt;/P&gt;
&lt;P&gt;%put &amp;amp;FirstMo;&lt;BR /&gt;%put &amp;amp;LastMo;&lt;/P&gt;
&lt;TABLE width="830"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="83"&gt;clientID&lt;/TD&gt;
&lt;TD width="83"&gt;DayCount1&lt;/TD&gt;
&lt;TD width="83"&gt;DayCount2&lt;/TD&gt;
&lt;TD width="83"&gt;DayCount3&lt;/TD&gt;
&lt;TD width="83"&gt;DayCount4&lt;/TD&gt;
&lt;TD width="83"&gt;DayCount5&lt;/TD&gt;
&lt;TD width="83"&gt;DayCount6&lt;/TD&gt;
&lt;TD width="83"&gt;DayCount7&lt;/TD&gt;
&lt;TD width="83"&gt;DayCount8&lt;/TD&gt;
&lt;TD width="83"&gt;DayCount9&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="83"&gt;1111&lt;/TD&gt;
&lt;TD width="83"&gt;28&lt;/TD&gt;
&lt;TD width="83"&gt;21&lt;/TD&gt;
&lt;TD width="83"&gt;21&lt;/TD&gt;
&lt;TD width="83"&gt;11&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="83"&gt;2222&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;16&lt;/TD&gt;
&lt;TD width="83"&gt;29&lt;/TD&gt;
&lt;TD width="83"&gt;21&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;28&lt;/TD&gt;
&lt;TD width="83"&gt;24&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="83"&gt;3333&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;27&lt;/TD&gt;
&lt;TD width="83"&gt;21&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;TD width="83"&gt;0&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The follow code create indicator array, but I need array that count # of days in each month clients stayed .&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myfile2;
set myfile1;
by clientID;

Adm_Mo= intck('month',"01Oct2011"d,startDt)+1;
Rel_Mo= intck('month',"01Oct2011"d,EndDt)+1;
retain flg&amp;amp;FirstMo. - flg&amp;amp;LastMo.;
array flg[&amp;amp;FirstMo.:&amp;amp;LastMo.] flg&amp;amp;FirstMo. - flg&amp;amp;LastMo.;

if first.clientID then do i = &amp;amp;FirstMo. to &amp;amp;LastMo.;
flg[i] = 0;
end;
do i = &amp;amp;FirstMo. to &amp;amp;LastMo.;
if i GE Adm_Mo and i LE Rel_Mo then flg[i] = 1;
end;

if last.clientID then output;
keep clientID flg&amp;amp;FirstMo. - flg&amp;amp;LastMo.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Feb 2020 01:35:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623474#M183567</guid>
      <dc:creator>dat333</dc:creator>
      <dc:date>2020-02-10T01:35:26Z</dc:date>
    </item>
    <item>
      <title>Re: Count days in month arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623480#M183569</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/3021"&gt;@dat333&lt;/a&gt;&amp;nbsp; &amp;nbsp;Your very wide reporting structure doesn't make it squeaky clean for me atleast as each Daycount is meant for monthly admission. At any rate, it's ideal to have long and narrow and take a call in how best the summary report is to be presented.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first step is basically to establish the day count. To my mind 03oct2011 to 31oct2011 is 29 days and your output shows 28 days. Well, this is something you can adjust being a simple arithmetic.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myfile1;
input clientID $ (startDt EndDt)(:date9.);
format startDt EndDt date9.;
cards;
1111 03Oct2011 21Nov2011
1111 10Dec2011 11Jan2012
2222 15Jan2012 21MAR2012
2222 03May2012 24Jun2012
3333 03Nov2011 21Dec2011
;
run;




data temp;
set myfile1;
by clientid;
do  while(startdt&amp;lt;enddt);
 ed=intnx('mon',startDt	,0,'e');
 if put(ed,monyy7. -l)=put(enddt,monyy7. -l) then ed=enddt;
 DayCount=ed-startdt+1;
 month_yr=put(ed,monyy7.);
 output;
 startdt= ed+1;
end;
format ed date9.;
keep clientid daycount month_yr;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The above is the fundamental structure that needs to be considered essentially forming the base to go wide if you like. I leave that up to you to decide how you may wanna transpose it as it doesnt provide a comprehensive structure to have it wide month after month, though you can transpose like below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc transpose data=temp out=want(drop=_:) prefix=Daycount_;
by clientid ;
id month_yr;
var daycount ;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Feb 2020 02:40:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623480#M183569</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-02-10T02:40:34Z</dc:date>
    </item>
    <item>
      <title>Re: Count days in month arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623486#M183573</link>
      <description>&lt;P&gt;A few ideas here:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data myfile1;
input clientID $ (startDt EndDt)(:date9.);
format startDt EndDt date9.;
cards;
1111 03Oct2011 21Nov2011
1111 10Dec2011 11Jan2012
2222 15Jan2012 21MAR2012
2222 03May2012 24Jun2012
3333 03Nov2011 21Dec2011
;
run;

data step1;
set myfile1;
do while (startdt lt enddt);
	thismonth = put(startdt,yymmn6.);
	days = intck('day',startdt,min(intnx('month',startdt,0,'e'),enddt))+1;
	output;
	startdt = intnx('month',startdt,1,'b');
end;
drop enddt;
run;

proc sort data=step1;
by clientid thismonth;
run;

proc transpose data=step1 out=want(drop=_name_) prefix=month_;
by clientid;
id thismonth;
var days;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or if you are concerned about missing values in want:&lt;/P&gt;
&lt;P&gt;(must have SAS ETS for proc timeseries)&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc timeseries data=step1 out=step2;
by clientid;
id startdt interval=month start='01OCT2011'd end='30JUN2012'd setmissing=0;
var days;
run;

proc transpose data=step2 out=want(drop=_name_) prefix=month_;
by clientid;
id startdt;
var days;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 10 Feb 2020 03:34:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623486#M183573</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-02-10T03:34:41Z</dc:date>
    </item>
    <item>
      <title>Re: Count days in month arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623496#M183578</link>
      <description>Thanks!   That's exactly what I needed.</description>
      <pubDate>Mon, 10 Feb 2020 04:29:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623496#M183578</guid>
      <dc:creator>dat333</dc:creator>
      <dc:date>2020-02-10T04:29:56Z</dc:date>
    </item>
    <item>
      <title>Re: Count days in month arrays</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623497#M183579</link>
      <description>Thanks for your help!</description>
      <pubDate>Mon, 10 Feb 2020 04:30:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Count-days-in-month-arrays/m-p/623497#M183579</guid>
      <dc:creator>dat333</dc:creator>
      <dc:date>2020-02-10T04:30:33Z</dc:date>
    </item>
  </channel>
</rss>

