Statistical programming, matrix languages, and more

Running a macro loop inside proc IML SAS 9.4

Occasional Contributor
Posts: 5

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 ;

Super User
Posts: 10,278

Re: Running a macro loop inside proc IML SAS 9.4

Posted in reply to kaushik07

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
How to convert datasets to data steps
How to post code
Posts: 4,243

Re: Running a macro loop inside proc IML SAS 9.4

Posted in reply to kaushik07

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);


Super User
Posts: 10,784

Re: Running a macro loop inside proc IML SAS 9.4

Posted in reply to kaushik07
In IML, CALL EXECUTEFILE() is very useful tool for such kind of scenario .

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