SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
ADou
Calcite | Level 5

Hi Experts,

 

I'd like to calculate the 60-month standard deviation(sigma) and I have the monthly returns for each company. Now I want to compute the standard deviations for the lat 60-month observations on the rolling bases. For example, I want the standard deviation of stock returns from 1987-02 to 1992-01 to be the sigma of 1992-01.  Do you know any good solutions?

Thanks a lot!

 

ADou

 

10 REPLIES 10
PGStats
Opal | Level 21

proc expand (part of SAS/ETS) would be a very good solution. It has plenty of moving window operations, including the STD.

PG
ADou
Calcite | Level 5

Thanks a lot! But I'm not very familiar with proc expand, could you please give me some example codes? And I've attached a printscreen of mt data. 

 

data.png

 

Thank you!

learsaas
Quartz | Level 8
proc sort data=a;
	by PERMNO Date;
run;
proc expand data=a out=result(drop=time);
	by PERMNO;
	convert Returns = Returns_movstd / transformout=(movstd 60*30);
run;
ADou
Calcite | Level 5

Thanks a lot!

 

ADou

ADou
Calcite | Level 5

Thanks for your example and it works well. But I still have a question. Since I have to delete the companies that have returns less than 12 months, so I need the number of standard deviation of each company in one rolling(60 months). It is something like nrolling_std. How should I modify the codes?

PGStats
Opal | Level 21

If you don't specify a time variable (with the ID statement) proc expand assumes 1 row = 1 time unit. So you can simply say:

 

proc sort data=a;
	by PERMNO Date;
run;

proc expand data=a out=result;
	by PERMNO;
	convert Returns = Returns_std / transformout=(movstd 60);
run;

PG
ADou
Calcite | Level 5

Thank you! 

 

ADou

Ksharp
Super User

CODE NOT TESTED .

 

 

proc sql;
create table have as
 select *,
(select std(return) from have where permno=a.permno and
 date between intnx('month',a.date,-60) and a.date) 
 as rolling_std
  from have as a;
quit;
ADou
Calcite | Level 5

Thanks KSharp! It takes a long time to run the sql.  And I still have a question. Since I have to delete the companies that have returns less than 12 months, so I need the number of standard deviation of each company in one rolling(60 months). It is something like nrolling_std. How should I modify the codes?

Ksharp
Super User

OK.just add one more clause.

 

proc sql;
create table have as
 select *,
(select std(return) from have where permno=a.permno and
 date between intnx('month',a.date,-60) and a.date) 
 as rolling_std,
(select count(*) from have where permno=a.permno and
 date between intnx('month',a.date,-60) and a.date) 
 as n_rolling_std
  from have as a;
quit;

sas-innovate-white.png

Special offer for SAS Communities members

Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

Register now!

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
  • 10 replies
  • 4348 views
  • 1 like
  • 4 in conversation