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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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;

View solution in original post

5 REPLIES 5
Ksharp
Super User

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;
bukky09
Fluorite | Level 6
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
Rick_SAS
SAS Super FREQ

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.

bukky09
Fluorite | Level 6
Thank you very much it work so I guess "streaminit (123)" is a standard word that is part of the code
ballardw
Super User

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.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

Multiple Linear Regression in SAS

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.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 5 replies
  • 1173 views
  • 1 like
  • 4 in conversation