BookmarkSubscribeRSS Feed
SASingaKorean
Calcite | Level 5

The summary of my question: I have a sas file ("KF.sas") that produces parameter estimates of state space model equations through Kalman Filter algorithm and Maximum Likelihood method. With inputs of individual fund's return, "KF.sas" produces the individual fund's parameter estimates. However, I need to find the parameter estimates of 200 mutual funds in order to do a bootstrap analysis or out-of-sample analysis. The number of mutual funds are overwhelming. Plus, I need to divide the time series of 16 years into 4 years and apply "KF.sas" to each 4 year periods. So, I am basically applying "KF.sas" to more than 200x4=800 times. Could you help me making a macro that applies "KF.sas" to a group of mutual funds?

 

Here is detail. Suppose that I have the following monthly return time series data of mutual funds, say work.mutualfunds :


example.PNG

 

 

The first column is trading month which runs over from 200207 to 201806, which gives you 192 months. For simplicity, let's assume that there are 10 funds, whose returns are in the columns ExPR_0:ExPR_9. The columns mktrf, SMB, VMG are market information. In order to find the parameter estimates of fund ExPR_0 over 200207 to 200606 (which is the first one-fourth row of the column ExPR_0), I can write a code as follows.

 

 

%let infile = work.mutualfunds;



%let variable1 = ExPR_0 (In fact, I first need to divide ExPR_0 (192 months) into 48 months, and then plug into variable 1);

%let variable2 = mktrf (In fact, I first need to divide ExPR_0 (192 months) into 48 months, and then plug into variable 2);

%let variable3 = SMB (In fact, I first need to divide ExPR_0 (192 months) into 48 months, and then plug into variable 3);

%let variable4 = VMG (In fact, I first need to divide ExPR_0 (192 months) into 48 months, and then plug into variable 4);



/* initial states and parameters required to run Kalman Filter*/

%let lead =0;

%let mu0=0;

%let sigma0 =0.7 ;

%let v  = 0.3;

%let alpha =0.05;

%let beta=1;

%let gamma=0.15;

%let delta=1;

%let sigma_eta=0.7;

%let sigma_eps=0.01;


/* Condition for convergence in Maximum likelihood method*/ %let opt = %str ({0 2 2 2 2 2 2}); %let tc = %str ({3000 9000}); %let blc = %str ({ . . . . . 0 0,                             . . . . . . .}); /* Run Kalman Filter*/ %include "KF.sas";  

 

This produces the parameter estimates of an individual fund over 48 months. Could you give me some tips for  making a macro for applying this to the rest of funds (ExPR_1:ExPR_9) and time periods (49-96 months, 97-144 months, 145-192 months) ?

 

For individual funds the above code takes 2 minutes to produce the result. The above is just an example. The number of mutual funds that I really need to deal with is more than 200. I am worried about the time it takes. I read a few articles that recommend BY-statement for Bootstrap analysis. However, I am not sure if similar BY-statement can be useful for my analysis.

 

I would be grateful if someone could help me. Thank you.

 

 

 

1 REPLY 1
Kurt_Bremser
Super User

When making code dynamic, you need to follow these steps:

  1. have working code for a single instance (you have that in KF.sas)
  2. identify all things in the code (statements, values, variable names etc) that have to change from one run to another
  3. then decide if you can set up an identifier that allows BY-group processing
  4. only if the previous point can't be done, prepare a macro:
  5. replace all changing elements with macro variables
  6. wrap the code into a macro definition, where the changing elements are supplied as parameters (or derived from the parameters)
  7. set up a means to call the macro repeatedly (from a loop, from a dataset, ...)

So you need to show us first what is relevant with regards to #2.

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 792 views
  • 0 likes
  • 2 in conversation