The SAS FCMP Procedure lets us write custom functions and subroutines. In this post, I will investigate the difference between a subroutine and a function in PROC FCMP by example. I will demonstrate simple examples of how to define them, store them and use them in data step logic. I you have not, read the blog post How Are PROC FCMP Functions Stored in SAS?. This will make the syntax to come clear.

A Subroutine

The PROC FCMP Documentation defines a subroutine as an independent computational block of code that you can call using a CALL statement. That is why is is commonly referred to as a Call Routine. We define a subroutine with the Subroutine Statement. In it, we specify the name of the routine. In this case s. Finally, we specify the arguments to the routine in parenthesis. In the code below, I specify four arguments. In1, in2, out1 and out2.

Next, I use the Outargs Statement to specify the variables that we want the subroutine to update. The Outargs Statement is very important to understand the creation of call routines in PROC FCMP. Finally, I specify how SAS should update the two variables in the Outargs Statement. In this case, I create a simple sum for out1 and a simple product for out2.

In the following dataset, you can see how I use the routine above in a Call Statement in a data set.

proc fcmp outlib=work.f.f;
   subroutine s (in1, in2, out1, out2);
      outargs out1, out2;
      out1=in1 + in2;
      out2=in1 * in2;
   endsub;
run;quit;
 
options cmplib=work.f;
data _null_;
   call s1(2, 3, sum, prod);
   put sum= / prod=;
run;

A Function

A function is a special case of a Subroutine. A SAS function returns a value. And only one value. Furthermore, a function is not used in a Call Statement. Instead we use it in an Assignment Statement. Not surprisingly, we create a custom SAS function in PROC FCMP with the Function Statement. In it, we specify the name of the function and the arguments for the function. In the code below, I create a function f with the two arguments in1 and in2. Next, I use an assignment statement to create out as the sum of the two input values. Finally, I use the Return Statement to specify that out is the return value.

Below, I use the function in a simple SAS data step. Notice that I use a simple assignment. Not a Call Statement as above.

proc fcmp outlib=work.f.f;
   function f(in1, in2);
      out=in1 + in2;
      return (out);
   endsub;
run;quit;
 
options cmplib=work.f;
data _null_;
   s = f(1, 2);
   put s=;
run;

Summary

In this post, I have investigated the difference between a subroutine and a function in PROC FCMP. We learned that a function is a special case of a routine. Also, the subroutine is used in a Call Statement and the function in an Assignment Statement. My rule of thumb is this: If I only need one return value, I write a function. If I need multiple, I write a subroutine. Naturally, there are exceptions to the rule.

In previous posts, I write about Data Step Arrays Vs PROC FCMP Arrays and How to Use the Hash Object in PROC FMCP.

You can download the entire code form this post here.