<?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: Aggregating for 365 Days, and not for Full Calendar Year in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241837#M44840</link>
    <description>&lt;P&gt;Thanks &lt;SPAN class="login-bold"&gt;FreelanceReinhard&lt;/SPAN&gt;&amp;nbsp;this solves my question. I consider this question resolved. I still have a lot to learn in terms of base SAS as some of the functions in your code are new to me (e.g. intck).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an add-up is there a way to twist your code so that instead of a year, I can sum per 30, 60, or 90 days (Quarterly)?&lt;/P&gt;</description>
    <pubDate>Mon, 11 Jan 2016 12:15:57 GMT</pubDate>
    <dc:creator>Filippos</dc:creator>
    <dc:date>2016-01-11T12:15:57Z</dc:date>
    <item>
      <title>Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241818#M44836</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following problem, for which I have been unable to find a solution through searching online.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Lets suppose we have the following table ("Purchases"):&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;Date                 Units_Sold             Brand       Year
18/03/2010                5                   A         2010
12/04/2010                2                   A         2010
22/05/2010                1                   A         2010
25/05/2010                7                   A         2010
11/08/2011                5                   A         2011
12/07/2010                2                   B         2010
22/10/2010                1                   B         2010
05/05/2011                7                   B         2011&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;And the same logic continues until the end of 2014, for different brands.&lt;/P&gt;&lt;P&gt;What I want to do is calculate the number of Units_Sold for every Brand, in each year. However, I don't want to do it for the calendar year, but for the full&amp;nbsp;year after a Brand appears (365 days).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So an example of what I &lt;STRONG&gt;don't&lt;/STRONG&gt; want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;proc sql;
create table Dont_Want as
select Year, Brand, sum(Units_Sold) as Unit_per_Year
from Purchases
group by Year, Brand;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The above logic is ok if we know that e.g. Brand "A" exists throughout the whole 2010. But if Brand "A" appeared on 18/03/2010 for the first time, and exists until now, then a comparison of Years 2010 and 2011 would not be good enough since for 2010 we are "missing" 3 months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;So what I want to do &lt;/STRONG&gt;is calculate the sum of Units_Sold&amp;nbsp;&lt;SPAN&gt;of a full year of a Brand since its first appearance on the data-set. For instance&lt;/SPAN&gt;&lt;STRONG&gt;:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;for A: the sum from 18/03/2010 until 17/03/2011, then from 18/03/2011 until 17/03/2012, etc.&lt;/P&gt;&lt;P&gt;for B: the sum from 12/07/2010 until 11/07/2011, etc.&lt;/P&gt;&lt;P&gt;and so on for all Brands.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is there a smart way of doing this?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;Fil&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2016 12:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241818#M44836</guid>
      <dc:creator>Filippos</dc:creator>
      <dc:date>2016-01-05T12:57:59Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241819#M44837</link>
      <description>&lt;P&gt;So what your saying is anything min(Date) to min(Date) + 365. &amp;nbsp;What do you want to do for those dates which are after that window?&lt;/P&gt;
&lt;P&gt;Here is an example that sets a flag 1 for within the group, 2 without. &amp;nbsp;You can then sum based on that. &amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  informat date ddmmyy10.;
  input date units_sold brand $ year;
  format date date9.;
datalines;
18/03/2010                5                   A         2010
12/04/2010                2                   A         2010
22/05/2010                1                   A         2010
25/05/2010                7                   A         2010
11/08/2011                5                   A         2011
;
run;

proc sql;
  create table WANT as
  select  A.*,
          case when B.DATE &amp;lt;= A.DATE &amp;lt;= intnx('day',B.DATE,365) then 1
               else 2 end as GRP
  from    WORK.HAVE A
  left join (select distinct BRAND,min(DATE) as DATE from WORK.HAVE group by BRAND) B
  on      A.BRAND=B.BRAND;
quit;&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Jan 2016 13:08:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241819#M44837</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-05T13:08:30Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241823#M44838</link>
      <description>&lt;P&gt;Many thanks RW9 for the quick and accurate reply.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your code seems to do most of the trick. However, in the scenario where we have e.g. 20 years of data, then the GRP variable does not work as it is limited to values of 1 and 2. In such a scenario I would be able to correctly sum only for the 1st year after a brand appears (GRP=1), and for all the rest the sum would not account for 365 days, but for all the rest 19 years (GRP=2).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Do you see what I mean?&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2016 13:44:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241823#M44838</guid>
      <dc:creator>Filippos</dc:creator>
      <dc:date>2016-01-05T13:44:01Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241824#M44839</link>
      <description>&lt;P&gt;How about this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Date :ddmmyy10. Units_Sold Brand $ Year;
format Date ddmmyy10.;
cards;
18/03/2010 5 A 2010
12/04/2010 2 A 2010
22/05/2010 1 A 2010
25/05/2010 7 A 2010
11/08/2011 5 A 2011
12/07/2010 2 B 2010
22/10/2010 1 B 2010
05/05/2011 7 B 2011
;

proc sql;
create table startdates as
select brand, min(date) as startd
from have
group by brand;

create table want as
select a.brand, intck('year', b.startd, a.date, 'c')+1 as YearNumber,
       catx(' ', put(intnx('year', b.startd, calculated YearNumber-1, 's'), ddmmyy10.), '-',
                 put(intnx('year', b.startd, calculated YearNumber, 's')-1, ddmmyy10.)) as Period length=23,
       sum(units_sold) as Units_per_Year
from have a, startdates b
where a.brand=b.brand
group by a.brand, YearNumber, Period;

quit;
    
proc print data=want;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit: Of course you can replace table STARTDATES&amp;nbsp;by an inline view:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.brand, intck('year', b.startd, a.date, 'c')+1 as YearNumber,
       catx(' ', put(intnx('year', b.startd, calculated YearNumber-1, 's'), ddmmyy10.), '-',
                 put(intnx('year', b.startd, calculated YearNumber, 's')-1, ddmmyy10.)) as Period length=23,
       sum(units_sold) as Units_per_Year
from have a, (select brand, min(date) as startd from have group by brand) b
where a.brand=b.brand
group by a.brand, YearNumber, Period;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2016 14:04:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241824#M44839</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-05T14:04:09Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241837#M44840</link>
      <description>&lt;P&gt;Thanks &lt;SPAN class="login-bold"&gt;FreelanceReinhard&lt;/SPAN&gt;&amp;nbsp;this solves my question. I consider this question resolved. I still have a lot to learn in terms of base SAS as some of the functions in your code are new to me (e.g. intck).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As an add-up is there a way to twist your code so that instead of a year, I can sum per 30, 60, or 90 days (Quarterly)?&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jan 2016 12:15:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241837#M44840</guid>
      <dc:creator>Filippos</dc:creator>
      <dc:date>2016-01-11T12:15:57Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241854#M44843</link>
      <description>&lt;P&gt;Yes, both the INTCK and the INTNX function are very&amp;nbsp;flexible and allow for a variety of interval types other than 'year', including 'day', 'week', 'month' and 'qtr' (i.e. quarter) and arbitrary multiples thereof, e.g. 'day30' for 30 days. In addition, there are options to shift the start of the interval. Please see the online help for more details.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So, for 30-day periods you would replace 'year' by 'day30' (and modify the variable names referring to "Year" correpondingly) and would get an output like this (where "Month" means 30 days):&lt;/P&gt;
&lt;PRE&gt;          Month                                 Units_
Brand    Number            Period             per_Month

  A         1      18/03/2010 - 16/04/2010        7
  A         3      17/05/2010 - 15/06/2010        8
  A        18      10/08/2011 - 08/09/2011        5
  B         1      12/07/2010 - 10/08/2010        2
  B         4      10/10/2010 - 08/11/2010        1
  B        10      08/04/2011 - 07/05/2011        7
