<?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: looking for efficient codes for calculating moving average. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699751#M214059</link>
    <description>&lt;P&gt;Sub-selects in SAS SQL are notorious for bad performance.&lt;/P&gt;
&lt;P&gt;If you supply example data in usable form (data steps with datalines!) for your two datasets, so we can get a picture and have something to test against, we will be able to suggest alternatives.&lt;/P&gt;</description>
    <pubDate>Wed, 18 Nov 2020 12:16:51 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-11-18T12:16:51Z</dc:date>
    <item>
      <title>looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699747#M214058</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
   create table capm3 as
   select *, 
          (select mean(rmrf) from capm2 
             where cusip=a.cusip
               and (intnx('year', a.Date, -30, 'b') le Date le a.Date)) 
                 as riskpremium format=dollar8.2
   from capm2 as a;
quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Hello all,&lt;/P&gt;&lt;P&gt;I am working on dealing with a big number of observations more than 40mililions.&amp;nbsp;&lt;/P&gt;&lt;P&gt;What I now want to do is to calculate the past 30-year annual average of stock returns.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Conceptually, I have to calculate moving averages of the last 30-year period of each observation and then have to annualize each of them. Before doing the annualization part, I am facing the big challenge of calculating moving average part.&lt;/P&gt;&lt;P&gt;What I have now are monthly stock returns. To be as doing correctly as possible, I should use&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;CODE class=" language-sas"&gt;(intnx('month', a.Date, -359, 'b') le Date le a.Date)) &lt;/CODE&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;instead of using 'year' and -30.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, what I rather would like to ask for your help is not this part. Since I am working on more than 40mils observations, the code I wrote above takes long hours to process, so far more than 5 hours but still processing....&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think this is not the right way to handle it and have been thinking of something else.&lt;/P&gt;&lt;P&gt;A very similar way and conceptually the same way I first did is like using PROC SQL and identifying the interval like&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;proc sql;&lt;BR /&gt;create table WANT as&lt;BR /&gt;select *,(select mean(rmfr) from HAVE where YYMM between a.YYMM-360 and a.fyear-1 and gvkey=a.gvkey) as WHATIWANT&lt;BR /&gt;from HAVE as a;&lt;BR /&gt;quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Yeah.. you know this took a long long long time as well.&lt;/P&gt;&lt;P&gt;It seems that it is common in finance and accounting studies to calculate the so-called "market risk premium" using the difference between the risk-free rate and whatever marker rate over the last 20 or 30 years before the time of observation, so I guess there must be a proper way to do so other than what I have tried so far.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hope kind ones of you share your wisdom with a rookie student.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 12:05:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699747#M214058</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-18T12:05:23Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699751#M214059</link>
      <description>&lt;P&gt;Sub-selects in SAS SQL are notorious for bad performance.&lt;/P&gt;
&lt;P&gt;If you supply example data in usable form (data steps with datalines!) for your two datasets, so we can get a picture and have something to test against, we will be able to suggest alternatives.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 12:16:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699751#M214059</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-18T12:16:51Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699760#M214063</link>
      <description>&lt;P&gt;PROC EXPAND produces &lt;A href="https://documentation.sas.com/?cdcId=pgmsascdc&amp;amp;cdcVersion=9.4_3.4&amp;amp;docsetId=etsug&amp;amp;docsetTarget=etsug_expand_examples04.htm&amp;amp;locale=en" target="_self"&gt;moving averages&lt;/A&gt;. If your SAS license does not include PROC EXPAND which is in SAS/ETS, there are &lt;A href="https://support.sas.com/kb/25/027.html" target="_self"&gt;DATA step methods&lt;/A&gt; of obtaining the same result.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 12:23:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699760#M214063</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-18T12:23:28Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699762#M214065</link>
      <description>Thank you for your reply, Paige,&lt;BR /&gt;&lt;BR /&gt;PROC EXPAND is a good alternative in most cases. The reason why I am not using it for this case is that PROC EXPAND cannot rule out current observations, t. I need a period from t-1 to t-n, which PROX EXPAND does not have while it has cmove, trimleft, or other good techniques...&lt;BR /&gt;</description>
      <pubDate>Wed, 18 Nov 2020 12:26:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699762#M214065</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-18T12:26:56Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699764#M214067</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270067"&gt;@JKCho&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Thank you for your reply, Paige,&lt;BR /&gt;&lt;BR /&gt;PROC EXPAND is a good alternative in most cases. The reason why I am not using it for this case is that PROC EXPAND cannot rule out current observations, t. I need a period from t-1 to t-n, which PROX EXPAND does not have while it has cmove, trimleft, or other good techniques...&lt;BR /&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If you want the moving average of time t-1 to t-n, then you use PROC EXPAND and then re-arrange the data set so that the moving averages are moved one row downwards. Now, time t has the average of times t-1 to t-n.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 12:38:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699764#M214067</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-18T12:38:04Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699767#M214069</link>
      <description>&lt;P&gt;Thank you for your kind of helping me.&lt;/P&gt;&lt;P&gt;I just made up an example of data just to help you understand what the dataset I am working on. What I want to do with this is to make average MONTHLY returns of each ID over the last 30 years.&lt;/P&gt;&lt;P&gt;For example, if the point of observation is 1988/03 and ID is 1, then I want to calculate avg. MONTHLY return of ID 1 over the period from 1958/03 to 1988/02.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your kindness again &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&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;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data person;
   input ID $ return $ YYMM;
   datalines;                      
