<?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 Re: How to save out constant value and own functions withing PROC IML?! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832703#M329146</link>
    <description>&lt;P&gt;The DOC helps huge. Still what is diff. between Function and Module?!&lt;/P&gt;
&lt;P&gt;I tried and saved out the three Functions, and can see it in IMLSTOR.&lt;/P&gt;
&lt;P&gt;When show-storage, log shows load of three matrices.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When invoke, it complains... Any help?!&lt;/P&gt;
&lt;P&gt;Awkward enough, the dataset is created with expected values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&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 out;
store MA WMA EWMA;			/* store function*/
run;quit;

proc iml;
show storage;
load MA WMA;
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 */ 
 
create _air_out_x var{t y MA WMA};  append;  close out;
run;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Contents of storage library = WORK.IMLSTOR

Matrices:
EWMA                             MA
WMA

Modules:




&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;43508  proc iml;
NOTE: IML Ready
43509  show storage;
NOTE: Opening storage library WORK.IMLSTOR
43510  load MA WMA;
43511  use Sashelp.Air;
43512     read all var "date" into t;
43513     read all var "air" into y;
43514  close;
NOTE: Closing SASHELP.AIR
43515  MA   = MA(y, 5);
ERROR: Invocation of unresolved module MA.

 statement : ASSIGN at line 43515 column 1
43515!                            /* moving average, k=5 */
43516  WMA  = WMA(y, 1:5);
ERROR: Invocation of unresolved module WMA.

 statement : ASSIGN at line 43516 column 1
43516!                            /* weighted moving average */
43517
43518  create _air_out_x var{t y MA WMA};
43518!                                     append;
43518!                                              close out;
NOTE: Cannot close WORK.OUT; it is not open.
43519  run;
NOTE: Module MAIN is undefined in IML; cannot be RUN.
43519!     quit;
NOTE: Exiting IML.
NOTE: The data set WORK._AIR_OUT_X has 144 observations and 4 variables.
NOTE: Storage library WORK.IMLSTOR closed.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE IML used (Total process time):
      real time           0.04 seconds
      cpu time
&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Sun, 11 Sep 2022 00:06:28 GMT</pubDate>
    <dc:creator>hellohere</dc:creator>
    <dc:date>2022-09-11T00:06:28Z</dc:date>
    <item>
      <title>How to save out constant value and own functions withing PROC IML?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832624#M329115</link>
      <description>&lt;P&gt;Inside PROC IML, constants like pi need be self-defined(or am I right? at least tried, complains).&lt;/P&gt;
&lt;P&gt;Also like the need to save/store MACROs, the self-defined functions inside PROC IML prefer to be saved/stored&lt;/P&gt;
&lt;P&gt;in many occasions. How to do so? Thanks,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	pi=3.1415916;	/*need self-defined*/
	indtemp=1:100;
	ind=indtemp/100*pi;
run;quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sat, 10 Sep 2022 12:58:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832624#M329115</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2022-09-10T12:58:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to save out constant value and own functions withing PROC IML?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832627#M329117</link>
      <description>&lt;P&gt;You can use the constant function, as in:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
    pi=constant('pi');
	indtemp=1:100;
    ind=indtemp/100*pi;
run;quit;   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can also avoid naming the scalar pi, as in:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
	indtemp=1:100;
    ind=indtemp/100*constant('pi');
run;quit;   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Sep 2022 13:06:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832627#M329117</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-09-10T13:06:21Z</dc:date>
    </item>
    <item>
      <title>Re: How to save out constant value and own functions withing PROC IML?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832631#M329119</link>
      <description>&lt;P&gt;Thanks. This works.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, is there any doc/weblink on list of constant? And how about to save/store function for IML?&lt;/P&gt;</description>
      <pubDate>Sat, 10 Sep 2022 13:16:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832631#M329119</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2022-09-10T13:16:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to save out constant value and own functions withing PROC IML?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832638#M329124</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/409584"&gt;@hellohere&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;BTW, is there any doc/weblink on list of constant? And how about to save/store function for IML?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Google can find the docs.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Sep 2022 14:06:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832638#M329124</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2022-09-10T14:06:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to save out constant value and own functions withing PROC IML?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832658#M329133</link>
      <description>&lt;P&gt;Take a look at the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/v_030/imlug/titlepage.htm" target="_self"&gt;SAS/IML User's Guide&lt;/A&gt;&amp;nbsp;which has a section on Packages, which is the functionality you are probably looking for.&lt;/P&gt;</description>
      <pubDate>Sat, 10 Sep 2022 16:37:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832658#M329133</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2022-09-10T16:37:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to save out constant value and own functions withing PROC IML?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832703#M329146</link>
      <description>&lt;P&gt;The DOC helps huge. Still what is diff. between Function and Module?!&lt;/P&gt;
&lt;P&gt;I tried and saved out the three Functions, and can see it in IMLSTOR.&lt;/P&gt;
&lt;P&gt;When show-storage, log shows load of three matrices.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When invoke, it complains... Any help?!&lt;/P&gt;
&lt;P&gt;Awkward enough, the dataset is created with expected values.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&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 out;
store MA WMA EWMA;			/* store function*/
run;quit;

proc iml;
show storage;
load MA WMA;
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 */ 
 
create _air_out_x var{t y MA WMA};  append;  close out;
run;quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Contents of storage library = WORK.IMLSTOR

Matrices:
EWMA                             MA
WMA

Modules:




&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;43508  proc iml;
NOTE: IML Ready
43509  show storage;
NOTE: Opening storage library WORK.IMLSTOR
43510  load MA WMA;
43511  use Sashelp.Air;
43512     read all var "date" into t;
43513     read all var "air" into y;
43514  close;
NOTE: Closing SASHELP.AIR
43515  MA   = MA(y, 5);
ERROR: Invocation of unresolved module MA.

 statement : ASSIGN at line 43515 column 1
43515!                            /* moving average, k=5 */
43516  WMA  = WMA(y, 1:5);
ERROR: Invocation of unresolved module WMA.

 statement : ASSIGN at line 43516 column 1
43516!                            /* weighted moving average */
43517
43518  create _air_out_x var{t y MA WMA};
43518!                                     append;
43518!                                              close out;
NOTE: Cannot close WORK.OUT; it is not open.
43519  run;
NOTE: Module MAIN is undefined in IML; cannot be RUN.
43519!     quit;
NOTE: Exiting IML.
NOTE: The data set WORK._AIR_OUT_X has 144 observations and 4 variables.
NOTE: Storage library WORK.IMLSTOR closed.
NOTE: The SAS System stopped processing this step because of errors.
NOTE: PROCEDURE IML used (Total process time):
      real time           0.04 seconds
      cpu time
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 11 Sep 2022 00:06:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-save-out-constant-value-and-own-functions-withing-PROC/m-p/832703#M329146</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2022-09-11T00:06:28Z</dc:date>
    </item>
  </channel>
</rss>

