<?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: the rolling prior five-year average computation in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238203#M43734</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array p{0:4} _temporary_;
set have; 
by object;
if first.object then call missing(of p{*});

if n(of p{*})&amp;gt;=3 then average = mean(of p{*});

p{mod(_n_,5)} = price;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: Modified to account for 3+ years of data present.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 08 Dec 2015 09:32:24 GMT</pubDate>
    <dc:creator>Reeza</dc:creator>
    <dc:date>2015-12-08T09:32:24Z</dc:date>
    <item>
      <title>the rolling prior five-year average computation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238194#M43731</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following is the datset I have.&lt;/P&gt;&lt;P&gt;gvkey&amp;nbsp;&amp;nbsp; fyear&amp;nbsp; rds&amp;nbsp;&lt;/P&gt;&lt;P&gt;0001&amp;nbsp;&amp;nbsp;&amp;nbsp; 1980&amp;nbsp;&amp;nbsp; 0.05&lt;/P&gt;&lt;P&gt;0001&amp;nbsp;&amp;nbsp;&amp;nbsp; 1981&amp;nbsp;&amp;nbsp; 0.02&lt;/P&gt;&lt;P&gt;0001&amp;nbsp;&amp;nbsp;&amp;nbsp; 1982&amp;nbsp;&amp;nbsp; 0.03&lt;/P&gt;&lt;P&gt;0001&amp;nbsp;&amp;nbsp;&amp;nbsp; 1983&amp;nbsp;&amp;nbsp;&amp;nbsp;0.04&lt;/P&gt;&lt;P&gt;0001&amp;nbsp;&amp;nbsp;&amp;nbsp; 1984&amp;nbsp;&amp;nbsp; 0.04&lt;/P&gt;&lt;P&gt;0001&amp;nbsp;&amp;nbsp;&amp;nbsp; 1985&amp;nbsp;&amp;nbsp; 0.05&lt;/P&gt;&lt;P&gt;0001&amp;nbsp;&amp;nbsp;&amp;nbsp; 1986&amp;nbsp;&amp;nbsp;&amp;nbsp;0.06&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I would like to compute the rolling prior five-year average for variable 'rds'. In other words, in 1986, average rds of prior five year is (rds in1985 + rds in 1984+rds in 1983+rds in 1982+rds in 1981) should be computed, and in 1985, average rds of prior five year is (rds in 1984 + rds in 1983 + rds in 1982 + rds in 1981 + rds in 1980) should be computed, and so on. And I also want to require at least three non-missing observations per gvkey. In this example, in 1983, average of rds of prior three year is (rds in 1982 + rds in 1981 + rds in 1980) can be computed. (not 1982, 1981). Any advice will be highly appreciated.&lt;/P&gt;&lt;P&gt;proc sql: create table want as&lt;/P&gt;&lt;P&gt;select *, select ave(rds) from have where fyear between intnx ('fyear',-5) and ('fyear',-1) from have as&amp;nbsp; rds5&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Dec 2015 01:48:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238194#M43731</guid>
      <dc:creator>kimx0961</dc:creator>
      <dc:date>2015-12-08T01:48:31Z</dc:date>
    </item>
    <item>
      <title>Re: the rolling prior five-year average computation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238195#M43732</link>
      <description>Do you have SAS/ETS? If so, Proc Expand is a good solution. If not, you can use a temporary array solution:&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/message/244232" target="_blank"&gt;https://communities.sas.com/message/244232&lt;/A&gt;</description>
      <pubDate>Tue, 08 Dec 2015 02:14:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238195#M43732</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-08T02:14:42Z</dc:date>
    </item>
    <item>
      <title>Re: the rolling prior five-year average computation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238201#M43733</link>
      <description>&lt;P&gt;Thank you for your prompt reply&lt;/P&gt;&lt;P&gt;According to my search, PROC EXPAND 'movave' option computes the rolling five-year mean&amp;nbsp;using current year value and values of prior years (lag4, lag3,lag2,lag1 values). My example needs to compute the folling prior five-year mean using values of lag5, lag4, lag3, lag2, and lag1. It wll be appreicated if you advise me more in detail about array statement.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Dec 2015 03:33:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238201#M43733</guid>
      <dc:creator>kimx0961</dc:creator>
      <dc:date>2015-12-08T03:33:10Z</dc:date>
    </item>
    <item>
      <title>Re: the rolling prior five-year average computation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238203#M43734</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
