<?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 Rolling cumulative 3-month and 9-month returns in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Rolling-cumulative-3-month-and-9-month-returns/m-p/68327#M19571</link>
    <description>Hi, I need to compute rolling 3-month and 9-month returns. I wrote this code but it is very awkward (and lengthy with y1, y2, y3, vv...y8);&lt;BR /&gt;
&lt;BR /&gt;
Can you please help suggest a better yet simple code? Thanks&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;data temp;&lt;BR /&gt;
	input x y;&lt;BR /&gt;
	datalines;&lt;BR /&gt;
	15 151&lt;BR /&gt;
	151 54&lt;BR /&gt;
	890 521&lt;BR /&gt;
	451 90&lt;BR /&gt;
	521 588&lt;BR /&gt;
	515 85&lt;BR /&gt;
	52 80&lt;BR /&gt;
	89 56&lt;BR /&gt;
	24 55&lt;BR /&gt;
	252 51&lt;BR /&gt;
	145 58&lt;BR /&gt;
	;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data m3; /*Rolling 3-month*/&lt;BR /&gt;
	set temp;&lt;BR /&gt;
	n=_n_;&lt;BR /&gt;
	y1=lag(y); y2=lag(y1);&lt;BR /&gt;
	cum_y=y+y1+y2;&lt;BR /&gt;
	if mod(n,3)^=0 then delete;&lt;BR /&gt;
	keep x cum_y;&lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
data m9; /*Rolling 9-month*/&lt;BR /&gt;
	set temp;&lt;BR /&gt;
	n=_n_;&lt;BR /&gt;
	y1=lag(y); y2=lag(y1); y3=lag(y2); y4=lag(y3); y5=lag(y4); y6=lag(y5); y7=lag(y6); y8=lag(y7);&lt;BR /&gt;
	cum_y=y+y1+y2+y3+y4+y5+y6+y7+y8;&lt;BR /&gt;
	if mod(n,9)^=0 then delete;&lt;BR /&gt;
	keep x cum_y;&lt;BR /&gt;
run;&lt;/B&gt;</description>
    <pubDate>Sun, 22 May 2011 10:07:14 GMT</pubDate>
    <dc:creator>smilingmelbourne</dc:creator>
    <dc:date>2011-05-22T10:07:14Z</dc:date>
    <item>
      <title>Rolling cumulative 3-month and 9-month returns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rolling-cumulative-3-month-and-9-month-returns/m-p/68327#M19571</link>
      <description>Hi, I need to compute rolling 3-month and 9-month returns. I wrote this code but it is very awkward (and lengthy with y1, y2, y3, vv...y8);&lt;BR /&gt;
&lt;BR /&gt;
Can you please help suggest a better yet simple code? Thanks&lt;BR /&gt;
&lt;BR /&gt;
&lt;B&gt;data temp;&lt;BR /&gt;
	input x y;&lt;BR /&gt;
	datalines;&lt;BR /&gt;
	15 151&lt;BR /&gt;
	151 54&lt;BR /&gt;
	890 521&lt;BR /&gt;
	451 90&lt;BR /&gt;
	521 588&lt;BR /&gt;
	515 85&lt;BR /&gt;
	52 80&lt;BR /&gt;
	89 56&lt;BR /&gt;
	24 55&lt;BR /&gt;
	252 51&lt;BR /&gt;
	145 58&lt;BR /&gt;
	;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data m3; /*Rolling 3-month*/&lt;BR /&gt;
	set temp;&lt;BR /&gt;
	n=_n_;&lt;BR /&gt;
	y1=lag(y); y2=lag(y1);&lt;BR /&gt;
	cum_y=y+y1+y2;&lt;BR /&gt;
	if mod(n,3)^=0 then delete;&lt;BR /&gt;
	keep x cum_y;&lt;BR /&gt;
run; &lt;BR /&gt;
&lt;BR /&gt;
data m9; /*Rolling 9-month*/&lt;BR /&gt;
	set temp;&lt;BR /&gt;
	n=_n_;&lt;BR /&gt;
	y1=lag(y); y2=lag(y1); y3=lag(y2); y4=lag(y3); y5=lag(y4); y6=lag(y5); y7=lag(y6); y8=lag(y7);&lt;BR /&gt;
	cum_y=y+y1+y2+y3+y4+y5+y6+y7+y8;&lt;BR /&gt;
	if mod(n,9)^=0 then delete;&lt;BR /&gt;
	keep x cum_y;&lt;BR /&gt;
run;&lt;/B&gt;</description>
      <pubDate>Sun, 22 May 2011 10:07:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rolling-cumulative-3-month-and-9-month-returns/m-p/68327#M19571</guid>
      <dc:creator>smilingmelbourne</dc:creator>
      <dc:date>2011-05-22T10:07:14Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling cumulative 3-month and 9-month returns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rolling-cumulative-3-month-and-9-month-returns/m-p/68328#M19572</link>
      <description>data m3;&lt;BR /&gt;
  set temp;&lt;BR /&gt;
  retain cum_y;&lt;BR /&gt;
  cum_y=sum(cum_y,y);&lt;BR /&gt;
  if mod(_n_,3)=0 then&lt;BR /&gt;
  do;&lt;BR /&gt;
    output;&lt;BR /&gt;
    call missing(cum_y);&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data m9;&lt;BR /&gt;
  set temp;&lt;BR /&gt;
  retain cum_y;&lt;BR /&gt;
  cum_y=sum(cum_y,y);&lt;BR /&gt;
  if mod(_n_,9)=0 then&lt;BR /&gt;
  do;&lt;BR /&gt;
    output;&lt;BR /&gt;
    call missing(cum_y);&lt;BR /&gt;
  end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
... and "better" than y3=lag(y2); would have been y3=lag3(y);&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Sun, 22 May 2011 14:00:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rolling-cumulative-3-month-and-9-month-returns/m-p/68328#M19572</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2011-05-22T14:00:58Z</dc:date>
    </item>
    <item>
      <title>Re: Rolling cumulative 3-month and 9-month returns</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Rolling-cumulative-3-month-and-9-month-returns/m-p/68329#M19573</link>
      <description>Thanks, Pattrick, for help. I'd read the book on Output several times, and didn't quite really catch the idea until I've read your code! I just couldn't think of using Output right after the MOD function. Yesterday I found a macro from a university for this rolling, and the macro was far, far complicated than yours. Basically, they used Proc Means for each 3-month and 9-month interval, then output the datasets, then use Proc Append to concatenate all datasets, and for the same thing it has at leas 5 SAS programs.&lt;BR /&gt;
&lt;BR /&gt;
Thanks so much and I've learnt a lot from your codes.

Message was edited by: smilingmelbourne</description>
      <pubDate>Sun, 22 May 2011 23:55:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Rolling-cumulative-3-month-and-9-month-returns/m-p/68329#M19573</guid>
      <dc:creator>smilingmelbourne</dc:creator>
      <dc:date>2011-05-22T23:55:37Z</dc:date>
    </item>
  </channel>
</rss>

