Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- SAS Procedures
- /
- How to calculate teh rolling standard deviations?

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

04-04-2013 09:03 PM

I have 30 years of profit data, and I want to calculate standard deviation for each year using past 5 years profits. The first year with standard deviation data should be the 6th year. ( I calculate the standard deviation for year 6 to year 30 ).

Please help.

Thanks.

Accepted Solutions

Solution

04-04-2013
10:42 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Anna_Guo

04-04-2013 10:42 PM

Below code works under the assumption that you don't have missing years.

data want;

array arr_prft {5} 8. _temporary_;

set WORK.ROLLIMG_STANDARD_DEVIATION;

by gvkey year;

length std_prft 8;

retain std_calc_flag 0;

element+1;

if element>dim(arr_prft) then

do;

element=1;

std_calc_flag=1;

end;

arr_prft[element]=prft;

if std_calc_flag then

std_prft=std(of arr_prft

output;

if last.gvkey then

call missing(of _all_);

run;

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Anna_Guo

04-04-2013 10:04 PM

Since you don't have sample data, I can only give out general suggestions.

1. Use Proc Expand. It has moving standard deviation built-in. it is the easiest way, but it requires license of SAS/ETS.

2. Use Lagn(), in your case, Lag5().

3. Use temporary Array(), presumably the most efficient one?

Upon seeing your sample data, all of these approaches could be finalized.

Haikuo

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Haikuo

04-04-2013 10:19 PM

Solution

04-04-2013
10:42 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Anna_Guo

04-04-2013 10:42 PM

Below code works under the assumption that you don't have missing years.

data want;

array arr_prft {5} 8. _temporary_;

set WORK.ROLLIMG_STANDARD_DEVIATION;

by gvkey year;

length std_prft 8;

retain std_calc_flag 0;

element+1;

if element>dim(arr_prft) then

do;

element=1;

std_calc_flag=1;

end;

arr_prft[element]=prft;

if std_calc_flag then

std_prft=std(of arr_prft

output;

if last.gvkey then

call missing(of _all_);

run;

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Patrick

04-04-2013 10:46 PM

Patrick,

Thanks. It is very likely I have missing years. If so, what kind of modifications should be?

Anna

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Anna_Guo

04-04-2013 11:26 PM

As Hai.kuo pointed out SAS/ETS procedures like Proc Expand and Proc Timeseries would do the job for you.

For the code I've posted easiest would be to fix up the time series adding years with missing prices.

data All_Years(keep=gvkey year);

set WORK.ROLLIMG_STANDARD_DEVIATION(keep=gvkey year);

by gvkey year;

lag_year=lag(year);

source_year=year;

if first.gvkey then output;

else

do year=lag_year+1 to source_year;

output;

end;

run;

data want;

array arr_prft {5} 8. _temporary_;

merge WORK.ROLLIMG_STANDARD_DEVIATION All_Years;

by gvkey year;

length std_prft 8;

retain std_calc_flag 0;

element+1;

if element>dim(arr_prft) then

do;

element=1;

std_calc_flag=1;

end;

if std_calc_flag then

std_prft=std(of arr_prft

arr_prft[element]=prft;

output;

if last.gvkey then

call missing(of _all_);

run;

There was a logical error in my code. Please note that the calculation for the standard deviation must come BEFORE assigning the current prft to the array element.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Patrick

02-03-2014 08:13 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to comeon2012

02-04-2014 12:48 AM

I'm sure Ksharp's answer will solve your problem beautifully.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Patrick

04-04-2013 10:51 PM

Patrick,

Thanks. I think it is good enough for me.

Anna