Creating Functions with PROC FCMP

A Brief Overview

Within SAS, you can create your own functions. Creating your own functions is useful when you have repetitive routines in your programs. The SAS Function Compiler (FCMP) procedure enables you to create custom functions using DATA step syntax. This feature enables programmers to more easily read, write, and maintain complex code with independent and reusable routines.

PROC FCMP Syntax

Use PROC FCMP to build user-defined functions and call routines with DATA step syntax.
PROC FCMP Syntax
Syntax, PROC FCMP step:
PROC FCMP OUTLIB=libref.table.package;
FUNCTION function-name(arguments)<$> <length>;
. . . programming statements. . .
RETURN(expression);
ENDSUB;
QUIT;
OUTLIB=libref.table.package
specifies the table and package that stores the compiled function. This is a required argument that must be specified in the PROC FCMP statement.
FUNCTION statement
The FUNCTION statement specifies the function-name and the function arguments, as well as whether the function returns a character or numeric value.
function-name(arguments)<$> <length>
specifies a subroutine declaration for a routine that returns a value. The FUNCTION statement begins the definition of a function.
<$>
specifies that the function returns a character value. If $ is not specified, the function returns a numeric value.
argument
specifies one or more arguments for the function. You specify character arguments by placing a dollar sign ($) after the argument name.
function-name
specifies the name of the function.
<length>
specifies the length of a character value.
RETURN(expression)
specifies the value that is returned by the function.
ENDSUB
ends the function.

CMPLIB= Option Syntax

In order for SAS to find the custom function, the CMPLIB= option must be specified in the OPTIONS statement. This option names the table where the function is stored within a package. The CMPLIB option is a global option. Once you set this option it remains in effect until you cancel, change, or exit your SAS session.
CMPLIB= system option syntax
OPTIONS CMPLIB=libref.table | (libref.table-1...libref.table-n)
libref.table
specifies the table or tables that SAS searches for a package that contains the desired function.

Example: Creating a Custom Character Function with One Argument

Suppose you need to create a new data set with the names of baseball players and their teams. The names of the baseball players in the Certadv.Baseball data set is in the wrong order. The Certadv.Baseball data set currently lists the players by last name followed by first name, and names are separated by commas. You need to create a custom function to reverse the order of the name so that the players’ first names are followed by their last names, separated by a space.
proc fcmp outlib=certadv.functions.dev;      /*1*/
   function ReverseName(lastfirst $) $ 40;   /*2*/
     First=scan(lastfirst,2,',');            /*3*/
     Last=scan(lastfirst,1,',');
   return(catx(' ',First,Last));             /*4*/
   endsub;                                   /*5*/
run;
1 The FCMP procedure enables you to create a custom function. The OUTLIB= option specifies Certadv.Functions as the table in which the Dev package is stored. The Dev package is a collection of routines that have unique names.
2 The FUNCTION statement specifies the function name as ReverseName. ReverseName is a custom function that has one character argument named LastFirst and it returns a character value with a length of 40. If a return value type and length are not specified, the default is numeric with a length of 8.
3 The body of the function consists of DATA step syntax. The assignment statement creates two new variables, First and Last, that are created using the SCAN function. The new variable First uses the SCAN function to return the second word from the LastFirst variable and the Last variable returns the first word from the LastFirst variable.
4 The RETURN statement specifies the value of ReverseName. The RETURN statement defines the value returned by the function. It uses the CATX function to concatenate the first and last variable values created within the function definition separated by a space.
5 The ENDSUB statement ends the function.
Now that you have created your custom function, you can use it in a DATA step just like any other SAS function.
options cmplib=certadv.functions;   /*1*/
data work.baseball;                 /*2*/
   set certadv.baseball;
   Player=reversename(Name);        /*3*/
   keep Name Team Player; 
run;
proc print data=work.baseball;
run;
1 The CMPLIB= option specifies the Certadv.Functions table for SAS to search for a package that contains the desired function.
2 The DATA step reads Certadv.Baseball to create Work.Baseball.
3 The DATA step creates a new variable named Player by referencing the custom function, ReverseName. The function reverses the order of FirstName and LastName values within Name and returns the string to the Player variable.
Output 13.3 PROC PRINT Result of Work.Baseball (partial output)
Partial Output of Work.Baseball

Example: Creating a Custom Numeric Function with One Argument

Suppose you need to create a custom function to generate fiscal quarter buckets based on the value of Dates in your data set. The following example uses one numeric argument and returns a numeric value for the function.
proc fcmp outlib=certadv.functions.dat;            /*1*/
   function MyQuarter(month);                      /*2*/
      if month in(2,3,4) then myqtr=1;             /*3*/
         else if month in(5,6,7) then myqtr=2;
         else if month in (8,9,10) then myqtr=3;
         else myqtr=4;
      return(myqtr);                               /*4*/
   endsub;                                         /*5*/
