This Sample describes a way to use SAS to call a C function that has an array parameter that is multidimensional. The key point is that SAS stores an array as a one-dimensional array (that is, as a 1-by-m array), regardless of the array dimensions. SAS handles the subscripting to accommodate a multidimensional array, but the underlying array storage is a single contiguous array of doubles.
Therefore, when you pass a multidimensional array from SAS to a C function, note that SAS actually passes the array as a single-dimensional array. If the C function expects the array in multiple dimensions, then the C code must be modified to convert the array into the expected dimensions. For example, modify the C function directly or use a C wrapper function.
The following steps are described:
The following C code defines a simple C function called setarray that accepts an array as input. It assigns values to the elements of the array and then returns the populated array:
Here are Linux commands to compile the above C code, which is also stored on the Downloads tab as setarray.c:
The following PROTO step links SAS to the shared library that contains the compiled C function. It declares the prototype for the C function and stores the function declaration in a SAS library:
The following FCMP step defines a SAS function that calls the C function. It prints the array to the SAS log, both before and after calling the C function. It also outputs the populated array to a SAS data set:
When you successfully run the above SAS program, it outputs the following to the SAS log:
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.
Before running the SAS code below, you must:
proc proto package=work.proto_ds.cfcns;
link "/tmp/setarray.so";
void setarray(double** A, int n, int m);
run;
proc fcmp outlib=work.proto_ds.cfcns inlib=work.proto_ds;
subroutine sas_setarray(A[*,*]) varargs;
outargs A;
n=dim(A,1);
m=dim(A,2);
put n= m=;
put ' ';
put 'BEFORE call to setarray function';
put A=;
call setarray(A, n, m);
put ' ';
put 'AFTER call to setarray function';
put A=;
rc=write_array('work.a',A);
put ' ';
endsub;
run;
options cmplib=work.proto_ds;
data _null_;
array A[1:4,1:3] _temporary_;
call sas_setarray(A);
run;
proc print data=a;
run;
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: | Third Party ==> Programming Common Programming Tasks ==> Iterative Processing Analytics ==> Matrix Programming Data Management ==> Manipulation and Transformation ==> Array processing Data Management ==> Manipulation and Transformation ==> Combining and Modifying Data Sets SAS Reference ==> Procedures ==> FCMP Common Programming Tasks ==> Reading and Writing SAS Data |
Date Modified: | 2013-10-16 04:27:09 |
Date Created: | 2013-10-02 05:53:01 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.2 TS1M0 | |
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.2 TS1M0 | |||
Microsoft Windows XP 64-bit Edition | 9.2 TS1M0 | |||
Microsoft® Windows® for x64 | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.2 TS1M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.2 TS1M0 | |||
Microsoft Windows XP Professional | 9.2 TS1M0 | |||
Windows Vista | 9.2 TS1M0 | |||
Windows Vista for x64 | 9.2 TS1M0 | |||
64-bit Enabled AIX | 9.2 TS1M0 | |||
64-bit Enabled HP-UX | 9.2 TS1M0 | |||
64-bit Enabled Solaris | 9.2 TS1M0 | |||
HP-UX IPF | 9.2 TS1M0 | |||
Linux | 9.2 TS1M0 | |||
Linux for x64 | 9.2 TS1M0 | |||
OpenVMS on HP Integrity | 9.2 TS1M0 | |||
Solaris for x64 | 9.2 TS1M0 |