Previous Page | Next Page

The FCMP Procedure

Special Functions and CALL Routines: C Helper Functions and CALL Routines


C Helper Functions and CALL Routines

Several helper functions are provided with the package to handle C-language constructs in PROC FCMP. Most C-language constructs must be defined in a package that is created by PROC PROTO before the constructs can be referenced or used by PROC FCMP. The ISNULL function and the SETNULL and STRUCTINDEX CALL routines have been added to extend the SAS language to handle C-language constructs that do not naturally fit into the SAS language.


ISNULL C Helper Function


Overview of the ISNULL C Helper Function

The ISNULL function determines whether a pointer element of a structure is null.


Syntax of the ISNULL C Helper Function

The syntax of the ISNULL function has the following form:

numeric-variable ISNULL (pointer-element);

where

numeric-variable

specifies a numeric value.

pointer-element

specifies a variable that contains the address of another variable.


Example 1: Generating a Linked List

In the following example, the LINKLIST structure and GET_LIST function are defined by using PROC PROTO. The GET_LIST function is an external C routine that generates a linked list with as many elements as requested.

struct linklist{
   double value;
   struct linklist * next;
};

struct linklist * get_list(int);


Example 2: Using the ISNULL C Helper Function in a Loop

The following code segment shows that the ISNULL C helper function loops over the linked list that is created by GET_LIST and writes out the elements.

proc proto package=sasuser.mylib.str2;
struct linklist{
   double value;
   struct linklist * next;
};

struct linklist * get_list(int);

externc get_list;
   struct linklist * get_list(int len){
      int i;
      struct linklist * list=0;
      list=(struct linklist*)
         malloc(len*sizeof(struct linklist));
      for (i=0;i<len-1;i++){
         list[i].value=i;
         list[i].next=&list[i+1];
      }
      list[i].value=i;
      list[i].next=0;
      return list;
   }
externcend;
run;

options pageno=1 nodate ls=80 ps=64;
proc fcmp libname=sasuser.mylib;
   struct linklist list;
   list=get_list(3);
   put list.value=;

   do while (^isnull(list.next));
      list = list.next;
      put list.value=;
   end;
run;

Results from Using the ISNULL C Helper Function

                                 The SAS System                                1

                               The FCMP Procedure

list.value=0
list.value=1
list.value=2

SETNULL C Helper CALL Routine


Overview of the SETNULL C Helper CALL Routine

The SETNULL CALL routine sets a pointer element of a structure to null.


Syntax of the SETNULL C Helper CALL Routine

The syntax of the SETNULL C Helper CALL routine has the following form:

CALL SETNULL(pointer-element);

where pointer-element is a pointer to a structure.


Example: Setting an Element in a Linklist to Null

The following example assumes that the same LINKLIST structure that is described in Example 1: Generating a Linked List is defined using PROC PROTO. The CALL SETNULL routine can be used to set the NEXT element to null:

   struct linklist list;
   call setnull(list.next);


STRUCTINDEX C Helper CALL Routine


Overview of the STRUCTINDEX C Helper CALL Routine

The STRUCTINDEX CALL routine enables you to access each structure element in an array of structures.


Syntax of the STRUCTINDEX C Helper CALL Routine

The syntax of the STRUCTINDEX routine has the following form:

CALL STRUCTINDEX(struct_array, index, struct_element);

where

struct_array

specifies an array.

index

is a 1-based index as used in most SAS arrays.

struct_element

points to an element in the array.


Example: Setting Point Structures in an Array

In the first part of this example, the following structures and function are defined by using PROC PROTO.

proc proto package=sasuser.mylib.str2;
struct point{
   short s;
   int i;
   long l;
   double d;
};

struct point_array {
   int            length;
   struct point p[2];
   char           name[32]; 
};
run;

In the second part of this example, the PROC FCMP code segment shows how to use the STRUCTINDEX CALL routine to retrieve and set each point structure element of an array called P in the POINT_ARRAY structure:

options pageno=1 nodate ls=80 ps=64;

proc fcmp libname=sasuser.mylib;
   struct point_array pntarray;
   struct point pnt;

   pntarray.length = 2;
   pntarray.name = "My funny structure";

   /* Get each element using the STRUCTINDEX CALL routine and set values. */
do i = 1 to 2;
   call structindex(pntarray.p, i, pnt);
   put "Before setting the" i "element: " pnt=;
   pnt.s = 1;
   pnt.i = 2;
   pnt.l = 3;
   pnt.d = 4.5;
   put "After setting the" i "element: " pnt=;
end;
run;

Results of Setting the Point Structure Elements of an Array

                                 The SAS System                                1

                               The FCMP Procedure

Before setting the 1 element:  pnt {s=0, i=0, l=0, d=0}
After setting the 1 element:  pnt {s=1, i=2, l=3, d=4.5}
Before setting the 2 element:  pnt {s=0, i=0, l=0, d=0}
After setting the 2 element:  pnt {s=1, i=2, l=3, d=4.5}

Previous Page | Next Page | Top of Page