Contents: |
Purpose / History / Requirements / Usage / Details / Limitations / See Also |

**NOTE: Beginning in SAS ^{®} 9.4M6 (TS1M6), this macro is available in the SAS/STAT^{®} Autocall library as the NLEST macro and does not need to be downloaded and defined before use. **

*PURPOSE:*- The NLEST (or NLEstimate) macro allows you to estimate one or more linear or nonlinear combinations of parameters from any model for which you can save the model parameters and their variance-covariance matrix. Most modeling procedures which offer ESTIMATE, CONTRAST, or LSMEANS statements only provide for estimating or testing linear combinations of model parameters. However, common estimation problems often involve nonlinear combinations, particularly in generalized models with nonidentity link functions such as logistic and Poisson models.
*HISTORY:*- The version of the NLEST (or NLEstimate) macro that you are using is displayed in the log when you specify
**version**(or any string) as the first argument. For example:%NLEST(version, ...)

The macro always attempts to check for a later version of itself. If it is unable to do this (such as if there is no active internet connection available), the macro will issue the following message in the log:

NOTE: Unable to check for newer version

The computations performed by the macro are not affected by the appearance of this message.

*Version**Update Notes*1.8 Added optional **print=**and**null=**parameters.1.6 Beginning with this release, the macro can be called using the preferred name NLEST and is available in the SAS/STAT Autocall Library. 1.51 Minor fix to version printing. 1.5 Added optional **title=**and**listnames=**parameters.1.4 When **df=**is omitted, large-sample Wald statistics are produced. When**score=**is omitted, results are saved in data set EST in addition to being displayed.1.3 Fixes error that occurred when input model has only a single parameter such as an intercept-only model. 1.2 SAS/IML ^{®}no longer required.1.1 Added **score=**and**outscore=**parameters.1.0 Initial coding *REQUIREMENTS:*- Base SAS
^{®}and SAS/STAT^{®}software are required. You might also need another SAS product to fit the desired model before using the macro. *USAGE:*- Follow the instructions on the
**Downloads**tab of this sample to save the NLEST/NLEstimate macro definition. Replace the text within quotation marks in the following statement with the location of the macro definition file on your system. In your SAS program or in the SAS editor window, specify this statement to define the macro and make it available for use:%inc "<location of your file containing the NLEST macro>";

Following this statement, you can call the macro. See the

**Results**tab for examples.There are two basic uses of the macro:

**Display parameter names**. Specify**shownames**as the first argument to display a list of the model parameter estimates and their associated names. The parameter names are needed to write the functions to be estimated in a subsequent run of the macro. The**f=**and**fdata=**parameters can be omitted when using**shownames**. For example:`%nlest(shownames, instore=logmod)`

**Estimation of functions**. Input the saved model using**instore=**(preferred) or both of**inest=**and**incovb=**. Then specify the function or functions to estimate using either the**f=**or**fdata=**parameter (or both). Do not specify**shownames**. The macro again displays the parameter names and also displays estimates of the specified functions. For example:`%nlest(instore=logmod, fdata=funcs)`

Following are the parameters available with the macro. The necessary model information is provided to the macro by specifying either the

