<?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 How to calculate moving averages if given at least 4 obs. over 10 years, regardless of gaps. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790449#M253078</link>
    <description>Hi, thank you in advance.&lt;BR /&gt;I want to calculate moving averages over the preceding 10 years.&lt;BR /&gt;If 10 years are not fully supplied, I can calculate based on at least 4 obs. over the preceding 10 years (Gaps in years do not matter).&lt;BR /&gt;&lt;BR /&gt;So, if the below data are given:&lt;BR /&gt;ID. YEAR. VALS&lt;BR /&gt;1. 1990. 0.24&lt;BR /&gt;2. 2003. 0.13&lt;BR /&gt;2. 2004. 0.22&lt;BR /&gt;2. 2005. 0.26&lt;BR /&gt;2. 2006. 0.1&lt;BR /&gt;2. 2007 0.95&lt;BR /&gt;5. 1998. 0.2&lt;BR /&gt;5. 1999. 0.33&lt;BR /&gt;5. 2000. 0.42&lt;BR /&gt;5 2001. 0.01&lt;BR /&gt;5. 2002. 0.42&lt;BR /&gt;5. 2004. 0.54&lt;BR /&gt;5. 2005. 0.83&lt;BR /&gt;5. 2006. 0.13&lt;BR /&gt;5. 2007. 0.25&lt;BR /&gt;5. 2011. 0.98&lt;BR /&gt;5. 2012. 0.4&lt;BR /&gt;5. 2016. 0.32&lt;BR /&gt;5. 2018. 0.15&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;My desired output is as follows:&lt;BR /&gt;&lt;BR /&gt;1. 1990. 0.24. .&lt;BR /&gt;2. 2003. 0.13. .&lt;BR /&gt;2. 2004. 0.22. .&lt;BR /&gt;2. 2005. 0.26. .&lt;BR /&gt;2. 2006. 0.1. 0.18&lt;BR /&gt;2. 2007. 0.95. 0.33&lt;BR /&gt;5. 1998. 0.2. .&lt;BR /&gt;5. 1999. 0.33. .&lt;BR /&gt;5. 2000. 0.42. .&lt;BR /&gt;5. 2001 0.01. 0.24&lt;BR /&gt;5. 2002. 0.42. 0.28&lt;BR /&gt;5. 2004. 0.54. 0.32&lt;BR /&gt;5. 2005 0.83. 0.39&lt;BR /&gt;5. 2006 0.13. 0.36&lt;BR /&gt;5. 2007 0.25. 0.35&lt;BR /&gt;5. 2011 0.98. 0.45&lt;BR /&gt;5. 2012. 0.4. 0.50&lt;BR /&gt;5. 2016 0.32. 0.42&lt;BR /&gt;5. 2018 0.15. 0.46&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;For ID 2 Year 2006, the moving average is over 2003~2006.&lt;BR /&gt;For ID 2 Year 2007, the moving average is over 2003~2007.&lt;BR /&gt;In the case of ID 5, the values for 2001~2007 are the averages starting from 1998, ending in the year of interest.&lt;BR /&gt;The value for 2011 is the average over 2001~2011.&lt;BR /&gt;The value for 2016, for example, is the average over 2006~2016.&lt;BR /&gt;&lt;BR /&gt;I was helped by SAS communities for many times in the past. I appreciate your help in advance!</description>
    <pubDate>Mon, 17 Jan 2022 02:18:48 GMT</pubDate>
    <dc:creator>KS99</dc:creator>
    <dc:date>2022-01-17T02:18:48Z</dc:date>
    <item>
      <title>How to calculate moving averages if given at least 4 obs. over 10 years, regardless of gaps.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790449#M253078</link>
      <description>Hi, thank you in advance.&lt;BR /&gt;I want to calculate moving averages over the preceding 10 years.&lt;BR /&gt;If 10 years are not fully supplied, I can calculate based on at least 4 obs. over the preceding 10 years (Gaps in years do not matter).&lt;BR /&gt;&lt;BR /&gt;So, if the below data are given:&lt;BR /&gt;ID. YEAR. VALS&lt;BR /&gt;1. 1990. 0.24&lt;BR /&gt;2. 2003. 0.13&lt;BR /&gt;2. 2004. 0.22&lt;BR /&gt;2. 2005. 0.26&lt;BR /&gt;2. 2006. 0.1&lt;BR /&gt;2. 2007 0.95&lt;BR /&gt;5. 1998. 0.2&lt;BR /&gt;5. 1999. 0.33&lt;BR /&gt;5. 2000. 0.42&lt;BR /&gt;5 2001. 0.01&lt;BR /&gt;5. 2002. 0.42&lt;BR /&gt;5. 2004. 0.54&lt;BR /&gt;5. 2005. 0.83&lt;BR /&gt;5. 2006. 0.13&lt;BR /&gt;5. 2007. 0.25&lt;BR /&gt;5. 2011. 0.98&lt;BR /&gt;5. 2012. 0.4&lt;BR /&gt;5. 2016. 0.32&lt;BR /&gt;5. 2018. 0.15&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;My desired output is as follows:&lt;BR /&gt;&lt;BR /&gt;1. 1990. 0.24. .&lt;BR /&gt;2. 2003. 0.13. .&lt;BR /&gt;2. 2004. 0.22. .&lt;BR /&gt;2. 2005. 0.26. .&lt;BR /&gt;2. 2006. 0.1. 0.18&lt;BR /&gt;2. 2007. 0.95. 0.33&lt;BR /&gt;5. 1998. 0.2. .&lt;BR /&gt;5. 1999. 0.33. .&lt;BR /&gt;5. 2000. 0.42. .&lt;BR /&gt;5. 2001 0.01. 0.24&lt;BR /&gt;5. 2002. 0.42. 0.28&lt;BR /&gt;5. 2004. 0.54. 0.32&lt;BR /&gt;5. 2005 0.83. 0.39&lt;BR /&gt;5. 2006 0.13. 0.36&lt;BR /&gt;5. 2007 0.25. 0.35&lt;BR /&gt;5. 2011 0.98. 0.45&lt;BR /&gt;5. 2012. 0.4. 0.50&lt;BR /&gt;5. 2016 0.32. 0.42&lt;BR /&gt;5. 2018 0.15. 0.46&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;For ID 2 Year 2006, the moving average is over 2003~2006.&lt;BR /&gt;For ID 2 Year 2007, the moving average is over 2003~2007.&lt;BR /&gt;In the case of ID 5, the values for 2001~2007 are the averages starting from 1998, ending in the year of interest.&lt;BR /&gt;The value for 2011 is the average over 2001~2011.&lt;BR /&gt;The value for 2016, for example, is the average over 2006~2016.&lt;BR /&gt;&lt;BR /&gt;I was helped by SAS communities for many times in the past. I appreciate your help in advance!</description>
      <pubDate>Mon, 17 Jan 2022 02:18:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790449#M253078</guid>
      <dc:creator>KS99</dc:creator>
      <dc:date>2022-01-17T02:18:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate moving averages if given at least 4 obs. over 10 years, regardless of gaps.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790451#M253079</link>
      <description>&lt;P&gt;Like this?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; data HAVE;
  input ID YEAR VAL;
  cards;
