<?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: Compounding return using lag function in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320930#M61944</link>
    <description>&lt;P&gt;PrOC EXPAND is definitely the way to go. But for those without SAS/ETS (and therefore without proc expand), here is a data step solution, with by groups,&amp;nbsp;to get&amp;nbsp;rolling returns of any size (9 in this case) without recourse to multiple lag functions.&amp;nbsp; Just a lag for the window size (lag9 here):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp; A rolling cum return (NEED) is generated by multiplying by the current rolling return and dividing by the lag9 return (as long as the lag9 record is for the same id).&lt;/LI&gt;
&lt;LI&gt;The IFN function avoids the problem of having a lag function executed in an IF statement.&amp;nbsp; IFN evaluates both outcomes (arguments 2 and 3 of the function) regardless of the evaluation of the first argument.&amp;nbsp; This means the lag queue (argument 2 of IFN) is&amp;nbsp;updated whether or not its results are used.&amp;nbsp; In other words, it&amp;nbsp; avoids the tedious lagging and conditional use of the lagged value.&amp;nbsp; I.e. it avoids this:&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; ret9=lag9(ret);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if lag9(id)=id then divisor=1+ret9;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; else divisor=1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; need=&amp;nbsp;&amp;nbsp; (need+1)*(1+ret)/divisor - 1;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want (drop=need);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; retain need&amp;nbsp;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if first.id then need=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; need =&amp;nbsp; (1+need)*(1+ret) /&amp;nbsp;(1+ifn(lag9(id)=id,lag9(ret),0))&amp;nbsp; - 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if lag9(id)=id then rolling_return9=need;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
    <pubDate>Fri, 23 Dec 2016 14:23:02 GMT</pubDate>
    <dc:creator>mkeintz</dc:creator>
    <dc:date>2016-12-23T14:23:02Z</dc:date>
    <item>
      <title>Compounding return using lag function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320669#M61939</link>
      <description>&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/6460i8B391C655B6DB648/image-size/original?v=v2&amp;amp;px=-1" border="0" alt="outcome.png" title="outcome.png" /&gt;&lt;/P&gt;&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm trying to execute the below code to essentially compund 3 month return ..essentially rolling 3 month comp return for each ID but found a peculiar issue while calculating using lag3 function as you may note below. I've copied the output and also shown what the expected outcome should be for comp3_ret. &amp;nbsp;Could you kindly let me know where I'm missing out:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;FONT color="#000080"&gt;data pras2;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;input DAT1 mmddyy11. ID $ RET VAR1 VAR2 FLAG ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;cards;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;12/31/2010 A 0.20 25 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;11/30/2010 A 0.20 15 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;01/31/2011 A 0.20 35 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;08/31/2010 A 0.20 35 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;10/31/2010 A 0.20 20 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;09/30/2010 A 0.20 35 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;12/31/2010 AA 0.25 25 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;11/30/2010 AA 0.25 15 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;01/31/2011 AA 0.25 35 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;08/31/2010 AA 0.25 35 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;10/31/2010 AA 0.25 20 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;09/30/2010 AA 0.25 35 100 1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080"&gt;PROC SORT DATA = PRAS2; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;BY ID DAT1;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080"&gt;data pras1;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;set pras2;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;by ID DAT1;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;retain comp;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;if first.id then comp = 1; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;comp = comp*(1+ret);&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;/**** Trailing 3 month compounded return ****/&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;if (first.id eq 0) and (ID eq lag2(ID)) then comp3_ret = comp - 1 ;&lt;/FONT&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;if (first.ID eq 0) and (ID eq lag3(ID)) then comp3_ret = (comp/lag3(comp))-1 ;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;run;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT color="#000080"&gt;PROC PRINT DATA = PRAS1;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;format DAT1 MMDDYY10. ;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;RUN;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;output : (see attached img)&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13276iC0B5A01578D93E46/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="outcome.png" title="outcome.png" /&gt;</description>
      <pubDate>Thu, 22 Dec 2016 04:09:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320669#M61939</guid>
      <dc:creator>Inquisitive101</dc:creator>
      <dc:date>2016-12-22T04:09:07Z</dc:date>
    </item>
    <item>
      <title>Re: Compounding return using lag function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320672#M61940</link>
      <description>&lt;P&gt;Create a counter that counts the records per ID. If counter is greater than 3 then do calculations.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also calculate LAG outside of the IF statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;LAG doesn't work the way you think it should, is the quick answer.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Longer explanation&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://support.sas.com/resources/papers/proceedings09/055-2009.pdf" target="_blank"&gt;http://support.sas.com/resources/papers/proceedings09/055-2009.pdf&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Dec 2016 04:31:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320672#M61940</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-12-22T04:31:47Z</dc:date>
    </item>
    <item>
      <title>Re: Compounding return using lag function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320674#M61941</link>
      <description>&lt;PRE&gt;
Not sure what you are looking for.


 data pras2;
input DAT1 mmddyy11. ID $ RET VAR1 VAR2 FLAG ;
cards;
12/31/2010 A 0.20 25 100 1
11/30/2010 A 0.20 15 100 1
01/31/2011 A 0.20 35 100 1
08/31/2010 A 0.20 35 100 1
10/31/2010 A 0.20 20 100 1
09/30/2010 A 0.20 35 100 1
12/31/2010 AA 0.25 25 100 1
11/30/2010 AA 0.25 15 100 1
01/31/2011 AA 0.25 35 100 1
08/31/2010 AA 0.25 35 100 1
10/31/2010 AA 0.25 20 100 1
09/30/2010 AA 0.25 35 100 1
;
run;

