BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
hellohere
Pyrite | Level 9

Trying to STORE Functions inside Proc IML. Somehow get confused. One stored and the other did not. 

Anyone can run the two codes, find the tricky thing?! Thanks, 

 

My Run, the first code says store. The second did not (code and log are below). 

proc iml;
/* Simple moving average of k data values.
   First k-1 values are assigned the mean of all previous values.
   Inputs:     y     column vector of length N >= k
               k     number of data points to use to construct each average
*/
start MA(y, k);
   MA = j(nrow(y), 1, .);
   do i = 1 to nrow(y);
      idx = max(1,(i-k+1)):i;   /* rolling window of data points */
      MA[i] = mean( y[idx] );   /* compute average */
   end;
   return ( MA );
finish;

/* Weighted moving average of k data values.
   First k-1 values are assigned the weighted mean of all preceding values.
   Inputs:     y     column vector of length N >= k
               wt    column vector of weights. w[k] is weight for most recent 
                      data; wt[1] is for most distant data value.  The function 
                     internally standardizes the weights so that sum(wt)=1.
   Example call: WMA  = WMA(y, 1:5);
*/
start WMA(y, wt);
   w = colvec(wt) / sum(wt);       /* standardize weights so that sum(w)=1 */
   k = nrow(w);
   MA = j(nrow(y), 1, .);
   /* handle first k values separately */
   do i = 1 to k-1;
      wIdx = k-i+1:k;                 /* index for previous i weights */
      tIdx = 1:i;                     /* index for previous i data values */
      MA[i] = sum(wt[wIdx]#y[tIdx]) / sum(wt[wIdx]);  /* weighted average */
   end;
   /* main computation: average of current and previous k-1 data values */
   do i = k to nrow(y);
      idx = (i-k+1):i;               /* rolling window of k data points */
      MA[i] = sum( w#y[idx] );       /* weighted sum of k data values */
   end;
   return ( MA );
finish;

/* Exponentially weighted moving average (EWMA) with smoothing parameter alpha.
   REF: http://www.sascommunity.org/sugi/SUGI90/Sugi-90-76%20Brocklebank.pdf
        https://en.wikipedia.org/wiki/Exponential_smoothing
   Inputs:      y     column vector of length N
                alpha scalar value 0 < alpha < 1
*/
start EWMA(y, alpha);
   MA = j(nrow(y), 1, .);
   MA[1] = y[1];              /* initialize first value of smoother */
   do i = 2 to nrow(y);
      MA[i] = alpha*y[i] + (1-alpha)*MA[i-1];
   end;
   return ( MA );
finish;

/* read time series data */
use Sashelp.Air;  
   read all var "date" into t;
   read all var "air" into y;
close;
MA   = MA(y, 5);           /* moving average, k=5 */
WMA  = WMA(y, 1:5);        /* weighted moving average */
EWMA = EWMA(y, 0.3);       /* exponentially WMA, alpha=0.3 */
 
create _air_out var{t y MA WMA EWMA};  append;  close _air_out;
store MA WMA EWMA;			/* store function*/

run;quit;
proc iml;
	start reverse1D(revx, A);
		len=max(dimension(A));
		ind=len:1; 
		revx=A[ind];
	finish;

	start reverse2D(revx, B);
		dim=dimension(B);
		dim1=dim[1]; dim2=dim[2];
	 	rowind=dim1:1;
		colind=dim2:1;
		revx=B[rowind, colind];	
	finish;
	A={1 2 3 4 5 6 7 8 9 . . .};
	B={1 2 3 4, 8 7 6 5};
	run reverse1D(revA,A);
	run reverse2D(revB,B);
	print revA revB;

	store reverse1D reverse2D; /*why complains and fails!!?*/
run;quit;
314  proc iml;
NOTE: IML Ready
315      start reverse1D(revx, A);
316          len=max(dimension(A));
317          ind=len:1;
318          revx=A[ind];
319      finish;
NOTE: Module REVERSE1D defined.
320
321      start reverse2D(revx, B);
322          dim=dimension(B);
323          dim1=dim[1];
323!                      dim2=dim[2];
324          rowind=dim1:1;
325          colind=dim2:1;
326          revx=B[rowind, colind];
327      finish;
NOTE: Module REVERSE2D defined.
328      A={1 2 3 4 5 6 7 8 9 . . .};
329      B={1 2 3 4, 8 7 6 5};
330      run reverse1D(revA,A);
331      run reverse2D(revB,B);
332      print revA revB;
333
334      store reverse1D reverse2D;
NOTE: Opening storage library WORK.IMLSTOR
ERROR: Matrix reverse1D has not been set to a value.
334!                                /*why complains and fails!!?*/
335  run;
NOTE: Module MAIN is undefined in IML; cannot be RUN.
335!     quit;
NOTE: Exiting IML.
NOTE: Storage library WORK.IMLSTOR closed.
NOTE: PROCEDURE IML used (Total process time):
      real time           0.03 seconds
      cpu time            0.03 seconds

 

1 ACCEPTED SOLUTION

Accepted Solutions
hellohere
Pyrite | Level 9

SRY to bother. Finally get the diff. between Function and Module. 

View solution in original post

2 REPLIES 2
hellohere
Pyrite | Level 9

SRY to bother. Finally get the diff. between Function and Module. 

Rick_SAS
SAS Super FREQ

Here is a link to the documentation of the STORE statement. The correct syntax for storing a module is 

store module=(MA WMA EWMA);

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 2 replies
  • 685 views
  • 0 likes
  • 2 in conversation