1. 1990. 0.24
2. 2003. 0.13
2. 2004. 0.22
2. 2005. 0.26
2. 2006. 0.1
2. 2007 0.95
5. 1998. 0.2
5. 1999. 0.33
5. 2000. 0.42
5 2001. 0.01
5. 2002. 0.42
5. 2004. 0.54
5. 2005. 0.83
5. 2006. 0.13
5. 2007. 0.25
5. 2011. 0.98
5. 2012. 0.4
5. 2016. 0.32
5. 2018. 0.15
run;

proc sql;
  select unique a.*
       , ifn(count(b.YEAR) &amp;gt; 3, mean(b.VAL), .) as AVG
  from HAVE a
         left join 
       HAVE b 
         on  a.ID   = b.ID
         and b.YEAR between a.YEAR-10 and a.YEAR
  group by 1, 2
  order by 1, 2;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;&lt;BR /&gt;&lt;BR /&gt;
&lt;DIV&gt;
&lt;DIV align="center"&gt;
&lt;TABLE class="table" summary="Procedure SQL: Query Results" width="207px" frame="box" rules="all" cellspacing="0" cellpadding="5"&gt;&lt;COLGROUP&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt; &lt;COL /&gt;&lt;/COLGROUP&gt;
&lt;THEAD&gt;
&lt;TR&gt;
&lt;TH class="r b header" scope="col" width="40px"&gt;ID&lt;/TH&gt;
&lt;TH class="r b header" scope="col" width="46.9375px"&gt;YEAR&lt;/TH&gt;
&lt;TH class="r b header" scope="col" width="42.4792px"&gt;VAL&lt;/TH&gt;
&lt;TH class="r b header" scope="col" width="78.0833px"&gt;AVG&lt;/TH&gt;
&lt;/TR&gt;
&lt;/THEAD&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;1&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;1990&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.24&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;2&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2003&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.13&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;2&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2004&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.22&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;2&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2005&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.26&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;2&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2006&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.1&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.1775&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;2&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2007&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.95&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.332&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;1998&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.2&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;1999&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.33&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2000&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.42&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;.&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2001&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.01&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.24&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2002&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.42&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.276&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2004&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.54&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.32&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2005&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.83&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.392857&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2006&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.13&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.36&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2007&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.25&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.347778&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2011&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.98&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.451429&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2012&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.4&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.507143&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2016&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.32&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.416&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="40px" class="r data"&gt;5&lt;/TD&gt;
&lt;TD width="46.9375px" class="r data"&gt;2018&lt;/TD&gt;
&lt;TD width="42.4792px" class="r data"&gt;0.15&lt;/TD&gt;
&lt;TD width="78.0833px" class="r data"&gt;0.4625&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;/DIV&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jan 2022 03:25:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790451#M253079</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2022-01-17T03:25:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate moving averages if given at least 4 obs. over 10 years, regardless of gaps.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790488#M253098</link>
      <description>Hi,&lt;BR /&gt;Thank you for your quick reply. I tried your codes, and they worked. But after a large amount of time for SAS finishing the task (5-7 minutes), when I tried to open the dataset, SAS runs simply forever.&lt;BR /&gt;My dataset contains 874,394 obs.&lt;BR /&gt;And the slightly modified codes that I ran are as follows:&lt;BR /&gt;&lt;BR /&gt;PROC SORT DATA= Insider; by PERSONID CUSIP6 YEAR; RUN;&lt;BR /&gt;proc sql; select unique a.*, ifn(count(b.YEAR) &amp;gt; 3, mean(b.Annual_order), .) as AVG&lt;BR /&gt;from Insider a left join Insider b on a.PERSONID = b.PERSONID and a.CUSIP6 = b.CUSIP6&lt;BR /&gt;and b.YEAR between a.YEAR-10 and a.YEAR&lt;BR /&gt;group by 1, 2 order by 1, 2; quit;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;What seems to be the problem? Can you provide a way out for me?&lt;BR /&gt;&lt;BR /&gt;Really appreciate your help!!</description>
      <pubDate>Mon, 17 Jan 2022 10:10:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790488#M253098</guid>
      <dc:creator>KS99</dc:creator>
      <dc:date>2022-01-17T10:10:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate moving averages if given at least 4 obs. over 10 years, regardless of gaps.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790494#M253101</link>
      <description>1. You dont need to sort&lt;BR /&gt;2. Add    create table</description>
      <pubDate>Mon, 17 Jan 2022 11:03:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790494#M253101</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2022-01-17T11:03:32Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate moving averages if given at least 4 obs. over 10 years, regardless of gaps.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790504#M253109</link>
      <description>&lt;PRE&gt;Assuming your data has been sorted by id and year.