1  0.0151  198801
2  0.0946  198801
3  0.0765  198801
1  0.0011  198802 
2  0.0021  198802  
3  0.0031  198802
1  0.0153  198803
2  0.0054  198803
3  0.0855  198803&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 12:44:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699767#M214069</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-18T12:44:49Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699770#M214071</link>
      <description>&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;For example, if the point of observation is 1988/03 and ID is 1, then I want to calculate avg. MONTHLY return of ID 1 over the period from 1958/03 to 1988/02.&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;I'm not sure what this has to do with calculating moving average. Or is it a separate problem?&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 12:48:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699770#M214071</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-18T12:48:56Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699774#M214073</link>
      <description>That is just an example of a moving average that I want to calculate.&lt;BR /&gt;Like an average at the point of that time, I need to calculate other averages of different times for different IDs, so moving averages.&lt;BR /&gt;Wish this can clarify better.</description>
      <pubDate>Wed, 18 Nov 2020 12:54:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699774#M214073</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-18T12:54:18Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699777#M214074</link>
      <description>Sorry, I do not think I understand what you want to deliver. Can you explain more on how lagged MAs can do with t-1 to t-n? To me, you look like saying something like a mean from t-1 to t-12 and another mean from t to t-11, but could not understand your message.</description>
      <pubDate>Wed, 18 Nov 2020 13:00:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699777#M214074</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-18T13:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699779#M214076</link>
      <description>Seems you meant, by its face value, to put a MA calculated from using data from t-1 to t-n at a row of time T. Oh... I did get it now. Thx. That is an easy trick. Thx!!</description>
      <pubDate>Wed, 18 Nov 2020 13:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699779#M214076</guid>
      <dc:creator>JKCho</dc:creator>
      <dc:date>2020-11-18T13:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699780#M214077</link>
      <description>&lt;P&gt;In your question, you used another&amp;nbsp;dataset (capm2) with completely different variables (rmrf, cusip). Please supply a sufficient example for this dataset, and the result you expect out of this data.&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 15:15:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699780#M214077</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-18T15:15:33Z</dc:date>
    </item>
    <item>
      <title>Re: looking for efficient codes for calculating moving average.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699781#M214078</link>
      <description>&lt;P&gt;For this example data, you could provide, at most, a moving average of 2, so the example is a poor fit to the problem. Nevertheless, here is some random data showing how to get the moving average you want, using the example in the PROC EXPAND documentation&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
   input year qtr x;
   date = yyq( year, qtr );
   format date yyqc.;
datalines;
1989 3 5238
1989 4 5289
1990 1 5375
1990 2 5443
1990 3 5514
1990 4 5527
1991 1 5557
1991 2 5615
;
proc expand data=test out=out method=none;
   id date;
   convert x = x_movave / transformout=(movave 5);
run;
proc sql;
	create table want as select a.*,b.x_movave
		from out(drop=x_movave) as a left join out(keep=date x_movave) as b
		on a.date=intnx('quarter',b.date,1,'b')
	order by a.date;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Nov 2020 13:07:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/looking-for-efficient-codes-for-calculating-moving-average/m-p/699781#M214078</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-18T13:07:33Z</dc:date>
    </item>
  </channel>
</rss>

