<?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: moving skewness in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/moving-skewness/m-p/747237#M234498</link>
    <description>Thank you for your reply.&lt;BR /&gt;&lt;BR /&gt;The process is similar to moving average and standard deviation in proc expand. It is a 5-year moving skewness that requires at least three values. the values for id=1 are calculated as follows:&lt;BR /&gt;&lt;BR /&gt;id year x skew&lt;BR /&gt;1 2000 5 0 = skew(5)&lt;BR /&gt;1 2001 6 0 = skew(5,6)&lt;BR /&gt;1 2002 . 0 = skew (5,6) because 2002 is missing&lt;BR /&gt;1 2003 9 1.29334278 =skew(5,6,9)&lt;BR /&gt;1 2004 4.6 1.5163234 =skew(5,6,9,4.6)&lt;BR /&gt;1 2005 7 0.43480062 = skew(6,9,4.6,7)&lt;BR /&gt;1 2006 3.9 0.51928908 = skew(9,4.6,7,3.9)&lt;BR /&gt;1 2007 4 0.97698034 = skew(9,4.6,7,3.9,4)</description>
    <pubDate>Thu, 10 Jun 2021 23:23:10 GMT</pubDate>
    <dc:creator>AmirSari</dc:creator>
    <dc:date>2021-06-10T23:23:10Z</dc:date>
    <item>
      <title>moving skewness</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-skewness/m-p/747227#M234493</link>
      <description>&lt;P&gt;Hi All,&lt;/P&gt;&lt;P&gt;I am trying to find a way to calculate a moving skewness by group for my sample. The window is 5 years from t-4 to t. I tried to use proc expand but it does not seem to have an option for skewness. Any help is greatly appreciated.&amp;nbsp;&lt;/P&gt;&lt;P&gt;here is a sample dataset.&lt;/P&gt;&lt;PRE&gt;id year x&lt;BR /&gt;1 2000 5&lt;BR /&gt;1 2001 6&lt;BR /&gt;1 2002 .&lt;BR /&gt;1 2003 9&lt;BR /&gt;1 2004 4.6&lt;BR /&gt;1 2005 7&lt;BR /&gt;1 2006 3.9&lt;BR /&gt;1 2007 4&lt;BR /&gt;2 2000 6&lt;BR /&gt;2 2001 3&lt;BR /&gt;2 2002 .&lt;BR /&gt;2 2003 2&lt;BR /&gt;2 2004 .&lt;BR /&gt;2 2005 7&lt;BR /&gt;2 2006 6&lt;BR /&gt;2 2007 8&lt;/PRE&gt;&lt;P&gt;here is my desired output:&lt;/P&gt;&lt;PRE&gt;id year x skew&lt;BR /&gt;1 2000 5 0&lt;BR /&gt;1 2001 6 0&lt;BR /&gt;1 2002 . 0&lt;BR /&gt;1 2003 9 1.29334278&lt;BR /&gt;1 2004 4.6 1.5163234&lt;BR /&gt;1 2005 7 0.43480062&lt;BR /&gt;1 2006 3.9 0.51928908&lt;BR /&gt;1 2007 4 0.97698034&lt;BR /&gt;2 2000 6 0&lt;BR /&gt;2 2001 3 0&lt;BR /&gt;2 2002 . 0&lt;BR /&gt;2 2003 2 1.29334278&lt;BR /&gt;2 2004 . 1.29334278&lt;BR /&gt;2 2005 7 1.45786297&lt;BR /&gt;2 2006 6 -1.457863&lt;BR /&gt;2 2007 8 -1.4430588&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;Thank you!&lt;/P&gt;</description>
      <pubDate>Thu, 10 Jun 2021 22:21:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-skewness/m-p/747227#M234493</guid>
      <dc:creator>AmirSari</dc:creator>
      <dc:date>2021-06-10T22:21:51Z</dc:date>
    </item>
    <item>
      <title>Re: moving skewness</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-skewness/m-p/747229#M234495</link>
      <description>&lt;P&gt;Your results really look like a T-3 to T window.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is my attempt however you may need to share exactly how you are getting values for some of your "results". I get a matching value, barring rounding, for Id =1 and year=2004 but not for year 2005. I do not believe that your Id=2 and year=2003 and year=2004 could have the same result. 2003 would be skewness of 6, 3 and 2 but 2004 would be skewness of 3 and 2.&lt;/P&gt;