&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;

data HAVE;
  input ID YEAR VAL;
  cards;
1. 1990. 0.24
2. 2003. 0.13
2. 2004. 0.22
2. 2005. 0.26
2. 2006. 0.1
2. 2007 0.95
5. 1998. 0.2
5. 1999. 0.33
5. 2000. 0.42
5 2001. 0.01
5. 2002. 0.42
5. 2004. 0.54
5. 2005. 0.83
5. 2006. 0.13
5. 2007. 0.25
5. 2011. 0.98
5. 2012. 0.4
5. 2016. 0.32
5. 2018. 0.15
;
run;


data want;
if _n_=1 then do;
 k=.;v=.;
 declare hash h();
 h.definekey('k');
 h.definedata('v');
 h.definedone();
end;


do until(last.id);
 set have;
 by id;
 h.add(key:year,data:val);

 n=0;sum=0;
 do k=year-10 to year;
  if h.find()=0 then do;n+1;sum+v; end;
 end;

 if n&amp;gt;3 then m_avg=sum/n;
 output;
end;


h.clear();
drop k v n sum;
run;&lt;/PRE&gt;</description>
      <pubDate>Mon, 17 Jan 2022 12:29:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790504#M253109</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2022-01-17T12:29:23Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate moving averages if given at least 4 obs. over 10 years, regardless of gaps.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790550#M253127</link>
      <description>&lt;P&gt;Not sure about speed but you should be able to understand this code and easily modify it.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data HAVE;
	infile cards dsd truncover;
	input ID YEAR VAL;
	cards;
