<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic IML/STORE, why one stored and the other did not in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/IML-STORE-why-one-stored-and-the-other-did-not/m-p/832709#M5860</link>
    <description>&lt;P&gt;Trying to STORE Functions inside Proc IML. Somehow get confused. One stored and the other did not.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyone can run the two codes, find the tricky thing?! Thanks,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My Run, the first code says store. The second did not (code and log are below).&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;gt;= 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 &amp;gt;= 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 &amp;lt; alpha &amp;lt; 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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 11 Sep 2022 01:48:53 GMT</pubDate>
    <dc:creator>hellohere</dc:creator>
    <dc:date>2022-09-11T01:48:53Z</dc:date>
    <item>
      <title>IML/STORE, why one stored and the other did not</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/IML-STORE-why-one-stored-and-the-other-did-not/m-p/832709#M5860</link>
      <description>&lt;P&gt;Trying to STORE Functions inside Proc IML. Somehow get confused. One stored and the other did not.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyone can run the two codes, find the tricky thing?! Thanks,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My Run, the first code says store. The second did not (code and log are below).&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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 &amp;gt;= 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 &amp;gt;= 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 &amp;lt; alpha &amp;lt; 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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;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

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Sep 2022 01:48:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/IML-STORE-why-one-stored-and-the-other-did-not/m-p/832709#M5860</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2022-09-11T01:48:53Z</dc:date>
    </item>
    <item>
      <title>Re: IML/STORE, why one stored and the other did not</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/IML-STORE-why-one-stored-and-the-other-did-not/m-p/832711#M5861</link>
      <description>&lt;P&gt;SRY to bother. Finally get the diff. between Function and Module.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Sep 2022 02:06:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/IML-STORE-why-one-stored-and-the-other-did-not/m-p/832711#M5861</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2022-09-11T02:06:33Z</dc:date>
    </item>
    <item>
      <title>Re: IML/STORE, why one stored and the other did not</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/IML-STORE-why-one-stored-and-the-other-did-not/m-p/832848#M5863</link>
      <description>&lt;P&gt;Here is a link to &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/v_019/imlug/imlug_langref_sect488.htm" target="_self"&gt;the documentation of the STORE statement&lt;/A&gt;. The correct syntax for storing a module is&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;store module=(MA WMA EWMA);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 12 Sep 2022 10:03:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/IML-STORE-why-one-stored-and-the-other-did-not/m-p/832848#M5863</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2022-09-12T10:03:27Z</dc:date>
    </item>
  </channel>
</rss>

