<?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 moving total amount for one year with group by facility in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-total-amount-for-one-year-with-group-by/m-p/618646#M181516</link>
    <description>&lt;P&gt;Uma:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Precisely because your date ranges change from record to record, using a macrovar approach is very problematic.&amp;nbsp; I suggest you consider using a DATA step.&amp;nbsp; It should&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Read in each customer's records sequentially (i.e. ordered by customer/date-of-transaction).&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Capture the first qualifying transaction&lt;/LI&gt;
&lt;LI&gt;Use the captured date to establish a 12-month range.&amp;nbsp; You can RETAIN these values over the rest of that customer's records (which will replace the need for macrovars in this case).&lt;BR /&gt;&lt;BR /&gt;Question:&amp;nbsp; Do you have any records of transactions OUTSIDE of campaign dates?&amp;nbsp; I assume not.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Make a new variable&amp;nbsp; CUM_PRIOR_AMT containing the accumulated TRAN_AMT only for prior qualifying records of that customer.&lt;/LI&gt;
&lt;LI&gt;Output only transactions within the dynamically generated 12-month range.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an untested program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (where=(tran_dt_during_cpgn^=.));
  by customer_id;

  retain _beg_date _end_date  .;
  if first.customer_id then do;&lt;BR /&gt;    _beg_date=tran_dt_during_cpgn;
    _end_date=intnx('month',_beg_date,12,'same');
    cum_prior_amt=0;
  end;
  if _beg_date &amp;lt;= tran_dt_during_cpgn &amp;lt;= _end_date;
  output;
  cum_prior_amt+tran_amt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note I use the WHERE clause because I assume that non-missing TRAN_DT_DURING_CPGN records &lt;EM&gt;&lt;STRONG&gt;are the only valid candidate transactions&lt;/STRONG&gt;&lt;/EM&gt; for your task.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because I assume that for each customer, all the &lt;EM&gt;&lt;STRONG&gt;non-missing TRAN_DT_DURING_CPGN are in ascending order&lt;/STRONG&gt;&lt;/EM&gt;, then the very first record satisfying the WHERE filter for each customer marks the start of your 12-month time span.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The INTNX function adds 12-moths to the _beg_date to get _end_date.&amp;nbsp; The "same" parameters tells INTNX to return a day-of-month that is the same as the day-of-month in _BEG_DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The subsetting IF keeps only records during the retained 12-month period.&amp;nbsp; So later records are ignored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program OUTPUTs the record-in-hand prior to updating CUM_PRIOR_AMT.&amp;nbsp; That's how to delay including the current TRAN_AMT in the cumulative total.&lt;/P&gt;</description>
    <pubDate>Mon, 20 Jan 2020 18:32:22 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2020-01-20T18:32:22Z</dc:date>
    <item>
      <title>How to calculate moving total amount for one year with group by facility</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-total-amount-for-one-year-with-group-by/m-p/618613#M181500</link>
      <description>&lt;P&gt;Hi Team,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to calculate total amount spend by each customer for 365 days where 12 months should be calculated from first purchase made by customer during the campaign window.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i have calculated the first purchase date during the campaign window but unable to pass the dates into where clause.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kindly have a look into sample data&lt;/P&gt;