1, 1990, 0.24
2, 2003, 0.13
2, 2004, 0.22
2, 2005, 0.26
2, 2006, 0.1
2, 2007, 0.95
5, 1998, 0.2
5, 1999, 0.33
5, 2000, 0.42
5, 2001, 0.01
5, 2002, 0.42
5, 2004, 0.54
5, 2005, 0.83
5, 2006, 0.13
5, 2007, 0.25
5, 2011, 0.98
5, 2012, 0.4
5, 2016, 0.32
5, 2018, 0.15
;
	;
	;
run;



*get first and last year for each ID to create skeleton table;
proc sql;
	create table years as select ID, min(year) as start_year, max(year) as 
		end_year from have group by ID order by 1;
quit;

*build out values for each year;
data skeleton;
	set years;

	do year=start_year to end_year;
		output;
	end;
run;

proc sort data=have;
	by id year;
run;

*merge in main table and calculate moving average at the same time;
data want;
	merge skeleton have;
	by ID year;
	array _val(0:9) _temporary_;
	
	_val{mod(_n_,10)} = val;

	if first.ID then
		call missing(of _val{*});

	if n(of _val(*))&amp;gt;=4 then
		avg=mean(of _val(*));
		
		drop start_year end_year;

		
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/365070"&gt;@KS99&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;Hi, thank you in advance.&lt;BR /&gt;I want to calculate moving averages over the preceding 10 years.&lt;BR /&gt;If 10 years are not fully supplied, I can calculate based on at least 4 obs. over the preceding 10 years (Gaps in years do not matter).&lt;BR /&gt;&lt;BR /&gt;So, if the below data are given:&lt;BR /&gt;ID. YEAR. VALS&lt;BR /&gt;1. 1990. 0.24&lt;BR /&gt;2. 2003. 0.13&lt;BR /&gt;2. 2004. 0.22&lt;BR /&gt;2. 2005. 0.26&lt;BR /&gt;2. 2006. 0.1&lt;BR /&gt;2. 2007 0.95&lt;BR /&gt;5. 1998. 0.2&lt;BR /&gt;5. 1999. 0.33&lt;BR /&gt;5. 2000. 0.42&lt;BR /&gt;5 2001. 0.01&lt;BR /&gt;5. 2002. 0.42&lt;BR /&gt;5. 2004. 0.54&lt;BR /&gt;5. 2005. 0.83&lt;BR /&gt;5. 2006. 0.13&lt;BR /&gt;5. 2007. 0.25&lt;BR /&gt;5. 2011. 0.98&lt;BR /&gt;5. 2012. 0.4&lt;BR /&gt;5. 2016. 0.32&lt;BR /&gt;5. 2018. 0.15&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;My desired output is as follows:&lt;BR /&gt;&lt;BR /&gt;1. 1990. 0.24. .&lt;BR /&gt;2. 2003. 0.13. .&lt;BR /&gt;2. 2004. 0.22. .&lt;BR /&gt;2. 2005. 0.26. .&lt;BR /&gt;2. 2006. 0.1. 0.18&lt;BR /&gt;2. 2007. 0.95. 0.33&lt;BR /&gt;5. 1998. 0.2. .&lt;BR /&gt;5. 1999. 0.33. .&lt;BR /&gt;5. 2000. 0.42. .&lt;BR /&gt;5. 2001 0.01. 0.24&lt;BR /&gt;5. 2002. 0.42. 0.28&lt;BR /&gt;5. 2004. 0.54. 0.32&lt;BR /&gt;5. 2005 0.83. 0.39&lt;BR /&gt;5. 2006 0.13. 0.36&lt;BR /&gt;5. 2007 0.25. 0.35&lt;BR /&gt;5. 2011 0.98. 0.45&lt;BR /&gt;5. 2012. 0.4. 0.50&lt;BR /&gt;5. 2016 0.32. 0.42&lt;BR /&gt;5. 2018 0.15. 0.46&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;For ID 2 Year 2006, the moving average is over 2003~2006.&lt;BR /&gt;For ID 2 Year 2007, the moving average is over 2003~2007.&lt;BR /&gt;In the case of ID 5, the values for 2001~2007 are the averages starting from 1998, ending in the year of interest.&lt;BR /&gt;The value for 2011 is the average over 2001~2011.&lt;BR /&gt;The value for 2016, for example, is the average over 2006~2016.&lt;BR /&gt;&lt;BR /&gt;I was helped by SAS communities for many times in the past. I appreciate your help in advance!&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 17 Jan 2022 17:16:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790550#M253127</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2022-01-17T17:16:02Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate moving averages if given at least 4 obs. over 10 years, regardless of gaps.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790579#M253136</link>
      <description>Thank you so much, ChrisNZ!&lt;BR /&gt;I will use your codes for further reference.</description>
      <pubDate>Mon, 17 Jan 2022 19:52:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790579#M253136</guid>
      <dc:creator>KS99</dc:creator>
      <dc:date>2022-01-17T19:52:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate moving averages if given at least 4 obs. over 10 years, regardless of gaps.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790580#M253137</link>
      <description>Thank you KSharp, it works.</description>
      <pubDate>Mon, 17 Jan 2022 19:53:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790580#M253137</guid>
      <dc:creator>KS99</dc:creator>
      <dc:date>2022-01-17T19:53:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate moving averages if given at least 4 obs. over 10 years, regardless of gaps.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790581#M253138</link>
      <description>Thank you Reeza, for your reports!&lt;BR /&gt;I will keep your codes for future use!</description>
      <pubDate>Mon, 17 Jan 2022 19:54:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-moving-averages-if-given-at-least-4-obs-over-10/m-p/790581#M253138</guid>
      <dc:creator>KS99</dc:creator>
      <dc:date>2022-01-17T19:54:25Z</dc:date>
    </item>
  </channel>
</rss>

