This sample provides step-by-step instruction and guidance through the process of defining your own function in C, compiling your C code, and then calling your C function from within a SAS DATA step. It is assumed that you are familiar with programming in C and compiling C source code.
The C function can be written in an editor of your choice, such as VI Editor or Microsoft Windows Notepad. This sample contains C source code for a function called myfactorial that computes the factorial of an integer:
In order for SAS to be able to use a C program, the C source code must be compiled into a shared library. Note that shared libraries are platform specific. For instance, a shared library that is compiled in an AIX operating environment is not compatible with a Windows platform. You should consider each of the following factors when you compile C source code:
There are several different C compilers. The configuration steps and command-line syntax that are required to compile your C source code is dependent upon which C compiler you use. For example, you can use the following command-line syntax to compile the myfactorial.c program via the GCC 4.2.0 compiler on an AIX 5.3 64-bit platform:
The resulting myfactorial.so file is a shared library that can then be used by SAS.
As another example, you can use the following configuration steps to compile the myfactorial.c program via Microsoft Visual Studio 2010 in the Windows 7 operating environment:
In addition to exporting function names, you might need to incorporate other modifications into your C source to account for features that vary according to the compiler that you use. For example, some compilers perform name mangling of the functions during the source code compilation. For more specific details on the configuration steps and syntax to use for your compiler, see the user manual for your compiler.
This step is optional, but it is recommended. If you are not able to call your C function directly from C, outside of SAS, then there is a strong likelihood that a call to the function from within SAS also will fail.
The following C test program calls the myfactorial function with an input argument of 6:
Use PROC PROTO to link SAS to the shared library that contains your C function. Sample 32080 provides greater detail for this step and the remaining steps, and the PROTO procedure documentation also contains relevant information. To use a C function in SAS, the corresponding shared library needs to be linked via the LINK statement. The following SAS code links SAS to the shared library and loads the function declarations to the Work.Proto_ds.Cfcns function package:
A successful execution of the SAS code above results in the following SAS log details:
NOTE: 'c:\temp\myfactorial.dll' loaded from specified path. NOTE: Prototypes saved to WORK.PROTO_DS.CFCNS. NOTE: PROCEDURE PROTO used (Total process time): real time 0.00 seconds cpu time 0.00 seconds
A C function that is linked to SAS is only available to specific SAS procedures, such as the FCMP and COMPILE procedures. PROC FCMP is a Base SAS® procedure, and PROC COMPILE is a SAS® Risk Dimensions® procedure. This sample uses PROC FCMP, but the syntax for PROC COMPILE is similar.
When you use PROC FCMP, it is possible both to directly call a C function and to define a function that is available to the DATA step and many SAS procedures. The following SAS code defines a SAS function called sas_myfactorial that contains a call to the myfactorial C function:
NOTE: 'c:\temp\myfactorial.dll' loaded from specified path. NOTE: Function sas_myfactorial saved to work.fcmp_ds.sasfcns. NOTE: PROCEDURE FCMP used (Total process time): real time 0.03 seconds cpu time 0.01 seconds
Use the SAS CMPLIB= system option to specify the storage locations for the C and FCMP functions:
It is now possible to call your function from the DATA step. From the DATA step, you need to call the FCMP function (sas_myfactorial in this sample) instead of the C function. The DATA step recognizes that the sas_myfactorial function is available because it has been saved to a CMPLIB library. The SAS function sas_myfactorial, in turn, calls the myfactorial C function, which was compiled in the myfactorial.dll shared library to which SAS was linked in the PROC PROTO step. In the following SAS code, the argument 6 is passed to the function:
When the argument is passed to the myfactorial C function, the calculated result is returned to the SAS function sas_myfactorial, which is in turn passed back to the DATA step. The flow of the full process is: DATA step → FCMP → PROTO → C → FCMP → DATA step.
Upon successful execution of the SAS code above, the following note is printed to the SAS log:
NOTE: 'c:\temp\myfactorial.dll' loaded from specified path. x=720 NOTE: DATA statement used (Total process time): real time 0.03 seconds cpu time 0.03 seconds
The SAS code from the four previous steps above is stored on the Downloads tab for this sample. That program is named myfactorial.sas.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> FCMP SAS Reference ==> Functions ==> External Routines SAS Reference ==> DATA Step Third Party ==> Programming |
Date Modified: | 2010-09-09 14:07:44 |
Date Created: | 2010-08-11 11:26:47 |
Product Family | Product | Host | Product Release | SAS Release | ||
Starting | Ending | Starting | Ending | |||
SAS System | Base SAS | Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.21 | 9.2 TS2M0 | ||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows XP 64-bit Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft® Windows® for x64 | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Datacenter Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Enterprise Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 Standard Edition | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2003 for x64 | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows Server 2008 for x64 | 9.21 | 9.2 TS2M0 | ||||
Microsoft Windows XP Professional | 9.21 | 9.2 TS2M0 | ||||
Windows Vista | 9.21 | 9.2 TS2M0 | ||||
Windows Vista for x64 | 9.21 | 9.2 TS2M0 | ||||
64-bit Enabled AIX | 9.21 | 9.2 TS2M0 | ||||
64-bit Enabled HP-UX | 9.21 | 9.2 TS2M0 | ||||
64-bit Enabled Solaris | 9.21 | 9.2 TS2M0 | ||||
HP-UX IPF | 9.21 | 9.2 TS2M0 | ||||
Linux | 9.21 | 9.2 TS2M0 | ||||
Linux for x64 | 9.21 | 9.2 TS2M0 | ||||
OpenVMS on HP Integrity | 9.21 | 9.2 TS2M0 | ||||
Solaris for x64 | 9.21 | 9.2 TS2M0 |