&lt;PRE&gt;data have;
   input id year x;
datalines;
1 2000 5
1 2001 6
1 2002 .
1 2003 9
1 2004 4.6
1 2005 7
1 2006 3.9
1 2007 4
2 2000 6
2 2001 3
2 2002 .
2 2003 2
2 2004 .
2 2005 7
2 2006 6
2 2007 8
;

data want;
   set have;
   by id;
   lx1=lag1(x);
   lx2=lag2(x);
   lx3=lag3(x);

   if first.id then yrcount=1;
   else yrcount+1;
   if yrcount ge 4 then skew = skewness(x, lx1, lx2, lx3);
   else skew = 0;
   drop lx: ;
run;&lt;BR /&gt;&lt;BR /&gt;
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 10 Jun 2021 22:40:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-skewness/m-p/747229#M234495</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-10T22:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: moving skewness</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-skewness/m-p/747237#M234498</link>
      <description>Thank you for your reply.&lt;BR /&gt;&lt;BR /&gt;The process is similar to moving average and standard deviation in proc expand. It is a 5-year moving skewness that requires at least three values. the values for id=1 are calculated as follows:&lt;BR /&gt;&lt;BR /&gt;id year x skew&lt;BR /&gt;1 2000 5 0 = skew(5)&lt;BR /&gt;1 2001 6 0 = skew(5,6)&lt;BR /&gt;1 2002 . 0 = skew (5,6) because 2002 is missing&lt;BR /&gt;1 2003 9 1.29334278 =skew(5,6,9)&lt;BR /&gt;1 2004 4.6 1.5163234 =skew(5,6,9,4.6)&lt;BR /&gt;1 2005 7 0.43480062 = skew(6,9,4.6,7)&lt;BR /&gt;1 2006 3.9 0.51928908 = skew(9,4.6,7,3.9)&lt;BR /&gt;1 2007 4 0.97698034 = skew(9,4.6,7,3.9,4)</description>
      <pubDate>Thu, 10 Jun 2021 23:23:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-skewness/m-p/747237#M234498</guid>
      <dc:creator>AmirSari</dc:creator>
      <dc:date>2021-06-10T23:23:10Z</dc:date>
    </item>
    <item>
      <title>Re: moving skewness</title>
      <link>https://communities.sas.com/t5/SAS-Programming/moving-skewness/m-p/747243#M234502</link>
      <description>&lt;P&gt;NOT a 5 year moving because of&lt;/P&gt;
&lt;P&gt;1 2003 9 1.29334278 =skewness (5,6,., 9) That is 4 year calculation.&lt;/P&gt;
&lt;P&gt;So you need to explain if that is a 5 year where the 5th value comes from to use as a 5 year.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This duplicates your output but note that it forces a 4-year value.&lt;/P&gt;
&lt;PRE&gt;data want;
   set have;
   by id;
   lx1=lag1(x);
   lx2=lag2(x);
   lx3=lag3(x);
   lx4=lag4(x);

   if first.id then yrcount=1;
   else yrcount+1;
   if yrcount = 4 then  skew = skewness(x, lx1, lx2, lx3);
   else if yrcount ge 5 then skew = skewness(x, lx1, lx2, lx3, lx4);
   else skew = 0;
   drop lx: ;
run;&lt;/PRE&gt;
&lt;P&gt;So you may need to consider what T-4 to T means.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Jun 2021 00:10:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/moving-skewness/m-p/747243#M234502</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-11T00:10:38Z</dc:date>
    </item>
  </channel>
</rss>

