The following program
shows the syntax that is used to create and call a PROC FCMP function
from a DATA step. This example computes the study day during a drug
trial.
The example creates
a function named STUDY_DAY in a package named TRIAL. A package is
a collection of routines that have unique names and is stored in the
data set sasuser.funcs. STUDY_DAY accepts two numeric arguments,
intervention_date and
event_date.
The body of the routine uses DATA step syntax to compute the difference
between the two dates, where days that occur before
intervention_date begin
at -1 and become smaller, and days that occur after and including
intervention_date begin
at 1 and become larger. This function never returns 0 for a study
day.
STUDY_DAY is called
from DATA step code as if it were any other function. When the DATA
step encounters a call to STUDY_DAY, it will not find this function
in its traditional library of functions. Instead, SAS searches each
of the libraries or data sets that are specified in the CMPLIB system
option for a package that contains STUDY_DAY. In this example, STUDY_DAY
is located in
sasuser.funcs.trial.
The program calls the function, passing the variable values for
start and
today,
and returns the result in the variable SD.
options pageno=1 nodate;
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 = '15Feb2006'd;
today = '27Mar2006'd;
sd = study_day(start, today);
put sd=;
run;
Output from the STUDY_DAY User-Defined Function
sd=41