array p{0:4} _temporary_;
set have; 
by object;
if first.object then call missing(of p{*});

if n(of p{*})&amp;gt;=3 then average = mean(of p{*});

p{mod(_n_,5)} = price;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EDIT: Modified to account for 3+ years of data present.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Dec 2015 09:32:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238203#M43734</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-08T09:32:24Z</dc:date>
    </item>
    <item>
      <title>Re: the rolling prior five-year average computation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238206#M43736</link>
      <description>&lt;P&gt;Can be done efficiently with arrays but you have to worry about missing years. Not a problem with&amp;nbsp;SQL:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input gvkey fyear rds;
datalines;
0001    1980   0.05
0001    1981   0.02
0001    1982   0.03
0001    1983   0.04
0001    1984   0.04
0001    1985   0.05
0001    1986   0.06
;

proc sql;
create table want as
select 
    a.gvkey, 
    a.fyear, 
    a.rds, 
    case 
        when count(b.rds) &amp;gt;= 3 then mean(b.rds) 
        else . 
        end as rds5y
from 
    have as a left join
    have as b 
        on  a.gvkey=b.gvkey and 
            b.fyear between a.fyear-5 and a.fyear-1
group by a.gvkey, a.fyear, a.rds;
select * from want;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Dec 2015 04:02:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238206#M43736</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-12-08T04:02:39Z</dc:date>
    </item>
    <item>
      <title>Re: the rolling prior five-year average computation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238604#M43838</link>
      <description>&lt;P&gt;Thank you so much for your help and time!&lt;/P&gt;&lt;P&gt;Your program worked successfully. Can I ask one more question?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have&amp;nbsp;six dataset with over 200,000 observations like the following and want to merge these six datasets efficiently.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;dataset 1:gvkey fyear rds&lt;/P&gt;&lt;P&gt;dataset 2:gvkey fyear emp&lt;/P&gt;&lt;P&gt;dataset 3:gvkey fyear emps&lt;/P&gt;&lt;P&gt;dataset 4:gvkey fyear ddd&lt;/P&gt;&lt;P&gt;dataset 5:gvkey fyear eee&lt;/P&gt;&lt;P&gt;dataset 6:gvkey fyear aaa&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;wanted integrated dataset: gvkey fyear rds emp emps ddd eee aaa&lt;/P&gt;&lt;P&gt;Using 'set' and 'merge' statements don't work well.&lt;/P&gt;&lt;P&gt;Thank you again for your help&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Dec 2015 22:50:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238604#M43838</guid>
      <dc:creator>kimx0961</dc:creator>
      <dc:date>2015-12-09T22:50:31Z</dc:date>
    </item>
    <item>
      <title>Re: the rolling prior five-year average computation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238608#M43839</link>
      <description>&lt;P&gt;MERGE should work well for joining your datasets once they are sorted. What do you mean by "don't work well". What code did you try? What problem did you encounter?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data all;&lt;/P&gt;
&lt;P&gt;merge dataset1&amp;nbsp;&lt;SPAN&gt;dataset2&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;dataset3&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;dataset4&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;dataset5&amp;nbsp;&lt;/SPAN&gt;&lt;SPAN&gt;dataset6;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;by gvkey fyear;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;run;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 09 Dec 2015 23:27:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238608#M43839</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2015-12-09T23:27:22Z</dc:date>
    </item>
    <item>
      <title>Re: the rolling prior five-year average computation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238623#M43842</link>
      <description>You should mark this question answered and post a new question, which people would be happy to help with &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Thu, 10 Dec 2015 02:34:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238623#M43842</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-10T02:34:23Z</dc:date>
    </item>
    <item>
      <title>Re: the rolling prior five-year average computation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238625#M43843</link>
      <description>&lt;P&gt;Thank you very much&lt;/P&gt;&lt;P&gt;It works well.&lt;/P&gt;</description>
      <pubDate>Thu, 10 Dec 2015 02:58:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238625#M43843</guid>
      <dc:creator>kimx0961</dc:creator>
      <dc:date>2015-12-10T02:58:31Z</dc:date>
    </item>
    <item>
      <title>Re: the rolling prior five-year average computation</title>
      <link>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238626#M43844</link>
      <description>Generally you mark the correct answer as correct, not your response....</description>
      <pubDate>Thu, 10 Dec 2015 03:23:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/the-rolling-prior-five-year-average-computation/m-p/238626#M43844</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2015-12-10T03:23:45Z</dc:date>
    </item>
  </channel>
</rss>

