Inside PROC IML, constants like pi need be self-defined(or am I right? at least tried, complains).
Also like the need to save/store MACROs, the self-defined functions inside PROC IML prefer to be saved/stored
in many occasions. How to do so? Thanks,
proc iml;
pi=3.1415916; /*need self-defined*/
indtemp=1:100;
ind=indtemp/100*pi;
run;quit;
You can use the constant function, as in:
proc iml;
pi=constant('pi');
indtemp=1:100;
ind=indtemp/100*pi;
run;quit;
You can also avoid naming the scalar pi, as in:
proc iml;
indtemp=1:100;
ind=indtemp/100*constant('pi');
run;quit;
Thanks. This works.
BTW, is there any doc/weblink on list of constant? And how about to save/store function for IML?
@hellohere wrote:
BTW, is there any doc/weblink on list of constant? And how about to save/store function for IML?
Google can find the docs.
Take a look at the SAS/IML User's Guide which has a section on Packages, which is the functionality you are probably looking for.
The DOC helps huge. Still what is diff. between Function and Module?!
I tried and saved out the three Functions, and can see it in IMLSTOR.
When show-storage, log shows load of three matrices.
When invoke, it complains... Any help?!
Awkward enough, the dataset is created with expected values.
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 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;
Contents of storage library = WORK.IMLSTOR
Matrices:
EWMA MA
WMA
Modules:
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
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.