TSMODEL Procedure
Example 29.2 User-Defined Program Statements
The following SAS macro statements create a user-defined subroutine and a user-defined function. Mylog is a subroutine that log-transforms a time series. Mymean is a function that computes the mean of a time series.
%macro fcmpcode;
subroutine mylog(actual[*], transform[*]);
outargs transform;
actlen = DIM(actual);
do t = 1 to actlen;
transform[t] = log(actual[t]);
end;
endsub;
function mymean(z[*]);
nz = DIM(z);
sum = 0;
nnmiss = 0;
do t = 1 to nz;
if z[t] ne . then do;
sum = sum + z[t];
nnmiss = nnmiss + 1;
end;
end;
if nnmiss eq 0 then return(.);
return(sum/nnmiss);
endsub;
%mend;
The following DATA step loads data set Sashelp.Air, which contains airline data into the table mylib.air. These statements assume that your libref is named mylib, but you can substitute any appropriately defined libref.
data mylib.air(replace=yes);
set sashelp.air;
run;
The input table mylib.air contains the variables Air and Date. The time series is recorded monthly.
The following statements form quarterly time series from the monthly series based on the median value of the total of the transactions recorded within each month. The OUTARRAYS statement specifies the Logair and Logairc arrays as output. The OUTSCALARS statement specifies the Meanlog scalars as output. The other arrays and scalars are not part of the output. The subsequent program statements create the output arrays and scalars.
proc tsmodel data=mylib.air
outarray=mylib.arrays(replace=yes)
outscalar=mylib.scalars(replace=yes)
outsum=mylib.airsum(replace=yes);
id date interval=qtr acc=total;
var air;
outarrays logair logairc;
outscalars meanlog;
submit;
%fcmpcode;
call mylog(air,logair);
meanlog = mymean(air);
do t = 1 to dim(air);
logairc[t] = logair[t] - meanlog;
end;
endsubmit;
quit;
The following PROC PRINT step displays the OUTSUM= table, as shown in Output 29.2.1.
proc print data=mylib.airsum; run;
Output 29.2.1: Summary Statistics for mylib.Air
| Obs | _NAME_ | NOBS | N | NMISS | MIN | MAX | MEAN | STDDEV | SUM | _STATUS_ | START | END | STARTOBS | ENDOBS |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | AIR | 48 | 48 | 0 | 341 | 1736 | 840.89583333 | 356.43144067 | 40363 | 0 | 1949:1 | 1960:4 | 1 | 48 |
| 2 | logair | 48 | 48 | 0 | 5.8318824773 | 7.4593388952 | 6.6430769906 | 0.4395319391 | 318.86769555 | 0 | 1949:1 | 1960:4 | 1 | 48 |
| 3 | logairc | 48 | 48 | 0 | -835.0639509 | -833.4364944 | -834.2527563 | 0.4395319394 | -40044.1323 | 0 | 1949:1 | 1960:4 | 1 | 48 |