This example demonstrates usage of the FCMP procedure. A new function is defined, stored, and tested and then used by another procedure.

In the first section of code, the function is defined:

proc fcmp outlib = sasuser.FCMP_Cat.fcns; function ln(x); y = log(x); return(y); endsub; quit; |

The new function, "LN," is simply the "LOG" function in SAS, which is the natural logarithm. While this new function definition is simple, the ability to define your own function is powerful. You are no longer limited to functions that are predefined within SAS.

The newly-defined "LN" function is then stored in the "fcns" package of the "FCMP_Cat" catalog in the "sasuser" library. Additional details on syntax of the FCMP procedure are available in the FCMP Procedure documentation.

The CMPLIB= system option is then used to let SAS know that the this catalog contains functions that need to be compiled during the program compilation:

options cmplib=(sasuser.FCMP_Cat); |

A test call to the function demonstrates that it is available in the function package and that it performs the computation as expected:

proc fcmp; out = ln(10); put "Testing Function Call"; put "LN(10) returns:" out; quit; |

The output from that test call is displayed on Page 1 of the output listing. See the tab for this result.

Data that follows the model specification

```
y = log(a*x) + z
```

`z`

is a standard normal random variable, `a`

is the population parameter, `x`

is the independent variable, and `y`

is the dependent variable.
data logdata; a = 0.5; do x=1 to 10000; z = rannor(123); y = log(a*x) + z; output; end; run; |

Finally, the parameter `a`

is estimated using the MODEL procedure, based on the data that was just generated:

proc model data = logdata; parameters a; y = ln(a*x); fit y; run; quit; |

Notice that the "LN" function that was defined in the code above is available to and successfully utilized by the MODEL procedure. This simple example demonstrates the power of the FCMP procedure, and the concepts here may be extended to define multiple complex functions to be used by other SAS procedures alongside predefined SAS functions.

The output generated by the MODEL procedure is available on the tab, Pages 2-4. See the tab for a full set of standalone code for this example.

- In SAS 9.1.3, user-defined FCMP functions and subroutines cannot be called from the DATA step (see SAS Note 41809). Functions and subroutines are stored in SAS catalogs. See
*The FCMP Procedure*(SAS 9.1). - In SAS 9.2 and later, user-defined FCMP functions and subroutines can be called from the DATA step. Functions and subroutines are stored in SAS data sets. See
*The FCMP Procedure*(SAS 9.2).

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.

The following code demonstrates creation, storage, testing, and usage of the "LN" function. The "LN" function in this example is simply the natural logarithm. The function gets stored in a function package within a SAS catalog in the "sasuser" library. A test call is made to this function from PROC FCMP. The function is then used by the MODEL Procedure.

/* Create a SAS function called "LN" in FCMP and store it in a SAS catalog, where */
/* outlib=library.catalog.package specifies where the SAS function will be stored */
proc fcmp outlib = sasuser.FCMP_Cat.fcns;
/* Example of a SAS function definition for the natural log */
function ln(x);
y = log(x);
return(y);
endsub;
quit;
/* Let SAS know about the "FCMP_Cat" catalog */
options cmplib=(sasuser.FCMP_Cat);
/* Test a call of the LN function using PROC FCMP */
proc fcmp;
/* Example of calling SAS function "LN" */
out = ln(10);
/* Print result to output listing */
put "Testing Function Call";
put "LN(10) returns:" out;
quit;
/* Generate synthetic data to be used in PROC MODEL */
data logdata;
a = 0.5;
do x=1 to 10000;
z = rannor(123);
y = log(a*x) + z;
output;
end;
run;
/* Utilize the "LN" function to estimate the parameter "a" with PROC MODEL */
proc model data = logdata;
parameters a;
y = ln(a*x);
fit y;
run;
quit;

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.

Page 1The SAS System 16:03 Thursday, April 12, 2007 The CMP Procedure Testing Function Call LN(10) returns: 2.302585093

Page 2The SAS System 16:03 Thursday, April 12, 2007 The MODEL Procedure Model Summary Model Variables 1 Parameters 1 Equations 1 Number of Statements 1 Model Variables y Parameters a Equations y The Equation to Estimate is y = F(a) NOTE: At OLS Iteration 8 CONVERGE=0.001 Criteria Met.

Page 3The SAS System 16:03 Thursday, April 12, 2007 The MODEL Procedure OLS Estimation Summary Data Set Options DATA= LOGDATA Minimization Summary Parameters Estimated 1 Method Gauss Iterations 8 Final Convergence Criteria R 0.000011 PPC(a) 0.000011 RPC(a) 0.003641 Object 0.000013 Trace(S) 0.999872 Objective Value 0.999772 Observations Processed Read 10000 Solved 10000

Page 4The SAS System 16:03 Thursday, April 12, 2007 The MODEL Procedure Nonlinear OLS Summary of Residual Errors DF DF Adj Equation Model Error SSE MSE Root MSE R-Square R-Sq y 1 9999 9997.7 0.9999 0.9999 0.5071 0.5071 Nonlinear OLS Parameter Estimates Approx Approx Parameter Estimate Std Err t Value Pr > |t| a 0.489533 0.00490 100.01 <.0001 Number of Observations Statistics for System Used 10000 Objective 0.9998 Missing 0 Objective*N 9998

The SAS Function Compiler (FCMP) Procedure allows you to create, test, and store SAS functions and subroutines for use by other SAS procedures. This example demonstrates each of these capabilities in conjunction with the MODEL Procedure.

Type: | Sample |

Topic: | Analytics ==> Time Series Analysis Analytics ==> Simulation Analytics ==> Regression Analytics ==> Forecasting Analytics ==> Econometrics SAS Reference ==> Procedures ==> MODEL SAS Reference ==> Procedures ==> FCMP |

Date Modified: | 2012-04-24 02:03:51 |

Date Created: | 2007-04-14 03:03:17 |

Product Family | Product | Host | SAS Release | |

Starting | Ending | |||

SAS System | SAS/OR | All | 9.1 TS1M3 | n/a |

SAS System | Base SAS | All | 9.1 TS1M3 | n/a |

SAS System | All | 9.1 TS1M3 | n/a | |

SAS System | SAS/STAT | All | 9.1 TS1M3 | n/a |

SAS System | SAS/ETS | All | 9.1 TS1M3 | n/a |