## first orde autocorrelation

Solved
Occasional Contributor
Posts: 15

# first orde autocorrelation

Hi,

I am trying to find the first order autocorrelation of monthly returns for each mainstrategy. This autocorrelation should be calculated based on a 5 year rolling window. I need some help with the approprate code. I have tried others but didnt work.

so I have a data set as such

Date       Return     AUM    mainstrategy

199512   -0.0055   26.9     Relative value

199601    0.0048   27.1      Relative value

199602    0.0089   30.7      CTA

Thank you

Accepted Solutions
Solution
‎03-07-2016 05:16 AM
Super User
Posts: 10,681

## Re: first orde autocorrelation

Here is the code you could get start.

``````data have;
call streaminit(1234);
length 	mainstrategy \$ 20;
do mainstrategy='Relative value','CTA';
do year=1990 to 2010;
do month=1 to 12;
date=mdy(month,1,year);
return=rand('uniform');
output;
end;
end;
end;
drop year month ;
format date yymmn6.;
run;

proc iml;
use have nobs nobs;
close;

start_end=t(loc( mainstrategy^=t( {' '}||remove(mainstrategy,nobs) ) ))||
t(loc( mainstrategy^=t( remove(mainstrategy,1)||{' '}) ) );
corr=j(nobs,1,.);
do i=1 to nrow(start_end);
do j=start_end[i,1] to start_end[i,2]-1-5*12;
corr[j,1]=corr(return[j:j+5*12]||return[j+1:j+1+5*12])[2];
end;
end;

create corr var{corr};
append ;
close;
quit;
data want;
merge have corr;
run;``````

All Replies
Solution
‎03-07-2016 05:16 AM
Super User
Posts: 10,681

## Re: first orde autocorrelation

Here is the code you could get start.

``````data have;
call streaminit(1234);
length 	mainstrategy \$ 20;
do mainstrategy='Relative value','CTA';
do year=1990 to 2010;
do month=1 to 12;
date=mdy(month,1,year);
return=rand('uniform');
output;
end;
end;
end;
drop year month ;
format date yymmn6.;
run;

proc iml;
use have nobs nobs;
close;

start_end=t(loc( mainstrategy^=t( {' '}||remove(mainstrategy,nobs) ) ))||
t(loc( mainstrategy^=t( remove(mainstrategy,1)||{' '}) ) );
corr=j(nobs,1,.);
do i=1 to nrow(start_end);
do j=start_end[i,1] to start_end[i,2]-1-5*12;
corr[j,1]=corr(return[j:j+5*12]||return[j+1:j+1+5*12])[2];
end;
end;

create corr var{corr};
append ;
close;
quit;
data want;
merge have corr;
run;``````
Occasional Contributor
Posts: 15

## Re: first orde autocorrelation

Hi,
Thanks for your reply. I would like to know if the "streaminit (123)" is a standard sas word or it means i should be calling from my own data set
SAS Super FREQ
Posts: 4,171

## Re: first orde autocorrelation

[ Edited ]

Since you did not provide data, Xia used the DATA step to generate random data. CALL STREAMINIT is a DATA step function that is used when generating random numbers.  It ensures that two people will see the same set of random values, thus is very useful on discussion forums.  You will not need to use that call when you analyze real data.

By the way, it is always good practice to provide data (real or fake) when you ask a question. It saves the experts a lot of time and will motivate more people to attempt a solution to your questions.

Occasional Contributor
Posts: 15

## Re: first orde autocorrelation

Thank you very much it work so I guess "streaminit (123)" is a standard word that is part of the code
Super User
Posts: 13,293

## Re: first orde autocorrelation

Please do not post the same the question in multiple locations. It is likely to get multiple similar responses or questions. When the responses are scattered across multiple threads then the solution may not be clear and then the "correct answer" is only attributed to one thread.

If the topic is not in an appropriate section of the forum then the thread may be moved to a more appropriate area.

🔒 This topic is solved and locked.