The TIMEDATA Procedure

Example 30.2 Using User-Defined Functions and Subroutines

This example uses the TIMEDATA procedure with a user-defined function and subroutine created by the FCMP procedure.

The following statements use the FCMP procedure to 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 compute the mean of a time series. The subroutine and function definitions are stored in the data set Work.Timefnc. The OPTIONS statement loads the subroutine and function definitions.


proc fcmp outlib=work.timefnc.funcs;

   subroutine mylog(actual[*], transform[*]);
   outargs transform;
   actlen  = DIM(actual);
   do t = 1 to actlen;
      transform[t] = log(actual[t]);
   end;
   endsub;

   function mymean(actual[*]);
   actlen  = DIM(actual);
   sum = 0;
   do t = 1 to actlen;
      sum = sum + actual[t];
   end;
   return( sum / actlen );
   endsub;

run;
quit;

options cmplib = work.timefnc;

The input data set Sashelp.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 (ACCUMULATE=TOTAL) of the transactions recorded with each time period and assign the SAS time format (FORMAT=YYMMDD.). The OUTARRAYS statement specifies the Logair and Myair arrays as output. The OUTSCALARS statement specifies the Mystats scalars as output. The other arrays and scalars are not part of the output. The subsequent programming statements create the output arrays and scalars. The PRINT=(ARRAYS SCALARS) prints the output arrays and scalars.


proc timedata data=sashelp.air out=work.air
              print=(scalars arrays);
              id date interval=qtr acc=t format=yymmdd.;
              vars air;
              outarrays logair myair;
              outscalars mystats;

       call mylog(air,logair);
       do t = 1 to dim(air);
       myair[t] = air[t] - logair[t];
       end;
       mystats= mymean(air);

run;