Statistical programming, matrix languages, and more

Running a macro loop inside proc IML SAS 9.4

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

                                                                                                                                        

                                                                                                                                       

c=mean(Y);                                                                                                                              

                                                                                                                                       

rho[i,] =   c;                                                                                                                          

                                                                                                                                       

                                                                                                                                       

end;                                                                                                                                    

                                                                                                                                       

                                                                                                                                        

                                                                                                                                       

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

                                                                                                                                       

                                                                                                                                        

data mfsas.dspch1_shtr_bts; set dspch1_shtr_bts; run;                                                                                  

                                                                                                                                        

end;                                                                                                                                   

                                                                                                                                        

                                                                                                                                     

                                                                                                                                        

quit ;

Esteemed Advisor
Posts: 5,933

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
SAS Super FREQ
Posts: 3,222

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

end;

Grand Advisor
Posts: 9,315

Re: Running a macro loop inside proc IML SAS 9.4

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

http://blogs.sas.com/content/iml/2015/06/15/executefile.html

Post a Question
Discussion Stats
  • 3 replies
  • 173 views
  • 0 likes
  • 4 in conversation