PROC SORT DATA = PRAS2; 
BY ID DAT1;
data pras1;
set pras2;
by ID DAT1;
retain comp found comp3_ret;
if first.id then do;comp = 1; found=0;comp3_ret=.;end;
comp = comp*(1+ret);

/**** Trailing 3 month compounded return ****/
if (first.id eq 0) and (ID eq lag2(ID)) and not found then do;
 comp3_ret = comp - 1 ;found=1;
end;
drop found;
run;


&lt;/PRE&gt;</description>
      <pubDate>Thu, 22 Dec 2016 05:36:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320674#M61941</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-12-22T05:36:21Z</dc:date>
    </item>
    <item>
      <title>Re: Compounding return using lag function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320694#M61942</link>
      <description>&lt;P&gt;Trying to change as little as possible in the existing program, the DATA step would become:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080"&gt;data pras1;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;set pras2;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;by ID DAT1;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;id_back2 = lag2(id);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;id_back3 = lag3(id);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;retain comp;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;if first.id then comp = 1; &lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;comp = comp*(1+ret);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#FF0000"&gt;comp_back3 = lag3(comp);&lt;/FONT&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;/**** Trailing 3 month compounded return ****/&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;if &amp;nbsp;&lt;FONT color="#FF0000"&gt;(ID eq id_back2)&lt;/FONT&gt; then comp3_ret = comp - 1 ;&lt;/FONT&gt;&lt;BR /&gt;&lt;STRONG&gt;&lt;FONT color="#000080"&gt;if &lt;FONT color="#FF0000"&gt;(ID eq id_back3)&lt;/FONT&gt; then comp3_ret = (comp/&lt;FONT color="#FF0000"&gt;comp_back3&lt;/FONT&gt;)-1 ;&lt;/FONT&gt;&lt;/STRONG&gt;&lt;BR /&gt;&lt;FONT color="#000080"&gt;run;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT color="#000080"&gt;You don't need to check for first.ID=0. &amp;nbsp;If ID is equal to ID from a couple of records previously, that can only happen when first.ID=0.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Dec 2016 08:23:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320694#M61942</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2016-12-22T08:23:14Z</dc:date>
    </item>
    <item>
      <title>Re: Compounding return using lag function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320747#M61943</link>
      <description>&lt;P&gt;thanks; but the objective is to replicate this for long duration comp. returns like say 6, 9 &amp;amp; 12 month returns. So I was hesitant to declare that many lag variabels upfront using arrays,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Meanwhile, got this sorted out using &lt;STRONG&gt;proc expand&lt;/STRONG&gt; from another post&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;"convert ret=comp_ret_3 / transformout=(+1 log reverse movsum 3 exp -1 ); &amp;nbsp;"&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Dec 2016 15:14:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320747#M61943</guid>
      <dc:creator>Inquisitive101</dc:creator>
      <dc:date>2016-12-22T15:14:45Z</dc:date>
    </item>
    <item>
      <title>Re: Compounding return using lag function</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320930#M61944</link>
      <description>&lt;P&gt;PrOC EXPAND is definitely the way to go. But for those without SAS/ETS (and therefore without proc expand), here is a data step solution, with by groups,&amp;nbsp;to get&amp;nbsp;rolling returns of any size (9 in this case) without recourse to multiple lag functions.&amp;nbsp; Just a lag for the window size (lag9 here):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Notes:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;&amp;nbsp; A rolling cum return (NEED) is generated by multiplying by the current rolling return and dividing by the lag9 return (as long as the lag9 record is for the same id).&lt;/LI&gt;
&lt;LI&gt;The IFN function avoids the problem of having a lag function executed in an IF statement.&amp;nbsp; IFN evaluates both outcomes (arguments 2 and 3 of the function) regardless of the evaluation of the first argument.&amp;nbsp; This means the lag queue (argument 2 of IFN) is&amp;nbsp;updated whether or not its results are used.&amp;nbsp; In other words, it&amp;nbsp; avoids the tedious lagging and conditional use of the lagged value.&amp;nbsp; I.e. it avoids this:&amp;nbsp;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; ret9=lag9(ret);&lt;BR /&gt;&amp;nbsp;&amp;nbsp; if lag9(id)=id then divisor=1+ret9;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; else divisor=1;&lt;BR /&gt;&amp;nbsp;&amp;nbsp; need=&amp;nbsp;&amp;nbsp; (need+1)*(1+ret)/divisor - 1;&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want (drop=need);&lt;/P&gt;
&lt;P&gt;&amp;nbsp; set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; retain need&amp;nbsp;;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if first.id then need=0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; need =&amp;nbsp; (1+need)*(1+ret) /&amp;nbsp;(1+ifn(lag9(id)=id,lag9(ret),0))&amp;nbsp; - 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if lag9(id)=id then rolling_return9=need;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Dec 2016 14:23:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Compounding-return-using-lag-function/m-p/320930#M61944</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-12-23T14:23:02Z</dc:date>
    </item>
  </channel>
</rss>