run;

options cmplib=certadv.functions;                  /*6*/
data work.dates;                                   /*7*/
   do Dates='15JAN2019'd to '31DEC2019'd by 30;
      MonNum=month(Dates);
      FiscalQuarter=MyQuarter(MonNum);             /*8*/
      output;
   end;
run;

proc print data=work.dates;                        /*9*/
   format Dates mmddyy10.;
run;
1 The FCMP procedure enables you to create a custom function. The OUTLIB= option specifies Certadv.Functions as the table in which the Dat package is stored. The Dat package is a collection of routines that have unique names.
2 The FUNCTION statement specifies a new function named MyQuarter that accepts one numeric argument named Month.
3 The body of the function consists of DATA step IF-THEN/ELSE syntax. This conditional logic defines the value to assign to MyQtr based on the value of the input argument Month.
4 The RETURN statement is used to return a value to the MyQtr function.
5 The ENDSUB statement ends the function.
6 The CMPLIB= option specifies the Certadv.Functions table for SAS to search for a package that contains the desired function.
7 The DATA step creates a temporary data set named Work.Dates. The DATA step uses the DO loop to create a Dates column with values from January 15 2019 to December 31 2019.
The assignment statement creates a new variable MonNum that uses the month function. SAS supplies the MONTH function to generate a value using the Dates variable.
8 The assignment statement creates the FiscalQuarter variable using the custom function MyQuarter and the value of MonNum as the argument.
9 The PRINT procedure prints the Work.Dates data set. The FORMAT statement, applied to the Dates column, displays the dates as mmddyy10.
Output 13.4 PROC PRINT Output of Work.Dates
PROC PRINT Output of Work.Dates

Example: Creating a Custom Character Function with Multiple Arguments

Thus far, the examples in this chapter have focused on creating functions that contained one argument. You can specify multiple arguments for custom functions by separating the arguments with a comma (,). If the argument is a character value, then a dollar sign ($) is placed after the argument before the comma.
The following example creates a character function called ReverseName with two character arguments, LastFirst and Pos.
proc fcmp outlib=certadv.functions.dev;              /*1*/
   function ReverseName(lastfirst $, pos $) $ 40;    /*2*/
      First=scan(lastfirst,2,',');                   /*3*/
      Last=scan(lastfirst,1,',');
      if substr(pos,2,1)='F' then 
         return(catx(' ','Outfielder',First,Last));
      else if substr(pos,2,1)='B' then 
         return(catx(' ','Baseman',First,Last));
      else return(catx(' ',pos,First,Last));
   endsub;                                           /*4*/
quit;

options cmplib=work.functions;                       /*5*/

data work.baseball;                                  /*6*/
   set certadv.baseball;
   Player=reversename(Name,Position);                /*7*/
   keep Name Team Position Player;
run;
proc print data=baseball;
run;
1 The FCMP procedure enables you to create a custom function. The OUTLIB= option specifies Certadv.Functions as the table in which the Dev package is stored. The Dev package is a collection of routines that have unique names.
2 The FUNCTION statement specifies the function name as ReverseName. ReverseName has two character arguments named LastFirst and Pos, and it returns character values with the length of 40. If a return value type and length are not specified, the default is numeric with a length of 8.
3 The body of the function consists of DATA step syntax. The assignment statement creates two new variables First and Last using the SCAN function. The new variable First uses the SCAN function to return the second word from the LastFirst variable value and the Last variable returns the first word from the LastFirst variable value.
The IF-THEN/ELSE statements use the SUBSTR function to extract the second character of the Pos value. Based on the value returned by the SUBSTR function, the appropriate concatenation is made using CATX. For example, in the fourth observation the second character in the Pos value is F so the ReverseName function returns Outfielder Andre Dawson.
If none of the criteria for the IF-THEN statements are met, then the ELSE statement returns by the value of Pos and the reversed order of the name.
4 The ENDSUB statement ends the function.
5 The CMPLIB= option specifies the Certadv.Functions table for SAS to search for a package that contains the desired function.
6 The DATA step creates a temporary data set named Work.Baseball by reading Certadv.Baseball.
7 The DATA step creates a new variable named Player and references the custom function, ReverseName. The function reverses the order of Name concatenated with the value of position and returns the string to the Player variable.
Output 13.5 PROC PRINT Result of Work.Baseball (partial output)
Partial Output: PROC PRINT Result of Work.Baseball
Last updated: October 16, 2019
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset