<?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: Rolling 12 months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587120#M167647</link>
    <description>&lt;P&gt;I have no idea about SAS VA and have never used it. The LAG usage here is mere simple arithmetic. I suggest using that. The number of columns is immaterial as your monthyear variable is continuous. Programming using continuous variable is generally easy&lt;/P&gt;</description>
    <pubDate>Mon, 09 Sep 2019 00:10:57 GMT</pubDate>
    <dc:creator>novinosrin</dc:creator>
    <dc:date>2019-09-09T00:10:57Z</dc:date>
    <item>
      <title>Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587108#M167636</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;&lt;P&gt;I don't have sas/ets and was trying to see if this question has been raised and answered previously.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am trying to create a rolling 12 month total which calculates the sum of the last 12 months.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Month&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;sum(rolling 12 months)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;April 2018&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;May 2018&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;June 2018&lt;/TD&gt;&lt;TD&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gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;June 2019&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;TD&gt;114&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;July 2019&lt;/TD&gt;&lt;TD&gt;16&lt;/TD&gt;&lt;TD&gt;126&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;August 2019&lt;/TD&gt;&lt;TD&gt;17&lt;/TD&gt;&lt;TD&gt;138&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;September 2019&lt;/TD&gt;&lt;TD&gt;18&lt;/TD&gt;&lt;TD&gt;150&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;October 2019&lt;/TD&gt;&lt;TD&gt;19&lt;/TD&gt;&lt;TD&gt;162&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;November 2019&lt;/TD&gt;&lt;TD&gt;20&lt;/TD&gt;&lt;TD&gt;174&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;</description>
      <pubDate>Sun, 08 Sep 2019 23:00:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587108#M167636</guid>
      <dc:creator>Ramakanthkrovi</dc:creator>
      <dc:date>2019-09-08T23:00:19Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587111#M167638</link>
      <description>&lt;P&gt;12 month windows super simple , however do you have an ID variable or is it just that one set? i.e no groups?&lt;/P&gt;</description>
      <pubDate>Sun, 08 Sep 2019 23:18:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587111#M167638</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-08T23:18:37Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587112#M167639</link>
      <description>&lt;P&gt;No ID variable. for every month I just need the actual (a) and sum(a to a-11).&lt;/P&gt;</description>
      <pubDate>Sun, 08 Sep 2019 23:42:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587112#M167639</guid>
      <dc:creator>Ramakanthkrovi</dc:creator>
      <dc:date>2019-09-08T23:42:37Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587113#M167640</link>
      <description>&lt;P&gt;Hi &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151521"&gt;@Ramakanthkrovi&lt;/a&gt;&amp;nbsp; &amp;nbsp;Thank you. The logic is very simple.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The window condition&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;on intnx('mon',a.month,-11)&amp;lt;=b.month&amp;lt;=a.month&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;according to your needs below&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Month :monyy7.	A ;
format month monyy7.;
cards;
Apr-18	1
May-18	2
Jun-18	3
Jul-18	4
Aug-18	5
Sep-18	6
Oct-18	7
Nov-18	8
Dec-18	9
Jan-19	10
Feb-19	11
Mar-19	12
Apr-19	13
May-19	14
Jun-19	15
Jul-19	16
Aug-19	17
Sep-19	18
Oct-19	19
Nov-19	20
;

proc sql;
create table want as
select a.* ,sum(b.a) as sum 
from have a left join have b
on intnx('mon',a.month,-11)&amp;lt;=b.month&amp;lt;=a.month
group by a.month ,a.a
order by a.month;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 00:49:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587113#M167640</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-09T00:49:12Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587114#M167641</link>
      <description>&lt;P&gt;Or Use lag&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Month :monyy7.	A ;
format month monyy7.;
cards;
Apr-18	1
May-18	2
Jun-18	3
Jul-18	4
Aug-18	5
Sep-18	6
Oct-18	7
Nov-18	8
Dec-18	9
Jan-19	10
Feb-19	11
Mar-19	12
Apr-19	13
May-19	14
Jun-19	15
Jul-19	16
Aug-19	17
Sep-19	18
Oct-19	19
Nov-19	20
;
data want;
set have;
s+a;
k=lag12(s);
want=s-sum(k,0);
drop k s;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 08 Sep 2019 23:53:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587114#M167641</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-08T23:53:09Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587115#M167642</link>
      <description>&lt;P&gt;If you have PROC EXPAND in your SAS license, use that, it's a lot easier than having to write a program to do it yourself.&lt;/P&gt;</description>
      <pubDate>Sun, 08 Sep 2019 23:58:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587115#M167642</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-09-08T23:58:35Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587116#M167643</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/10892"&gt;@PaigeMiller&lt;/a&gt;&amp;nbsp;-&amp;nbsp;&lt;/P&gt;&lt;P&gt;no, unfortunately we don't have sas/ets licence.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I was also looking to do this in SAS VA (7.4) if possible. is it simpler than writing a program?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 00:01:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587116#M167643</guid>
      <dc:creator>Ramakanthkrovi</dc:creator>
      <dc:date>2019-09-09T00:01:58Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587117#M167644</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;- The lag worked great.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Is it simpler to do this way if we have multiple columns or is there a way I can do it in SAS VA 7.4?&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 00:03:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587117#M167644</guid>
      <dc:creator>Ramakanthkrovi</dc:creator>
      <dc:date>2019-09-09T00:03:05Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587120#M167647</link>
      <description>&lt;P&gt;I have no idea about SAS VA and have never used it. The LAG usage here is mere simple arithmetic. I suggest using that. The number of columns is immaterial as your monthyear variable is continuous. Programming using continuous variable is generally easy&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 00:10:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587120#M167647</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-09T00:10:57Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587122#M167648</link>
      <description>&lt;P&gt;VA has built in functionality to handle date dimensions - is your variable formatted and identified as a date in VA?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/3-FAQs-on-date-calculations-in-SAS-Visual-Analytics/ta-p/251260" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/3-FAQs-on-date-calculations-in-SAS-Visual-Analytics/ta-p/251260&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 00:30:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587122#M167648</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-09-09T00:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587125#M167649</link>
      <description>&lt;P&gt;Hello &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151521"&gt;@Ramakanthkrovi&lt;/a&gt;&amp;nbsp; Some extra fun with temp array&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;

data have;
input Month :monyy7.	A ;
format month monyy7.;
cards;
Apr-18	1
May-18	2
Jun-18	3
Jul-18	4
Aug-18	5
Sep-18	6
Oct-18	7
Nov-18	8
Dec-18	9
Jan-19	10
Feb-19	11
Mar-19	12
Apr-19	13
May-19	14
Jun-19	15
Jul-19	16
Aug-19	17
Sep-19	18
Oct-19	19
Nov-19	20
;

data want;
 do _n_=1 by 1 until(z); 
  set have end=z;
  array t(0:11) _temporary_;
  t(mod(_n_,12))=a;
  s=sum(of t(*));
  output;
 end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 00:40:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587125#M167649</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-09T00:40:35Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587128#M167651</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
input Month :monyy7.	A ;
format month monyy7.;
cards;
Apr-18	1
May-18	2
Jun-18	3
Jul-18	4
Aug-18	5
Sep-18	6
Oct-18	7
Nov-18	8
Dec-18	9
Jan-19	10
Feb-19	11
Mar-19	12
Apr-19	13
May-19	14
Jun-19	15
Jul-19	16
Aug-19	17
Sep-19	18
Oct-19	19
Nov-19	20
;
data want;
if _n_=1 then do;
  if 0 then set have(rename=(month=_m a=_a));
   dcl hash H (dataset:'have(rename=(month=_m a=_a))') ;
   h.definekey  ("_m") ;
   h.definedata ("_m","_a") ;
   h.definedone () ;
   dcl hiter hi('h');
 end;
 set have;
 do while(hi.next()=0);
  if intnx('mon',month,-11)&amp;lt;=_m&amp;lt;=month then sum=sum(sum,_a);
 end;
 drop _:;
 run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 09 Sep 2019 01:03:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587128#M167651</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-09T01:03:45Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587129#M167652</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I had to do this to get a rolling 12 month figure. the article is not informative for what I was trying to do. otherwise it works.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;RelativePeriod(_Sum_, 'NDC'n, _ApplyAllFilters_, 'Reg_Mon'n, _ByMonth_, 0, _Full_, {Date}) + RelativePeriod(_Sum_, 'NDC'n, _ApplyAllFilters_, 'Reg_Mon'n, _ByMonth_, -1, _Full_, {Date}) + RelativePeriod(_Sum_, 'NDC'n, _ApplyAllFilters_, 'Reg_Mon'n, _ByMonth_, -2, _Full_, {Date}) + RelativePeriod(_Sum_, 'NDC'n, _ApplyAllFilters_, 'Reg_Mon'n, _ByMonth_, -3, _Full_, {Date})&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;+ ...&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;+RelativePeriod(_Sum_, 'NDC'n, _ApplyAllFilters_, 'Reg_Mon'n, _ByMonth_, -11, _Full_, {Date}) + RelativePeriod(_Sum_, 'NDC'n, _ApplyAllFilters_, 'Reg_Mon'n, _ByMonth_, -12, _Full_, {Date})&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 02:43:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587129#M167652</guid>
      <dc:creator>Ramakanthkrovi</dc:creator>
      <dc:date>2019-09-09T02:43:00Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587130#M167653</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;- if there is an ID variable, what will be the impact on the code?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;TABLE&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;Region&lt;/TD&gt;&lt;TD&gt;Month&lt;/TD&gt;&lt;TD&gt;A&lt;/TD&gt;&lt;TD&gt;sum(rolling 12 months)&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;April 2018&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;May 2018&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;June 2018&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;July 2018&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;August 2018&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;15&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;September 2018&lt;/TD&gt;&lt;TD&gt;6&lt;/TD&gt;&lt;TD&gt;21&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;October 2018&lt;/TD&gt;&lt;TD&gt;7&lt;/TD&gt;&lt;TD&gt;28&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;November 2018&lt;/TD&gt;&lt;TD&gt;8&lt;/TD&gt;&lt;TD&gt;36&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;December 2018&lt;/TD&gt;&lt;TD&gt;9&lt;/TD&gt;&lt;TD&gt;45&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;January 2019&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;55&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;February 2019&lt;/TD&gt;&lt;TD&gt;11&lt;/TD&gt;&lt;TD&gt;66&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;March 2019&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;78&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;April 2019&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;90&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;Australia&lt;/TD&gt;&lt;TD&gt;May 2019&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;102&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NZ&lt;/TD&gt;&lt;TD&gt;April 2018&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NZ&lt;/TD&gt;&lt;TD&gt;May 2018&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR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TR&gt;&lt;TR&gt;&lt;TD&gt;NZ&lt;/TD&gt;&lt;TD&gt;March 2019&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;TD&gt;78&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NZ&lt;/TD&gt;&lt;TD&gt;April 2019&lt;/TD&gt;&lt;TD&gt;13&lt;/TD&gt;&lt;TD&gt;90&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;NZ&lt;/TD&gt;&lt;TD&gt;May 2019&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;102&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 02:51:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587130#M167653</guid>
      <dc:creator>Ramakanthkrovi</dc:creator>
      <dc:date>2019-09-09T02:51:58Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587232#M167703</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/151521"&gt;@Ramakanthkrovi&lt;/a&gt;&amp;nbsp; Now you are talking. I asked earlier the same and you categorically said NO. &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyways, let us know which solution in (SQL,LAG,ARRAY,HASH) you want to use and we shall work with that alone. Plus, I got the initial impression that you didn't really seem to go for a programming solution and rather a VA solution. So please think over and ask your teammates(senior)/boss at work as to what should be done and how it should be done.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Finally, while the slight modification is rather too simple however would you/your team be able to maintain,modify and update programming solutions considering you may be completely from a different background as a whole. Believe it or not, at my place of work any of the advance techniques are NOT welcome coz many even prefer 1000 lines of hard coding and some seniors deem that as "&lt;EM&gt;different&lt;/EM&gt;" coding style while I call it "&lt;EM&gt;substandard&lt;/EM&gt;". lol &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 13:39:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587232#M167703</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2019-09-09T13:39:46Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587382#M167768</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;&amp;nbsp;- sorry about that.&amp;nbsp;&lt;/P&gt;&lt;P&gt;In the data that I have, there is no ID variable and so I replied that the lag worked which it did. I am just curious to know how having an ID variable will impact the code as I assumed it to be same as using a by statement.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Apologies about that. I am trying to learn this thing to an extent where I don't have to come back to the forum if I do get a request sooner or later to include an ID variable.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 09 Sep 2019 23:21:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587382#M167768</guid>
      <dc:creator>Ramakanthkrovi</dc:creator>
      <dc:date>2019-09-09T23:21:45Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587391#M167771</link>
      <description>&lt;P&gt;If you have an ID variable, then you need a way to prevent the&amp;nbsp;initial sums of one ID from being contaminated by data from the prior ID - i.e., whenever you start a new ID, the sum has to be reset to zero or missing:&amp;nbsp; And in addition, while the rolling sum requires subtracting the lag12(A) value, it should do so only when the lag12(region) is the same as the current region.&amp;nbsp; I.e.:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by region;
  if first.region then sum_a=.;
  sum_a + a + -1*ifn(lag12(region)=region,lag12(a),0);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;This uses the following features of SAS&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp;The BY statement tells sas to expect the data to be sorted, and to establish 2 dummy variables (first.region and last.region) that inform you whether the observations in hand is the start or end of a distinct region.&lt;/LI&gt;
&lt;LI&gt;Statements like&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; variable + expression + expression + ...&lt;BR /&gt;(note the lack of an equals sign) are SUM statement which (a) add the values of the variable (and the expressions - which themselves can be variables) and put the result in the variable.&amp;nbsp; It also tells SAS to retain the variable, enabling your rolling sum.&lt;/LI&gt;
&lt;LI&gt;The IFN function tests the first argument.&amp;nbsp; If true (i.e. the current obs is the 13th or later within a region), then IFN returns the 2nd argument: lag12(a).&amp;nbsp; Otherwise it returns the 3rd argument (0).&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;But you ask about doing this for a set of variables (say A, B, and C).&amp;nbsp; The natural SAS approach is to declare A B and C as an array, and to define an analogous array with variables SUMA, SUMB, and SUMC.&amp;nbsp; You might be tempted to do this, which just puts the sum statement (and it associated IFN and LAG functions) inside a loop over the 3 variables A, B, C.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by region;
  array vars {3} a b c;
  array sums {3} suma sumb  sumc;
  if first.region then call missing(of sums{*});

  do v=1 to 3;
    sums{v} + vars{v} + -1*ifn(lag12(region)=region,lag12(vars{v}),0);
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But this would give the wrong result, because the "LAG12(region)" function would be run 3 times for each operation.&amp;nbsp; The LAG function is a FIFO queue updater, so in this case it would step through&amp;nbsp;the underlying history of region values 3 times as fast as you want.&amp;nbsp; The best option is to take the LAG12(region) evaluation outside of the loop:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_LGREG12 v);
  set have;
  by region;
  array vars {3} a b c;
  array sums {3} suma sumb  sumc;
  if first.region then call missing(of sums{*});

  _LGREG12=lag12(region);
  do v=1 to 3;
    sums{v} + vars{v} + -1*ifn(_LGREG12=region,lag12(vars{v}),0);
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;regards,&lt;/P&gt;
&lt;P&gt;Mark&lt;/P&gt;</description>
      <pubDate>Tue, 10 Sep 2019 01:03:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/587391#M167771</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2019-09-10T01:03:46Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling 12 months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/619430#M181851</link>
      <description>Can you share the same SQL code with the use of ID variable</description>
      <pubDate>Thu, 23 Jan 2020 08:18:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Rolling-12-months/m-p/619430#M181851</guid>
      <dc:creator>rekhaa</dc:creator>
      <dc:date>2020-01-23T08:18:01Z</dc:date>
    </item>
  </channel>
</rss>

