<?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: Average return based on daily returns from past six months in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306541#M65543</link>
    <description>&lt;P&gt;1. Make sure to convert your date to a SAS date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Look into PROC EXPAND, specifically the CONVERT statement. The examples in documentation is helpful.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Or look as possibly using an array method:&amp;nbsp;&lt;A href="https://gist.github.com/statgeek/27e23c015eae7953eff2" target="_blank"&gt;https://gist.github.com/statgeek/27e23c015eae7953eff2&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have daily data but talk about 6 month average. You'll have to be clear in your definition as 'months' is not a standard period.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 22 Oct 2016 09:43:14 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2016-10-22T09:43:14Z</dc:date>
    <item>
      <title>Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306534#M65540</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;At the beginning of each month t, I want to estimate average return based on daily returns from past six (t-6 to t-1) months. There will be almost 20-25 daily observations in a month, but I am keeping very few in sample for teh convenience. The&amp;nbsp;original data is around 20 million observations. Please find the SAS data below. Based on this data, I estimate average return on 20100701 and 20100802 for stock ID 1 and stock ID 2. The result should be 0.006358 for stock 1 on &lt;SPAN&gt;20100701 and 0.00679 on 20100802. The result should be 0.01401 for stock 1 on &lt;SPAN&gt;20100701 and 0.008157 on 20100802.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;Thanks a lot for your help.&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;Best,&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&lt;SPAN&gt;Cheema&lt;/SPAN&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;data have;&lt;BR /&gt;infile cards expandtabs truncover;&lt;BR /&gt;input stock date : yymmdd8. ret ;&lt;BR /&gt;format date yymmdd8.;&lt;BR /&gt;cards;&lt;BR /&gt;1 20100107 0.04135&lt;BR /&gt;1 20100126 -0.02544&lt;BR /&gt;1 20100129 0.03384&lt;BR /&gt;1 20100201 0.01089&lt;BR /&gt;1 20100202 0.02621&lt;BR /&gt;1 20100226 0.01001&lt;BR /&gt;1 20100301 -0.0132&lt;BR /&gt;1 20100330 -0.0181&lt;BR /&gt;1 20100331 -0.00308&lt;BR /&gt;1 20100401 0.05394&lt;BR /&gt;1 20100429 -0.01077&lt;BR /&gt;1 20100430 0.0031&lt;BR /&gt;1 20100503 0.00931&lt;BR /&gt;1 20100521 0&lt;BR /&gt;1 20100531 -0.01427&lt;BR /&gt;1 20100601 -0.02718&lt;BR /&gt;1 20100617 0.05019&lt;BR /&gt;1 20100630 -0.01236&lt;BR /&gt;1 20100701 0&lt;BR /&gt;1 20100728 0.01193&lt;BR /&gt;1 20100730 0.01621&lt;BR /&gt;1 20100802 -0.0174&lt;BR /&gt;2 20100107 0.06069&lt;BR /&gt;2 20100126 -0.06219&lt;BR /&gt;2 20100129 0.01989&lt;BR /&gt;2 20100201 -0.04551&lt;BR /&gt;2 20100202 0.04768&lt;BR /&gt;2 20100226 -0.05462&lt;BR /&gt;2 20100301 0.02613&lt;BR /&gt;2 20100330 -0.04155&lt;BR /&gt;2 20100331 0.04336&lt;BR /&gt;2 20100401 -0.00536&lt;BR /&gt;2 20100429 -0.02561&lt;BR /&gt;2 20100430 -0.0166&lt;BR /&gt;2 20100503 0.00563&lt;BR /&gt;2 20100521 0&lt;BR /&gt;2 20100531 0.01119&lt;BR /&gt;2 20100601 0&lt;BR /&gt;2 20100617 0.01521&lt;BR /&gt;2 20100630 0.27384&lt;BR /&gt;2 20100701 0.09519&lt;BR /&gt;2 20100728 -0.12793&lt;BR /&gt;2 20100730 -0.04815&lt;BR /&gt;2 20100802 -0.02235&lt;BR /&gt;run&lt;BR /&gt;;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 08:10:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306534#M65540</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-10-22T08:10:51Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306538#M65541</link>
      <description>&lt;P&gt;Check next code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc means data=have (where=(date between (todaty() - day(today() -1) to&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; intnx('month',today(),-6) ) NWAY;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; class stock;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var ret;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output out=want &amp;nbsp;mean=;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; format mean 8.7;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 09:29:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306538#M65541</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-10-22T09:29:02Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306540#M65542</link>
      <description>&lt;P&gt;Hi Shmuel,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry, I am looking for average returns over a period of 50 years. Therefore, I need a macro code that can estimate and roll it forward. I use some code to estimate it, but that is not very efficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheema&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 09:38:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306540#M65542</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-10-22T09:38:22Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306541#M65543</link>
      <description>&lt;P&gt;1. Make sure to convert your date to a SAS date.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2. Look into PROC EXPAND, specifically the CONVERT statement. The examples in documentation is helpful.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;3. Or look as possibly using an array method:&amp;nbsp;&lt;A href="https://gist.github.com/statgeek/27e23c015eae7953eff2" target="_blank"&gt;https://gist.github.com/statgeek/27e23c015eae7953eff2&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You have daily data but talk about 6 month average. You'll have to be clear in your definition as 'months' is not a standard period.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 09:43:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306541#M65543</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-22T09:43:14Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306542#M65544</link>
      <description>&lt;P&gt;I'm not expert with statistics. Are you looking for forecast ?&lt;/P&gt;
&lt;P&gt;I leave this question to others.&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 09:43:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306542#M65544</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-10-22T09:43:18Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306543#M65545</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/39704"&gt;@MAC1430&lt;/a&gt; wrote:&lt;BR /&gt;
&lt;P&gt;Hi Shmuel,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Sorry, I am looking for average returns over a period of 50 years. Therefore, I need a macro code that can estimate and roll it forward. I use some code to estimate it, but that is not very efficient.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheema&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You DO NOT need a macro. There's nothing here that requires one and you should be aiming for a single pass of your data.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 09:44:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306543#M65545</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-22T09:44:54Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306545#M65546</link>
      <description>&lt;P&gt;Dear Reeza,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for your reply. I want to estimate average returns based on daily returns in past six months. Hope it is clear now. I will really appreciate your help. I will try to edit the date problem, if there is edit option. Please find the SAS data below in case if the original once can not be edited. I hope this date is fine.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;infile cards expandtabs truncover;&lt;BR /&gt;input stock date : yymmdd8. ret ;&lt;BR /&gt;format date yymmddn8.;&lt;BR /&gt;cards;&lt;BR /&gt;1 20100107 0.04135&lt;BR /&gt;1 20100126 -0.02544&lt;BR /&gt;1 20100129 0.03384&lt;BR /&gt;1 20100201 0.01089&lt;BR /&gt;1 20100202 0.02621&lt;BR /&gt;1 20100226 0.01001&lt;BR /&gt;1 20100301 -0.0132&lt;BR /&gt;1 20100330 -0.0181&lt;BR /&gt;1 20100331 -0.00308&lt;BR /&gt;1 20100401 0.05394&lt;BR /&gt;1 20100429 -0.01077&lt;BR /&gt;1 20100430 0.0031&lt;BR /&gt;1 20100503 0.00931&lt;BR /&gt;1 20100521 0&lt;BR /&gt;1 20100531 -0.01427&lt;BR /&gt;1 20100601 -0.02718&lt;BR /&gt;1 20100617 0.05019&lt;BR /&gt;1 20100630 -0.01236&lt;BR /&gt;1 20100701 0&lt;BR /&gt;1 20100728 0.01193&lt;BR /&gt;1 20100730 0.01621&lt;BR /&gt;1 20100802 -0.0174&lt;BR /&gt;2 20100107 0.06069&lt;BR /&gt;2 20100126 -0.06219&lt;BR /&gt;2 20100129 0.01989&lt;BR /&gt;2 20100201 -0.04551&lt;BR /&gt;2 20100202 0.04768&lt;BR /&gt;2 20100226 -0.05462&lt;BR /&gt;2 20100301 0.02613&lt;BR /&gt;2 20100330 -0.04155&lt;BR /&gt;2 20100331 0.04336&lt;BR /&gt;2 20100401 -0.00536&lt;BR /&gt;2 20100429 -0.02561&lt;BR /&gt;2 20100430 -0.0166&lt;BR /&gt;2 20100503 0.00563&lt;BR /&gt;2 20100521 0&lt;BR /&gt;2 20100531 0.01119&lt;BR /&gt;2 20100601 0&lt;BR /&gt;2 20100617 0.01521&lt;BR /&gt;2 20100630 0.27384&lt;BR /&gt;2 20100701 0.09519&lt;BR /&gt;2 20100728 -0.12793&lt;BR /&gt;2 20100730 -0.04815&lt;BR /&gt;2 20100802 -0.02235&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 10:13:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306545#M65546</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-10-22T10:13:53Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306546#M65547</link>
      <description>&lt;P&gt;Dear Shmuel,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks, It is a rolling average using daily returns from past six months. I have 5000 stocks and 50 years data&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheema&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 10:16:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306546#M65547</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-10-22T10:16:16Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306548#M65548</link>
      <description>&lt;P&gt;You have a big table ,therefore it is not good for SQL.&lt;/P&gt;
&lt;P&gt;Also assuming data has been sorted by stock .&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards  truncover;
input stock date : yymmdd10. ret ;
format date yymmdd10.;
cards;
1 20100107 0.04135
1 20100126 -0.02544
1 20100129 0.03384
1 20100201 0.01089
1 20100202 0.02621
1 20100226 0.01001
1 20100301 -0.0132
1 20100330 -0.0181
1 20100331 -0.00308
1 20100401 0.05394
1 20100429 -0.01077
1 20100430 0.0031
1 20100503 0.00931
1 20100521 0
1 20100531 -0.01427
1 20100601 -0.02718
1 20100617 0.05019
1 20100630 -0.01236
1 20100701 0
1 20100728 0.01193
1 20100730 0.01621
1 20100802 -0.0174
2 20100107 0.06069
2 20100126 -0.06219
2 20100129 0.01989
2 20100201 -0.04551
2 20100202 0.04768
2 20100226 -0.05462
2 20100301 0.02613
2 20100330 -0.04155
2 20100331 0.04336
2 20100401 -0.00536
2 20100429 -0.02561
2 20100430 -0.0166
2 20100503 0.00563
2 20100521 0
2 20100531 0.01119
2 20100601 0
2 20100617 0.01521
2 20100630 0.27384
2 20100701 0.09519
2 20100728 -0.12793
2 20100730 -0.04815
2 20100802 -0.02235
;
run;
data want;
 if _n_=1 then do;
  if 0 then set have(rename=(ret=_ret));
  declare hash h();
  h.definekey('date');
  h.definedata('_ret');
  h.definedone();
 end;
 
 do until(last.stock);
  set have;
  by stock;
  _ret=ret;h.add();
 end;
 do until(last.stock);
  set have;
  by stock;
  sum=0;n=0;
  do k=intnx('month',date,-6,'b') to intnx('month',date,-1,'e');
   if h.find(key:k)=0 then do;sum+_ret;n+1;end;
  end;
  avg_ret=divide(sum,n);
  output;
 end;
h.clear();
drop sum n k _ret;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 22 Oct 2016 11:11:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306548#M65548</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-22T11:11:28Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306549#M65549</link>
      <description>&lt;P&gt;Dear Ksharp,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot for it. Is it possible that it will start estimating average return from 7th month? For example, in our case it will estimate first return in July based on the daily returns from January to June. Second, is it possible that it estimate the average return on the first available day of the month only, other days can be shown as just missing.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks again,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheema&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 11:27:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306549#M65549</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-10-22T11:27:06Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306555#M65552</link>
      <description>&lt;P&gt;You can use same code that I posted with slight change &lt;STRONG&gt;for any period&lt;/STRONG&gt; you want:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%let &lt;STRONG&gt;start&lt;/STRONG&gt; = 2016JAN01; /* or any other date to start */&lt;/P&gt;
&lt;P&gt;%let &lt;STRONG&gt;upto&lt;/STRONG&gt; = 2016JUN30; /*&amp;nbsp;&lt;SPAN&gt;or any other date to end */&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;proc means data=have (where=(date between&amp;nbsp;&lt;STRONG&gt;"&amp;amp;start"d&lt;/STRONG&gt; &amp;nbsp;to &lt;STRONG&gt;"&amp;amp;upto"d&lt;/STRONG&gt; )&amp;nbsp;) NWAY;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; class stock;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; var ret;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; output out=want &amp;nbsp;mean=;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; format mean 8.7;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 13:20:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306555#M65552</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-10-22T13:20:12Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306584#M65570</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/88384"&gt;@Shmuel﻿&lt;/a&gt;&amp;nbsp;The OP wants a moving average which can't be accomplished in PROC means but can be in Proc EXPAND.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 22:47:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306584#M65570</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-22T22:47:26Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306585#M65571</link>
      <description>&lt;P&gt;Dear Shmuel,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks for it. Another member already provided the solution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Cheema&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 22:47:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306585#M65571</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-10-22T22:47:57Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306588#M65574</link>
      <description>&lt;P&gt;You do realize that since your 6 month terms aren't consistent the validity of your results are questionable?&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Oct 2016 23:00:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306588#M65574</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-22T23:00:32Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306591#M65576</link>
      <description>Hi Reeza,&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Thanks for your concern. Basically, I estimate average return each month for each stock based on its daily returns in past six months.&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Best,&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Cheema&lt;BR /&gt;</description>
      <pubDate>Sat, 22 Oct 2016 23:09:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306591#M65576</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-10-22T23:09:57Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306592#M65577</link>
      <description>&lt;P&gt;And it doesn't matter if that's 120, 125, or 130 days?&amp;nbsp;&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>Sat, 22 Oct 2016 23:21:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306592#M65577</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-10-22T23:21:35Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306593#M65578</link>
      <description>Yes, as long as the returns are from past six months. The frequency is not important.</description>
      <pubDate>Sat, 22 Oct 2016 23:25:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306593#M65578</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-10-22T23:25:15Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306609#M65589</link>
      <description>&lt;P&gt;Sure. Also assuming table has been sorted by Stock,Date.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have;
infile cards  truncover;
input stock date : yymmdd10. ret ;
format date yymmdd10.;
cards;
1 20100107 0.04135
1 20100126 -0.02544
1 20100129 0.03384
1 20100201 0.01089
1 20100202 0.02621
1 20100226 0.01001
1 20100301 -0.0132
1 20100330 -0.0181
1 20100331 -0.00308
1 20100401 0.05394
1 20100429 -0.01077
1 20100430 0.0031
1 20100503 0.00931
1 20100521 0
1 20100531 -0.01427
1 20100601 -0.02718
1 20100617 0.05019
1 20100630 -0.01236
1 20100701 0
1 20100728 0.01193
1 20100730 0.01621
1 20100802 -0.0174
2 20100107 0.06069
2 20100126 -0.06219
2 20100129 0.01989
2 20100201 -0.04551
2 20100202 0.04768
2 20100226 -0.05462
2 20100301 0.02613
2 20100330 -0.04155
2 20100331 0.04336
2 20100401 -0.00536
2 20100429 -0.02561
2 20100430 -0.0166
2 20100503 0.00563
2 20100521 0
2 20100531 0.01119
2 20100601 0
2 20100617 0.01521
2 20100630 0.27384
2 20100701 0.09519
2 20100728 -0.12793
2 20100730 -0.04815
2 20100802 -0.02235
;
run;
data want;
 if _n_=1 then do;
  if 0 then set have(rename=(ret=_ret));
  declare hash h();
  h.definekey('date');
  h.definedata('_ret');
  h.definedone();
 end;
 
 do until(last.stock);
  set have;
  by stock;
  _ret=ret;h.add();
 end;
 do until(last.stock);
  set have;
  by stock;
  avg_ret=.;
  month=month(date);
  if lag(month) ne month then do;
    sum=0;n=0;
    do k=intnx('month',date,-6,'b') to intnx('month',date,-1,'e');
     if h.find(key:k)=0 then do;sum+_ret;n+1;end;
    end;
    avg_ret=divide(sum,n);
  end;
  output;
 end;
h.clear();
drop sum n k _ret month;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Oct 2016 03:23:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306609#M65589</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-23T03:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306616#M65593</link>
      <description>Thanks a lot Ksharp, really appreciated &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Sun, 23 Oct 2016 04:09:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/306616#M65593</guid>
      <dc:creator>MAC1430</dc:creator>
      <dc:date>2016-10-23T04:09:20Z</dc:date>
    </item>
    <item>
      <title>Re: Average return based on daily returns from past six months</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/367518#M87506</link>
      <description>Hello Dear. I am trying to use these codes and they work perfect for the purpose they are here but i have some issues.&lt;BR /&gt;1) it is calculating even if last six months are not available. is it possible to calculate only for cases in which there are previous six months available?&lt;BR /&gt;2) I want to keep this get only first value of month for each month in each month.&lt;BR /&gt;thanks for posting these.</description>
      <pubDate>Thu, 15 Jun 2017 21:50:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Average-return-based-on-daily-returns-from-past-six-months/m-p/367518#M87506</guid>
      <dc:creator>Kyojik</dc:creator>
      <dc:date>2017-06-15T21:50:28Z</dc:date>
    </item>
  </channel>
</rss>