&lt;/PRE&gt;</description>
      <pubDate>Tue, 05 Jan 2016 16:16:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241854#M44843</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-05T16:16:22Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241861#M44845</link>
      <description>&lt;P&gt;Sorry, was in a meeting. &amp;nbsp;You can actually setup groups very easily for any number you want. &amp;nbsp;Dates are numbers of days since a specific date. &amp;nbsp;Hence if you subtract the min(DATE) off any date, and then floor&amp;nbsp;the result divided by your number you should get groups, i.e.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;&lt;BR /&gt; informat date ddmmyy10.;&lt;BR /&gt; input date units_sold brand $ year;&lt;BR /&gt; format date date9.;&lt;BR /&gt;datalines;&lt;BR /&gt;18/03/2010 5 A 2010&lt;BR /&gt;12/04/2010 2 A 2010&lt;BR /&gt;22/05/2010 1 A 2010&lt;BR /&gt;25/05/2010 7 A 2010&lt;BR /&gt;11/08/2011 5 A 2011&lt;BR /&gt;19/07/2012 5 A 2011&lt;BR /&gt;17/01/2013 5 A 2011&lt;BR /&gt;;&lt;BR /&gt;run;&lt;BR /&gt;proc sql;&lt;BR /&gt; create table WANT as&lt;BR /&gt; select A.*,&lt;BR /&gt; floor((A.DATE - B.DATE) / 365) as GRP&lt;BR /&gt; from WORK.HAVE A&lt;BR /&gt; left join (select distinct BRAND,min(DATE) as DATE from WORK.HAVE group by BRAND) B&lt;BR /&gt; on A.BRAND=B.BRAND&lt;BR /&gt; order by A.BRAND,A.DATE;&lt;BR /&gt;quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 05 Jan 2016 17:07:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241861#M44845</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2016-01-05T17:07:25Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241908#M44861</link>
      <description>Amazing, many thanks!</description>
      <pubDate>Tue, 05 Jan 2016 20:21:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241908#M44861</guid>
      <dc:creator>Filippos</dc:creator>
      <dc:date>2016-01-05T20:21:19Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241978#M44891</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Date :ddmmyy10. Units_Sold Brand $ Year;
format Date ddmmyy10.;
cards;
18/03/2010 5 A 2010
12/04/2010 2 A 2010
22/05/2010 1 A 2010
25/05/2010 7 A 2010
11/08/2011 5 A 2011
12/07/2010 2 B 2010
22/10/2010 1 B 2010
05/05/2011 7 B 2011
;
run;

data temp;
 set have;
 by Brand;
 retain temp;
 if first.Brand then do;
  temp=Date;
 end;
 if Date gt intnx('year',temp,1,'s')-1 then temp=intnx('year',temp,1,'s');
 format temp ddmmyy10.;
run;
proc summary data=temp;
by Brand temp;
var Units_Sold ;
output out=want sum=sum_count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jan 2016 03:15:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241978#M44891</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-01-06T03:15:51Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241979#M44892</link>
      <description>&lt;P&gt;This could be more robust .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Date :ddmmyy10. Units_Sold Brand $ Year;
format Date ddmmyy10.;
cards;
18/03/2010 5 A 2010
12/04/2010 2 A 2010
22/05/2010 1 A 2010
25/05/2010 7 A 2010
11/08/2011 5 A 2011
12/07/2010 2 B 2010
22/10/2010 1 B 2010
05/05/2011 7 B 2011
;
run;

data temp;
 set have;
 by Brand;
 retain temp;
 if first.Brand then temp=Date;
 if Date gt intnx('year',temp,1,'s')-1 then temp=mdy(month(temp),day(temp),year(Date));
 format temp ddmmyy10.;
run;
proc summary data=temp;
by Brand temp;
var Units_Sold ;
output out=want sum=sum_count;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 06 Jan 2016 03:25:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/241979#M44892</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-01-06T03:25:18Z</dc:date>
    </item>
    <item>
      <title>Re: Aggregating for 365 Days, and not for Full Calendar Year</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/242681#M45058</link>
      <description>&lt;P&gt;&lt;SPAN style="line-height: 20px;"&gt;Apologies for the late reply Ksharp&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks, this seems to also do the trick and it's nice to see different versions of the solution for the same problem.&lt;/P&gt;</description>
      <pubDate>Mon, 11 Jan 2016 12:18:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Aggregating-for-365-Days-and-not-for-Full-Calendar-Year/m-p/242681#M45058</guid>
      <dc:creator>Filippos</dc:creator>
      <dc:date>2016-01-11T12:18:36Z</dc:date>
    </item>
  </channel>
</rss>

