Have you ever wondered, Why SAS never had functions similar to any other programming Language i.e. C, C++, Java…
LINK/ Return statement and SAS Macros to some extent served the purpose in modularizing and developing re-usable code for repeating tasks…but doesn’nt make it is easy to follow….Macros are easier to reuse; however, they are not independent from the main program, they involve non-DATA step syntax, and substantial use of macros can result in illegible code.
SAS finally broke this jinx with FCMP procedure in version 9.1 and above.
In SAS®9.1, the FCMP procedure provides the ability to write functions to be used in conjunction with few SAS Procedures….
Starting with SAS® 9.2, these user-written functions can be called from a DATA step as well.PROC FCMP provides the ability to write true functions and CALL routines in DATA step syntax that are stored in a data set. In SAS 9.2, FCMP routines can be called from the DATA step like any other SAS function. This enables programmers to more easily read, write, and maintain complex code with independent and reusable subroutines.
Here are some examples of How to Use them…
NOTE: These Examples below work with SAS v9.2 ONLY.
Using Numeric Data in the FUNCTION Statement
The following example uses numeric data as input to the FUNCTION statement of PROC FCMP:
Using Numeric Data in the FUNCTION Statement
The following example uses numeric data as input to the FUNCTION statement of PROC FCMP:
Using Character Data in the FUNCTION Statement
The following example uses character data as input to the FUNCTION statement of PROC FCMP. The output from FUNCTION test is assigned a length of 12 bytes.
proc fcmp outlib=work.funcs.math; |
Using Variable Arguments with an Array
The following example shows an array that accepts variable arguments. The example implies that the summation function can be called as follows: sum = summation(1, 2, 3, 4, 5); .
proc fcmp outlib=work.funcs.math; |
function summation (b[*]) varargs; |
array x(*) x1-x3 {10,20,30}; |
Directory Traversal(v 9.2 ONLY)
Refer to this Paper for Detailed Explaination of a sample code
Example Codes