<?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: How to calculate the number of days person belonged in a group in each month? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349078#M80932</link>
    <description>&lt;P&gt;At the end it work (it took some time to calculate). Thanks&lt;/P&gt;</description>
    <pubDate>Tue, 11 Apr 2017 12:49:04 GMT</pubDate>
    <dc:creator>viollete</dc:creator>
    <dc:date>2017-04-11T12:49:04Z</dc:date>
    <item>
      <title>How to calculate the number of days person belonged in a group in each month?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349050#M80917</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have this type of data:&lt;/P&gt;
&lt;P&gt;ID start_study end_study&lt;/P&gt;
&lt;P&gt;1 &amp;nbsp;01APR2010 &amp;nbsp;20OCT2010&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;2 &amp;nbsp;01APR2010 &amp;nbsp;01APR2014&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;3 &amp;nbsp;01Nov2010 &amp;nbsp;20OCT2015&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;4 &amp;nbsp;01APR2010 &amp;nbsp;20OCT2016&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;5 &amp;nbsp;01APR2010 &amp;nbsp;20APR2010&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;.......&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The period I am looking in is from 01APR2010 till 01APR2017. I want to create a variable for each month: 84 varibles.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The start_study is the date when a person joined a group and end_study is the date when he left it. I want to calculate for each person how many days per month a person belonged for a group, for example:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;ID=1 in first month (april 2010) was in group for 30 days, in second month (may 2010) was for 31 days , ...., in seventh month (octorber 2010) was for 20 days and after would be 0 cause he left the group.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;what would be the best way to do this?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;thank you&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 10:49:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349050#M80917</guid>
      <dc:creator>viollete</dc:creator>
      <dc:date>2017-04-11T10:49:41Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the number of days person belonged in a group in each month?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349055#M80920</link>
      <description>&lt;P&gt;Instead of creating variables for months, it's better to write a record per month. Never keep data (months) in structure (variables/columns).&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input ID start_study :date9. end_study :date9.;
format start_study end_study date9.;
cards;
1 01APR2010 20OCT2010
2 01APR2010 01APR2014
3 01Nov2010 20OCT2015
4 01APR2010 20OCT2016
5 01APR2010 20APR2010
;
run;

data want (keep=id year month yearmonth days);
set have;
length yearmonth $6;
do until (start_study &amp;gt; end_study);
  year = year(start_study);
  month = month(start_study);
  yearmonth = put(year,z4.) !! put(month,z2.);
  days = min(intnx('month',start_study,0,'end'),end_study) - start_study + 1;
  output;
  start_study = intnx('month',start_study,1,'begin');
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Also note how I presented example data in a data step. This allows your assumed helpers to recreate your dataset with a simple copy/paste and run. See this as a simple courtesy toward those you want to help you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In case you absolutely need columns per month, yearmonth can be used in a proc transpose to name the columns.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 11:08:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349055#M80920</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-11T11:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the number of days person belonged in a group in each month?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349070#M80927</link>
      <description>&lt;P&gt;Thanks! It works fine for small data size, but mine is more than 7 million of observations (sorry that i didnt mentioned that before!)&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 12:22:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349070#M80927</guid>
      <dc:creator>viollete</dc:creator>
      <dc:date>2017-04-11T12:22:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the number of days person belonged in a group in each month?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349073#M80929</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/37682"&gt;@viollete&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Thanks! It works fine for small data size, but mine is more than 7 million of observations (sorry that i didnt mentioned that before!)&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;And?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;7 Million * 38 bytes (4*8 for the numerics and 6 for yearmonth) equates to roughly 270 MB, which is puny.&lt;/P&gt;
&lt;P&gt;And you can even reduce the number of bytes needed:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;length year month days 3;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;as the small numbers don't need more precision.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And keep in mind that forcing SAS to have columns for all months when most of the times the values will be missing is a colossal waste of resources.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 12:34:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349073#M80929</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2017-04-11T12:34:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate the number of days person belonged in a group in each month?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349078#M80932</link>
      <description>&lt;P&gt;At the end it work (it took some time to calculate). Thanks&lt;/P&gt;</description>
      <pubDate>Tue, 11 Apr 2017 12:49:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-the-number-of-days-person-belonged-in-a-group/m-p/349078#M80932</guid>
      <dc:creator>viollete</dc:creator>
      <dc:date>2017-04-11T12:49:04Z</dc:date>
    </item>
  </channel>
</rss>