**instore=**parameter or both the**inest=**and**incovb=**parameters. If the modeling procedure provides a STORE statement for saving the fitted model,**instore=**is generally the better method for providing the model information. Additionally, if**shownames**is not specified as the first argument, either the**fdata=**or**f=**parameters (or both) is required.**shownames**- When specified as the first parameter in the macro call and
**score=**is not specified, displays a list of the model parameter estimates and their associated names as mentioned above. **instore=***item-store*- Specifies the fitted model that was saved using the STORE statement in the modeling procedure. The OUT= option in the STORE statement saves the model in a file known as an
*item store*. This is the preferred method for providing the required model information. However, if the modeling procedure does not offer the STORE statement, then you may be able to use the**inest=**and**incovb=**parameters. See Limitations below. **inest=***data-set-name*- Specifies the data set of parameter estimates saved using an ODS OUTPUT statement in the modeling procedure. The parameter estimates of the model should be stored in a variable named ESTIMATE in this data set. An error is issued if the ESTIMATE variable is not found.
**incovb=***data-set-name*- Specifies the data set containing the variance-covariance matrix of model parameters saved using an ODS OUTPUT statement in the modeling procedure. Typically, an option such as COVB is required in the modeling procedure to make this matrix available for saving. If the saved data set contains numeric variables other than those containing the covariance matrix, they should be removed before specifying the data set in this macro parameter in order to avoid a compatibility error. The
**incovb=**data set should have the same number of observations (rows) and variables (columns) as the number of rows in the**inest=**data set in order to be compatible. See Limitations below. **fdata=***data-set-name*- Specifies a data set containing the functions to be estimated (estimands). This parameter can be used when you have more than one function to estimate. This data set must contain two character variables with names LABEL and F. In each observation of the data set, F contains an expression to be estimated involving the parameter names of the model. LABEL contains a text string used to label the estimate of the function in the results. The label does not need to be enclosed in quotes. Do not use double quotes (") in the either the F or LABEL variable. Use single quotes (') instead if needed. The displayed results are also saved in data set Est.
**f=***expression*- Specifies an expression representing a single function to be estimated (the estimand) involving the parameter names. This parameter can be used when only one function is to be estimated. Do not use double quotes (") in the expression. Use single quotes (') instead if needed. Any quotes must appear in pairs. For estimands that involve values from data set variables, also use the
**score=**parameter. When**score=**is not specified, the displayed results are also saved in data set Est. **label=***text-string*- Optionally, can be used to label the single function specified in the
**f=**parameter. Ignored if**f=**is not specified. Do not use quotes surrounding or within*text-string*. **null=***value*- Specifies
*value*in the null hypothesis H0: f(β)=*value*, where f(β) is the function of parameters specified in**f=**or**fdata=**and*value*is a numeric value. Scientific notation, such as 1E4, is not allowed. Requires version 1.8 or later of the macro. The default is**null=0**. **df=***value*- Specifies the degrees of freedom to be used in the test and confidence interval computed for the estimated functions.
*value*must be a non-zero, positive value. Scientific notation, such as 1E4 is not allowed. If omitted or null, large-sample Wald statistics are given. The degrees of freedom for testing a linear combination of parameters in a linear model would typically be the number of observations used in fitting the model minus the number of parameters estimated in the model – essentially, the error degrees of freedom. **alpha=***value*- Specifies the alpha level to be used in computing confidence limits. If omitted, alpha=0.05.
**score=***data-set-name*- Specifies a data set for which the function in
**f=**is evaluated for each observation. Use this parameter along with the**f=**parameter when the estimand involves values of variables in the input data set. The**score=**parameter cannot be used with the**fdata=**parameter. **outscore=***data-set-name*- Names a data set to be created that is a copy of the
**score=**data set and has additional variables for the estimated function, its standard error, test statistic, p-value, and confidence limits in each observation. If not specified, the data set is named NLEst. No data set is created if**score=**is not specified. **listnames=yes|no**- Display parameter names or not before the results table.
**listnames=no**suppresses the table of names. The default is**listnames=yes**. **print=yes|no**- Display or suppress the results table. The default is
**print=yes**. Note that**print=**has no effect on displaying the table of parameter names which is controlled by**listnames=**. To suppress all printed output, specify**print=no**and**listnames=no**. Requires version 1.8 or later of the macro. The default is**print=yes**. **title=***title-text*- Specifies a title for the results table. The
*title-text*must not contain quotes (" or '), ampersands(&), commas(,), or parentheses. If omitted,**title=**Nonlinear Function Estimate.

*DETAILS:*- The NLEST (or NLEstimate) macro takes advantage of the ESTIMATE statement in PROC NLMIXED which can estimate and test nonlinear (or linear) functions of model parameters, f(β). It uses NLMIXED to fit a multivariate normal distribution with mean equal to the input model parameter estimates and variance equal to the input model covariance matrix. This creates a log likelihood that is zero and NLMIXED is able to converge immediately to a model with the specified parameter estimates and covariance matrix. It is then possible to estimate and test the function(s) specified in the
**f=**or**fdata=**parameters. The test provided is a test of the null hypothesis H0: f(β)=*value*, where*value*is specified in the**null=**parameter.**BY group processing**The NLEST macro does not directly support BY group processing. That is, it cannot process results from a modeling procedure that was run using a BY statement. However, this capability can be provided by the RunBY macro which can run both the modeling procedure and the NLEST macro repeatedly for each of the BY groups in your data. See the RunBY macro documentation for details on its use. Also see the example titled "BY group processing" in the

**Results**tab above.**Output data set**Results from the macro are automatically saved in data set EST.

*LIMITATIONS:*- Some modeling procedures cannot provide the necessary covariance matrix for some models or estimands. For example, since PROC GLIMMIX cannot provide a covariance matrix that includes the scale parameter, an estimand involving the scale parameter cannot be estimated. Some procedures either do not have a STORE statement (such as PROC FMM) or do not save the necessary model information (such as PROC COUNTREG). In such cases, use
**inest=**and**incovb=**instead of**instore=**. Note that in some cases, such as with PROC FMM, the data set containing the covariance matrix must be modified to remove extraneous numeric variables in order to avoid a compatibility error. *SEE ALSO:*- The NLMeans macro can be used to do multiple comparisons among the levels of a model effect on the mean scale. The macro forms the expressions that must be evaluated and then calls the NLEST macro to do the computations. See the NLMeans macro description for details. The Margins macro can also be used to estimate and test differences or contrasts of means. Unlike the NLMeans macro, the Margins macro can be used when one or more predictors in the model is not fixed.

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.

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.

In addition to the following examples, several more examples of using the NLEST (or NLEstimate) macro can be found in these notes:

- Estimating differences in probabilities with confidence interval
- Estimating sensitivity, specificity, positive and negative predictive values, and other statistics
- Estimating rate differences (with confidence interval) using a Poisson model
- Estimating relative risks in a multinomial response model
- Confidence interval for a ratio of two linear combinations of model parameters
- Comparing groups with respect to the dose (or predictor value) that yields a specified response probability
- Harmonic mean estimate, confidence interval, and test for lognormal data
- Estimating and comparing counts and rates (with confidence intervals) in zero-inflated models
- Estimating a relative risk (also called risk ratio, prevalence ratio)
- Fitting truncated Poisson and negative binomial models
- Estimating the difference in differences of means
- Estimating attributable risks when covariates are present using logistic and other models

**EXAMPLE 1: Estimate difference in group means in a log-linked gamma model**- The example titled "Gamma Distribution Applied to Life Data" in the GENMOD documentation presents failure times of 201 machine parts from two manufacturers, denoted A and B. These GENMOD statements fit the log-linked gamma model and use the LSMEANS statement to estimate the manufacturer means. The STORE statement saves the model in an item store for later use by the NLEST macro.
proc genmod data = lifdat; class mfg; model lifetime = mfg / dist=gamma link=log; lsmeans mfg / e ilink diff exp; store out=gammod; run;

The values in the Estimate column in "MFG Least Squares Means" table are the estimates of the linear combinations of model parameters defined in the "Coefficients for MFG Least Squares Means" table. This table of coefficients is produced by the E option. That is, the MFG="A" estimate, 6.1501, is the Row1 linear combination defined as 1*Intercept+1*MFG_{A}. Similarly for MFG="B". The Intercept, MFG_{A}, and MFG_{B}estimated model parameters are shown in the "Analysis Of Maximum Likelihood Parameter Estimates" table. Note that since the linear predictor in this gamma model estimates the log gamma mean, linear combinations of the model parameters, such as those from the LSMEANS, ESTIMATE, or CONTRAST statements, also estimate the log gamma mean. Therefore, 6.1501 is the estimated log mean for manufacturer A. The ILINK option in the LSMEANS statement applies the inverse of the link function to the estimates. In this log-linked model, that means that the estimates are exponentiated. The resulting mean estimates are presented in the column labelled "Mean". The estimated mean lifetimes are 468.74 for manufacturer A and 459.51 for manufacturer B. The DIFF option computes the pairwise differences among the LS-mean estimates and presents them in the "Differences of MFG Least Squares Means" table. This produces a difference of the log means, or equivalently a log ratio of means, in the Estimate column. The ratio of means is estimated to be exp(6.1501-6.1302) = 468.74/459.51 = 1.0201. The EXP option exponentiates this difference producing in an estimated ratio of means in the Exponentiated column. Note that the EXP option also exponentiates the estimates in the "MFG Least Squares Means" table resulting in the Exponentiated column that reproduces the Mean column from the ILINK option in this case.Analysis Of Maximum Likelihood Parameter Estimates Parameter DF Estimate Standard

ErrorWald 95% Confidence Limits Wald Chi-Square Pr > ChiSq Intercept 1 6.1302 0.1043 5.9257 6.3347 3451.61 <.0001 MFG A 1 0.0199 0.1559 -0.2857 0.3255 0.02 0.8985 MFG B 0 0.0000 0.0000 0.0000 0.0000 . . Scale 1 0.8275 0.0714 0.6987 0.9800

Coefficients for MFG Least

Squares MeansParameter MFG Row1 Row2 Intercept 1 1 MFG A A 1 MFG B B 1

MFG Least Squares Means MFG Estimate Standard Error z Value Pr > |z| Mean Standard Error

of MeanExponentiated A 6.1501 0.1159 53.07 <.0001 468.74 54.3172 468.74 B 6.1302 0.1043 58.75 <.0001 459.51 47.9468 459.51

Differences of MFG Least Squares Means MFG _MFG Estimate Standard Error z Value Pr > |z| Exponentiated A B 0.01989 0.1559 0.13 0.8985 1.0201 Note that the difference in means cannot be directly estimated by PROC GENMOD using the LSMEANS (or ESTIMATE) statement. Instead, you can estimate the difference in log means (log mean ratio) or ratio of means as shown above. Of course, a point estimate of the mean difference can be computed from the estimated means: 468.74 - 459.51 = 9.23. However, a confidence interval for this difference is not easily obtained. The mean difference is this nonlinear combination of parameters:

mean(A) - mean(B) = exp(Intercept+MFG

_{A}) - exp(Intercept)This nonlinear function can be estimated with the NLEST macro. In order to specify the function to estimate, the names of the parameters must be known. Running the NLEST macro with

**shownames**as the first argument displays the parameter names. The model information is provided in the**instore=**macro parameter by specifying the name of the item store saved previously by the STORE statement.%nlest(shownames, instore=gammod)

The resulting table displays the parameter names you need to write the functions you want to estimate.

Name Estimate B_p1 6.1302 B_p2 0.01989 B_p3 0 The nonlinear function above can then be written and specified in the

**f=**macro parameter.%nlest( instore=gammod, label=Mean Diff, f=exp(B_p1+B_p2)-exp(B_p1) )

The estimated mean difference is 9.2309, agreeing with the value computed above. A large-sample 95% confidence interval for the mean difference is (-132.77, 151.23). Note that a warning message is produced in this example since the default GLM parameterization is used for the CLASS variable, MFG, which is not a full-rank parameterization. The results are still correct. If a full rank parameterization was used, such as reference parameterization as requested with the PARAM=REF option in the CLASS statement, the warning would not appear and the results are the same.

Nonlinear Function Estimate

Label Estimate Standard Error Wald Chi-Square Pr > ChiSq Alpha Lower Upper Mean Diff 9.2309 72.4517 0.016233 0.8986 0.05 -132.77 151.23 The function to estimate can also be specified in a data set using the

**fdata=**macro parameter. This method is particularly useful when there are multiple functions to estimate. The following statements create a data set with the required variables, LABEL and F, for estimating the manufacturer means and their difference. Each observation in the data set specifies a function to estimate in F and a label for the function in LABEL.data fd; length label f $32767; infile datalines delimiter=','; input label f; datalines; Mfg A,exp(B_p1+B_p2) Mfg B,exp(B_p1) Mean Diff,exp(B_p1+B_p2)-exp(B_p1) ; %nlest(instore=gammod, fdata=fd)

The mean estimates match those from the LSMEANS statement above and the difference estimate is the same as from the previous NLEST call.

Nonlinear Function Estimate

Label Estimate Standard Error Wald Chi-Square Pr > ChiSq Alpha Lower Upper Mfg A 468.74 54.3172 74.4726 <.0001 0.05 362.28 575.20 Mfg B 459.51 47.9468 91.8495 <.0001 0.05 365.54 553.49 Mean Diff 9.2309 72.4517 0.0162 0.8986 0.05 -132.77 151.23 Note that the desired model and nonlinear functions of the parameters can be estimated by using PROC NLMIXED. The ESTIMATE statement in NLMIXED can estimate nonlinear functions of model parameters. This is fairly easy for model like the gamma model above since the gamma distribution is directly supported in the MODEL statement of NLMIXED. But this can be more difficult for distributions that are not directly available such as the multinomial, beta, lognormal, and other distributions as well as special distributions like truncated or zero-inflated distributions. The NLEST macro uses NLMIXED and its ESTIMATE statement by adopting the fitted model parameters and covariance matrix supplied by other modeling procedures. This enables you to fit the desired model using the most natural and convenient procedure and can greatly simplify estimation of nonlinear functions of model parameters such as differences of means in generalized models when using a link function other than the identity link.

These statements fit the gamma model above (using reference parameterization for MFG) and again estimate the means and mean difference. The use of very large degrees of freedom (df=1e8) essentially produces a large-sample test and confidence interval like the default from the NLEST macro.

proc nlmixed data=lifdat; mu=exp(b1+b2*(mfg="A")); model lifetime ~ gamma(scale,mu/scale); estimate "Mfg A" exp(b1+b2) df=1e8; estimate "Mfg B" exp(b1) df=1e8; estimate "Mean Diff" exp(b1+b2)-exp(b1) df=1e8; run;

The results are very close to those from the NLEST macro above differing only because of minor estimation method differences.

**EXAMPLE 2: BY group processing**- The NLEST macro cannot process results from a modeling procedure that used a BY statement. However, the RunBY macro can be used to run both the modeling procedure and the NLEST macro on BY groups in the data. The following uses the insurance data that appears in the example titled "Poisson Regression" in the Getting Started section of the GENMOD documentation. The difference in age rates is to be estimated by car size using separate models fit to the data for each car size.
In the statements below, a WHERE statement is included in the GENMOD modeling step to subset the input data to one level of CAR. The special macro variables, _BYx and _LVLx, are used by the RunBY macro to fit the model to each BY group and then to run the NLEST macro. The BYlabel macro variable is specified in a TITLE statement in GENMOD and in

**title=**in NLEST to label the displayed results with the BY group definition.%macro code(); proc genmod data=insure; where &_BY1=&_LVL1; class age; model c=age / dist=poisson offset=ln; lsmeans age / e diff exp cl; ods output coef=coeffs; store out=insmodel; title "&BYlabel"; run; %nlest(instore=insmodel, label=Rate Difference, f=exp(b_p1+b_p2)-exp(b_p1), listnames=no, title=Difference of Age Rates &BYlabel) %mend; %RunBY(data=insure, by=car)

Right-click on the link below and select **Save** to save the NLEST/NLEstimate macro definition to a file. It is recommended that you name the file nlest.sas.

The NLEST macro allows you to estimate one or more linear or nonlinear combinations of parameters from any model for which you can save the model parameters and their variance-covariance matrix.

#### Operating System and Release Information

Type: | Sample |

Topic: | Analytics ==> Regression SAS Reference ==> Procedures ==> NLMIXED SAS Reference ==> Macro |

Date Modified: | 2021-04-27 11:34:23 |

Date Created: | 2016-08-11 14:16:41 |

Product Family | Product | Host | SAS Release | |

Starting | Ending | |||

SAS System | N/A | Aster Data nCluster on Linux x64 | ||

DB2 Universal Database on AIX | ||||

DB2 Universal Database on Linux x64 | ||||

Netezza TwinFin 32-bit SMP Hosts | ||||

Netezza TwinFin 32bit blade | ||||

Netezza TwinFin 64-bit S-Blades | ||||

Netezza TwinFin 64-bit SMP Hosts | ||||

Teradata on Linux | ||||

z/OS | ||||

z/OS 64-bit | ||||

IBM AS/400 | ||||

OpenVMS VAX | ||||

N/A | ||||

Android Operating System | ||||

Apple Mobile Operating System | ||||

Chrome Web Browser | ||||

Macintosh | ||||

Macintosh on x64 | ||||

Microsoft® Windows® for 64-Bit Itanium-based Systems | ||||

Microsoft Windows Server 2003 Datacenter 64-bit Edition | ||||

Microsoft Windows Server 2003 Enterprise 64-bit Edition | ||||

Microsoft Windows XP 64-bit Edition | ||||

Microsoft® Windows® for x64 | ||||

OS/2 | ||||

SAS Cloud | ||||

Microsoft Windows 8 Enterprise 32-bit | ||||

Microsoft Windows 8 Enterprise x64 | ||||

Microsoft Windows 8 Pro 32-bit | ||||

Microsoft Windows 8 Pro x64 | ||||

Microsoft Windows 8.1 Enterprise 32-bit | ||||

Microsoft Windows 8.1 Enterprise x64 | ||||

Microsoft Windows 8.1 Pro 32-bit | ||||

Microsoft Windows 8.1 Pro x64 | ||||

Microsoft Windows 10 | ||||

Microsoft Windows 95/98 | ||||

Microsoft Windows 2000 Advanced Server | ||||

Microsoft Windows 2000 Datacenter Server | ||||

Microsoft Windows 2000 Server | ||||

Microsoft Windows 2000 Professional | ||||

Microsoft Windows NT Workstation | ||||

Microsoft Windows Server 2003 Datacenter Edition | ||||

Microsoft Windows Server 2003 Enterprise Edition | ||||

Microsoft Windows Server 2003 Standard Edition | ||||

Microsoft Windows Server 2003 for x64 | ||||

Microsoft Windows Server 2008 | ||||

Microsoft Windows Server 2008 R2 | ||||

Microsoft Windows Server 2008 for x64 | ||||

Microsoft Windows Server 2012 Datacenter | ||||

Microsoft Windows Server 2012 R2 Datacenter | ||||

Microsoft Windows Server 2012 R2 Std | ||||

Microsoft Windows Server 2012 Std | ||||

Microsoft Windows XP Professional | ||||

Windows 7 Enterprise 32 bit | ||||

Windows 7 Enterprise x64 | ||||

Windows 7 Home Premium 32 bit | ||||

Windows 7 Home Premium x64 | ||||

Windows 7 Professional 32 bit | ||||

Windows 7 Professional x64 | ||||

Windows 7 Ultimate 32 bit | ||||

Windows 7 Ultimate x64 | ||||

Windows Millennium Edition (Me) | ||||

Windows Vista | ||||

Windows Vista for x64 | ||||

64-bit Enabled AIX | ||||

64-bit Enabled HP-UX | ||||

64-bit Enabled Solaris | ||||

ABI+ for Intel Architecture | ||||

AIX | ||||

HP-UX | ||||

HP-UX IPF | ||||

IRIX | ||||

Linux | ||||

Linux for x64 | ||||

Linux on Itanium | ||||

OpenVMS Alpha | ||||

OpenVMS on HP Integrity | ||||

Solaris | ||||

Solaris for x64 | ||||

Tru64 UNIX |