Hi All,
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.
here is a sample dataset.
id year x
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
here is my desired output:
id year x skew
1 2000 5 0
1 2001 6 0
1 2002 . 0
1 2003 9 1.29334278
1 2004 4.6 1.5163234
1 2005 7 0.43480062
1 2006 3.9 0.51928908
1 2007 4 0.97698034
2 2000 6 0
2 2001 3 0
2 2002 . 0
2 2003 2 1.29334278
2 2004 . 1.29334278
2 2005 7 1.45786297
2 2006 6 -1.457863
2 2007 8 -1.4430588
Thank you!
NOT a 5 year moving because of
1 2003 9 1.29334278 =skewness (5,6,., 9) That is 4 year calculation.
So you need to explain if that is a 5 year where the 5th value comes from to use as a 5 year.
This duplicates your output but note that it forces a 4-year value.
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;
So you may need to consider what T-4 to T means.
Your results really look like a T-3 to T window.
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.
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;
NOT a 5 year moving because of
1 2003 9 1.29334278 =skewness (5,6,., 9) That is 4 year calculation.
So you need to explain if that is a 5 year where the 5th value comes from to use as a 5 year.
This duplicates your output but note that it forces a 4-year value.
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;
So you may need to consider what T-4 to T means.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.