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
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;
read all var{mainstrategy date return};
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;
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;
read all var{mainstrategy date return};
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;
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.
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.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.
Find more tutorials on the SAS Users YouTube channel.