Statistical programming, matrix languages, and more

Running a macro loop inside proc IML SAS 9.4

New Contributor
Posts: 3

Running a macro loop inside proc IML SAS 9.4

Hi ,

I am writing following the piece of code  to  bootstrap 5000 bi-variate ("Sharpe" "Treynor") sample values  from the file 'rsltdspbetach1' from 'mfsas' library. It is working fine. The issue is I have over 100 such  'rsltdspbetach'  files i.e. rsltdspbetach2, ....., rsltdspbetach1, rsltdspbetach100. Each have "Sharpe" "Treynor" in them . How to perform it  , given the structure ? Macros are not running  inside iml loop.  i, j etc causing  SAs to stop.


proc iml;                                                                                                                          





%let MyData = mfsas.rsltdspbetach1; %let NumSamples = 5000;                                                                             



call randseed(12345);                                                                                                                  


use &MyData;                                                                                                                           


read all var {"Sharpe" "Treynor" } into X;                                                                                             


close &MyData;                                                                                                                         


print X;                                                                                                                               


N=nrow(X); C=ncol(X); print N C ;                                                                                                       


*ndx = SampleReplace(1:N, &NumSamples, N);                                                                                              




*Resample from the rows of  X.  Generate the indices for  all bootstrap resamples with a single call;                                  


ndx = Sample(1:N, N // &NumSamples) ;*-ndx will be a &NumSamples x N matrix;                                                           




n_ndx=nrow(ndx); p_ndx=ncol(ndx); print  n_ndx  p_ndx ;                                                                                


*print (ndx[1:3,1:5]);                                                                                                                 


*print ndx;                                                                                                                            



rho = j(&NumSamples, ncol(X));  *Matrix of 1s.This will be used to hold the results i.e. Y matrices;                                   


*print rho;                                                                                                                             


do i = 1 to &NumSamples ;                                                                                                               



rows = ndx[i, ];                                                                                                                       


*print rows;                                                                                                                            


Y = X[rows, ];                                                                                                                          



  *print X      print Y;                                                                                                               


*rho [i+ (i-1)*(N-1) : i+ i*(N-1), 1:C] =Y  ;    *C=ncol(X);                                                                           





rho[i,] =   c;                                                                                                                          







create dspch1_shtr_bts from rho; append from rho; close dspch1_shtr_bts;                                                                



data mfsas.dspch1_shtr_bts; set dspch1_shtr_bts; run;                                                                                  






quit ;

Esteemed Advisor
Posts: 6,646

Re: Running a macro loop inside proc IML SAS 9.4

You cannot run a macro loop in a SAS procedure, you can only use it to create code for you. Creating dynamic code is the main purpose of the macro processor.

Maxims of Maximally Efficient SAS Programmers
Posts: 3,406

Re: Running a macro loop inside proc IML SAS 9.4

You don't need a macro.  Just use a SAS/IML DO loop to loop over the data sets and do whatever computtion you want.

To  loop over data sets, see the article "Read data sets that are specified by an array of names."


The code will look something like this (untested):


do i = 1 to 100;

   dsname = "mfsas.rsltdspbetach" + strip(char(i));   /* loop over data sets */

   use (dsname);

   /* computation here */

   close (dsname);


Grand Advisor
Posts: 9,576

Re: Running a macro loop inside proc IML SAS 9.4

In IML, CALL EXECUTEFILE() is very useful tool for such kind of scenario .

Ask a Question
Discussion stats
  • 3 replies
  • 4 in conversation