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.
|
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.
|
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. |
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. |
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. |
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. |