&lt;TABLE width="627"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="97"&gt;CPGNID&lt;/TD&gt;
&lt;TD width="96"&gt;CUSTOMER_ID&lt;/TD&gt;
&lt;TD width="144"&gt;CPGN_STRT_DT&lt;/TD&gt;
&lt;TD width="144"&gt;CPGN_END_DT&lt;/TD&gt;
&lt;TD width="146"&gt;Tran_dt_During_CPGN&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;106248162U&lt;/TD&gt;
&lt;TD&gt;A1000000050&lt;/TD&gt;
&lt;TD&gt;27-Dec-19&lt;/TD&gt;
&lt;TD&gt;3-Jan-20&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;106249382U&lt;/TD&gt;
&lt;TD&gt;A1000000050&lt;/TD&gt;
&lt;TD&gt;3-Jan-20&lt;/TD&gt;
&lt;TD&gt;10-Jan-20&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;106252082U&lt;/TD&gt;
&lt;TD&gt;A1000000050&lt;/TD&gt;
&lt;TD&gt;19-Jan-20&lt;/TD&gt;
&lt;TD&gt;26-Jan-20&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;106248382U&lt;/TD&gt;
&lt;TD&gt;A1000001868&lt;/TD&gt;
&lt;TD&gt;28-Dec-19&lt;/TD&gt;
&lt;TD&gt;4-Jan-20&lt;/TD&gt;
&lt;T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;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD&gt;106252682U&lt;/TD&gt;
&lt;TD&gt;A1000100205&lt;/TD&gt;
&lt;TD&gt;19-Jan-20&lt;/TD&gt;
&lt;TD&gt;26-Jan-20&lt;/TD&gt;
&lt;TD&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Transaction table would have CUSTOMER_ID, TRAN_DT and TRAN_AMT&amp;nbsp; (historical to t-1 date)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I could try with :&lt;/P&gt;
&lt;P&gt;DATA _null_;&lt;/P&gt;
&lt;P&gt;call symput('SD',"'"||(STRIP(put(today()-1,date9.)))||"'d");&lt;BR /&gt;call symput('SD12',"'"||(STRIP(put(intnx('Month',today(),-12,'S'),Date9.)))||"'d");&lt;/P&gt;
&lt;P&gt;Run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Proc sql;&lt;BR /&gt;create table Revenue&amp;nbsp; as&lt;BR /&gt;select A.CUSTOMER_ID&lt;BR /&gt;, SUM(B.TRAN_AMT) AS REV_12M&lt;/P&gt;
&lt;P&gt;from TEMP A&lt;BR /&gt;left join TRAN_TABLE B&lt;BR /&gt;on A.CUSTOMER_ID =B.CUSTOMER_ID&lt;BR /&gt;Where B.TRAN_DT GE &amp;amp;SD12. and B.TRAN_DT LE &amp;amp;SD.&lt;BR /&gt;Group by 1 ;&lt;BR /&gt;Quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here issue is with 'SD' macro variable as it is changing for each customer so i am not able to think about the logic.&lt;/P&gt;
&lt;P&gt;Rev_12M get calculated only for those customer who has shopped during that campaign period so customer *303 would have revenue only for one campaign which is&amp;nbsp;106248242U and same for customer&amp;nbsp;A1000100052 revenue would be different as first time where clause window should be 01JAN2020 -365 days and for second time 03JAN2020 -365.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Kindly help.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Regards,&lt;/P&gt;
&lt;P&gt;Uma Shanker Saini&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jan 2020 17:18:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-total-amount-for-one-year-with-group-by/m-p/618613#M181500</guid>
      <dc:creator>umashankersaini</dc:creator>
      <dc:date>2020-01-20T17:18:04Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate moving total amount for one year with group by facility</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-total-amount-for-one-year-with-group-by/m-p/618646#M181516</link>
      <description>&lt;P&gt;Uma:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Precisely because your date ranges change from record to record, using a macrovar approach is very problematic.&amp;nbsp; I suggest you consider using a DATA step.&amp;nbsp; It should&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;Read in each customer's records sequentially (i.e. ordered by customer/date-of-transaction).&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;Capture the first qualifying transaction&lt;/LI&gt;
&lt;LI&gt;Use the captured date to establish a 12-month range.&amp;nbsp; You can RETAIN these values over the rest of that customer's records (which will replace the need for macrovars in this case).&lt;BR /&gt;&lt;BR /&gt;Question:&amp;nbsp; Do you have any records of transactions OUTSIDE of campaign dates?&amp;nbsp; I assume not.&lt;BR /&gt;&lt;BR /&gt;&lt;/LI&gt;
&lt;LI&gt;Make a new variable&amp;nbsp; CUM_PRIOR_AMT containing the accumulated TRAN_AMT only for prior qualifying records of that customer.&lt;/LI&gt;
&lt;LI&gt;Output only transactions within the dynamically generated 12-month range.&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is an untested program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=_:);
  set have (where=(tran_dt_during_cpgn^=.));
  by customer_id;

  retain _beg_date _end_date  .;
  if first.customer_id then do;&lt;BR /&gt;    _beg_date=tran_dt_during_cpgn;
    _end_date=intnx('month',_beg_date,12,'same');
    cum_prior_amt=0;
  end;
  if _beg_date &amp;lt;= tran_dt_during_cpgn &amp;lt;= _end_date;
  output;
  cum_prior_amt+tran_amt;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Note I use the WHERE clause because I assume that non-missing TRAN_DT_DURING_CPGN records &lt;EM&gt;&lt;STRONG&gt;are the only valid candidate transactions&lt;/STRONG&gt;&lt;/EM&gt; for your task.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Because I assume that for each customer, all the &lt;EM&gt;&lt;STRONG&gt;non-missing TRAN_DT_DURING_CPGN are in ascending order&lt;/STRONG&gt;&lt;/EM&gt;, then the very first record satisfying the WHERE filter for each customer marks the start of your 12-month time span.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The INTNX function adds 12-moths to the _beg_date to get _end_date.&amp;nbsp; The "same" parameters tells INTNX to return a day-of-month that is the same as the day-of-month in _BEG_DATE.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The subsetting IF keeps only records during the retained 12-month period.&amp;nbsp; So later records are ignored.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The program OUTPUTs the record-in-hand prior to updating CUM_PRIOR_AMT.&amp;nbsp; That's how to delay including the current TRAN_AMT in the cumulative total.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Jan 2020 18:32:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-total-amount-for-one-year-with-group-by/m-p/618646#M181516</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-01-20T18:32:22Z</dc:date>
    </item>
  </channel>
</rss>

