FCMP Procedure

Example 1: Creating a Function and Calling the Function from a DATA Step

Features:

PROC FCMP statement option: OUTLIB=

DATA step

This example shows how to compute a study day during a drug trial by creating a function in FCMP and using that function in a DATA step.

Program

proc fcmp outlib=sasuser.funcs.trial;
   function study_day(intervention_date, event_date);
      n = event_date - intervention_date;
         if n >= 0 then
            n = n + 1;
         return (n);
   endsub;
options cmplib=sasuser.funcs;
data _null_;
   start = '15Feb2010'd;
   today = '27Mar2010'd;
   sd = study_day(start, today);
   put sd=;
run;

Program Description

Specify the name of an output package to which the compiled function and CALL routine are written.The package is stored in the data set Sasuser.Funcs.
proc fcmp outlib=sasuser.funcs.trial;
Create a function called STUDY_DAY.STUDY_DAY is created in a package called Trial, and contains two numeric input arguments.
   function study_day(intervention_date, event_date);
Use a DATA step IF statement to calculate EVENT-DATE.Use DATA step syntax to compute the difference between EVENT_DATE and INTERVENTION_DATE. The days before INTERVENTION_DATE begin at -1 and become smaller. The days after and including INTERVENTION_DATE begin at 1 and become larger. (This function never returns 0 for a study date.)
      n = event_date - intervention_date;
         if n >= 0 then
            n = n + 1;
         return (n);
   endsub;
Use the CMPLIB= system option to specify a SAS data set that contains the compiler subroutine to include during program compilation.
options cmplib=sasuser.funcs;
Create a DATA step to produce a value for the function STUDY_DAY.The function uses a start date and today's date to compute the value. STUDY_DAY is called from the DATA step. When the DATA step encounters a call to STUDY_DAY, it does not find this function in its traditional library of functions. It searches each of the data sets that are specified in the CMPLIB system option for a package that contains STUDY_DAY. In this case, it finds STUDY_DAY in sasuser.funcs.trial.
data _null_;
   start = '15Feb2010'd;
   today = '27Mar2010'd;
   sd = study_day(start, today);
Write the output to the SAS log.
   put sd=;
Execute the SAS program.
run;

Output: Log

Output from Creating a Function and Calling the Function from a DATA Step
sd=41