NOTE: Writing Your Own Functions

This post was kindly contributed by NOTE: The blog of RTSL.eu - Development with SAS® - go there to comment and to read the full post.

Did you ever hear of PROC JLAUNCH? No, neither did I until I tried the FCmp Function Editor. And, if you didn’t know, the FCmp Function Editor is an interactive tool for exploring and maintaining functions created with PROC FCMP. There are a lot of good reasons for creating your own function. Read on…

I confess I dismissed the SAS Function Compiler (PROC FCMP) as nothing more than an alternative to my old, familiar macro language. A colleague of mine recently pointed out that although FCMP may be similar to macro in the functionality that it provides, it provides the functionality in a far more elegant and packaged manner. FCMP avoids the complexities of SAS macro language such as percentage signs, quoting, and the fact that all values and variables are character. FCMP offers the novice-intermediate SAS programmer an easy entry into structured programming.

You can create functions, CALL routines, and subroutines in FCMP; and those items can then be used in DATA steps, WHERE clauses, ODS statements, and selected PROCs. The details of using FCMP are in the Base SAS Procedures Guide, but I’ll step you through some basics here.

Firstly, you should understand that your functions will be stored in a data set. When you define a function you specify a library, data set, and a package in which to store the function. Data sets can store more than one package, and packages can store more than one function. The following log illustrates some further basics (with explanation following the log).

15 options cmplib=work.funclib;
16
17 proc fcmp OUTLIB=work.funclib.NOTEPKG;
18   function HERBERT();
19     x=41;
20     y=42;
21     z=44;
22     return (43);
23   endsub;
24 run;
NOTE: Function HERBERT saved to work.funclib.NOTEPKG.
25
26 quit;
27
28 data lotus;
29   y=40;
30   x=herbert();
31   put _all_;
32 run;
y=40 x=43 _ERROR_=0 _N_=1

The simple example above shows a) use of the CMPLIB option to specify a search path for bespoke functions, b) variables used within the function definition are local to the definition and do not interfere with variables used in the code that called the function, and c) return values are specified with the RETURN statement.

The DATA step I/O statements (for example, INPUT, FILE, SET, and MERGE) are not available in PROC FCMP. However, some functionality of the PUT statement is supported.

I recommend that you adopt a naming standard for your bespoke functions in order to distinguish them from in-built SAS functions. This will avert much gnashing of teeth by hapless maintenance programmers later in the life of your code.

And finally, back to JLAUNCH. The FCmp Function Editor is a handy tool for helping you to create and maintain your functions. You can access it from the good, old-fashioned Display Manager interface, i.e. (on Windows) Start –> All Programs –> SAS –> SAS 9.2 (English). It’ll probably prompt you to login to your metadata server, then you’ll get a nice, windowed Java application to play with (hence the use of JLAUNCH).

Once inside the FCmp Function Editor, you can right-click a library, choose New Function, and then use a wizard-like interface to specify the attributes of your function, and its code. FCmp Function Editor will generate the SAS code for you (except the CMPLIB options statement) and it will check the syntax for you (you need to click View –> Show Log to see your errors). Upon clicking OK, it’ll create and store the function for you. You can update the function at any time, or retrieve the full PROC FCMP code by double-clicking the function from within FCmp Function Editor.

All-in-all I’m seeing FCMP in a new light and I’m sure it’ll feature in my future work. However, I will confess to being too sweeping in my earlier comparison of SAS macro language with FCMP. If you want to do stuff at the data set or PROC level you’ll still need to learn macro!

This post was kindly contributed by NOTE: The blog of RTSL.eu - Development with SAS® - go there to comment and to read the full post.