BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
AmirSari
Quartz | Level 8

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!

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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.

 

View solution in original post

3 REPLIES 3
ballardw
Super User

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;

 

AmirSari
Quartz | Level 8
Thank you for your reply.

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:

id year x skew
1 2000 5 0 = skew(5)
1 2001 6 0 = skew(5,6)
1 2002 . 0 = skew (5,6) because 2002 is missing
1 2003 9 1.29334278 =skew(5,6,9)
1 2004 4.6 1.5163234 =skew(5,6,9,4.6)
1 2005 7 0.43480062 = skew(6,9,4.6,7)
1 2006 3.9 0.51928908 = skew(9,4.6,7,3.9)
1 2007 4 0.97698034 = skew(9,4.6,7,3.9,4)
ballardw
Super User

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.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 960 views
  • 2 likes
  • 2 in conversation