Contents: | Purpose / History / Requirements / Usage / Details / Limitations / See Also |
NOTE: Beginning in SAS® 9.4M6 (TS1M6), a version of this macro is available in the SAS/STAT® Autocall library and does not need to be downloaded and defined before use. To access features in more recent versions of the macro (see History), download and run as described in Usage below.
%nlmeans(version, ...)
The NLMeans 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.4 | Added where= and covdrop=. Requires version 1.9 or later of the NLEST macro. |
1.3 | Added print | noprint to options=. Added null=. Requires version 1.8 or later of the NLEST macro. |
1.04, 1.1 | Store sets of results in data sets EST1, EST2, ... . Append all sets into a single results file using options=append. Version 1.1 is available in the SAS/STAT Autocall Library beginning in SAS 9.4M6 (TS1M6). |
1.03 | Fix for LABEL variable in contrasts= data set. |
1.02 | Minor fix to version printing. |
1.01 | A LABEL variable can optionally be included in the contrasts= data set. |
1.0 | Initial coding |
%inc "<location of your file containing the NLEST macro>"; %inc "<location of your file containing the NLMeans macro>";
After defining both macros, fit a generalized linear model and include one or more LSMEANS, SLICE, or ESTIMATE statements with the E option and save the model using the STORE statement. You can then call the NLMeans macro to compare means. See the Results tab for examples.
The following parameters are required when using the NLMeans 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.
The following parameters are optional:
While the DIFF option in the LSMEANS and SLICE statements provide pairwise differences on the link scale, Liβ-Ljβ, differences on the mean scale, μi-μj, are not available. Similarly, in an ESTIMATE statement that defines a difference, Liβ-Ljβ, the ILINK option applies the inverse of the link function to the difference, g-1(L1β-L2β) rather than computing the difference of the inverse linked estimates g-1(L1β)-g-1(L2β) = μi-μj. The quantity g-1(L1β-L2β) is generally only of interest in the case where the link function, g, is the log. In this case, the ILINK (or EXP) option estimates the ratio of means.
Note that the NLMeans macro is not needed for models that use the identity link. This includes models fit by the REG, GLM, MIXED, or ORTHOREG procedures and others. For these models, differences of Liβ are differences of μi. Consequently, the results of the DIFF option in the LSMEANS or SLICE statement, or the results of an ESTIMATE statement that defines a difference of Liβ directly provide the differences of μi. You can also use the LSMESTIMATE statement to estimate or test differences or contrasts of means.
The NLMeans macro can be used to provide estimates and tests of differences of means, μi-μj, ratios of means, μi/μj, or more complex contrasts of means, Liμi. Standard errors are obtained using the delta method. To do this, you supply the saved model and a data set containing the coefficients, Li, used by the LSMEANS, SLICE, or ESTIMATE statements. You also indicate the link function used in the model. The model is best saved using the STORE statement in the modeling procedure. The coefficients can be saved by including the E option in any LSMEANS, SLICE, or ESTIMATE statement specified in the procedure, and by including an ODS OUTPUT statement to save the displayed table of coefficients in a data set. In most cases, the name of the coefficients table is Coef, so the following statement saves it in a data set.
ods output coef=data-set-name;
See the list of macro parameters above for details about how to provide the saved model and coefficients to the macro, about how to request means, ratios, or contrasts, and other options.
The macro can process more than one set of estimates. Multiple sets of estimates occur when the modeling procedure includes: a SLICE statement, multiple LSMEANS or ESTIMATE statements, or a combination of these statements. The macro processes each set and a table of results is displayed for each set of estimates.
For ordinal multinomial models, the macro by default produces comparisons within each of the response functions. To do comparisons across all response functions, specify options=difall. For nominal multinomial models (link=glogit), the macro produces comparisons within each of the response level probabilities. When specifying a contrasts= data set, each contrast (row) of the data set must contain coefficients for all estimates in all response functions (ordinal) or in all response levels (nominal).
Since the LSMEANS and SLICE statements require GLM parameterized CLASS variables (PARAM=GLM in the CLASS statement), the NLEST/NLEstimate macro called by the NLMeans macro will typically display the following Warning message in this log. This Warning can be ignored.
WARNING: The final Hessian matrix is not positive definite, and therefore the estimated covariance matrix is not full rank and may be unreliable. The variance of some parameter estimates is zero or some parameters are linearly related to other parameters.
Specifying inest= and incovb= instead of instore= is generally not necessary. If used, modifications of those data sets or use of where= and/or covdrop= might be needed to correct incompatible parameter vector and covariance matrix. In some cases, it might be necessary to use the NLEST macro directly rather than NLMeans.
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. Otherwise, an error message is issued that indicates the relevant numbers of rows and columns. If the incovb= data set contains numeric variables other than those containing the covariance matrix, they should be removed in order to avoid a compatibility error. This can be done either by preprocessing the data set to remove the extraneous variables or by specifying them in covdrop= (requires version 1.4 or later of the NLMeans macro and version 1.9 or later of the NLEST macro).
When the macro processes a single set of estimates (such as from a single LSMEANS statement), results are automatically saved in data set EST. When multiple sets of estimates are processed, the results from each set are saved by default in separate data sets named EST1, EST2, EST3, ... . Specify options=append to create a single data set named EST_ALL of all results from all sets. If EST_ALL already exists, new results are appended to it. The last results set is also stored in data set EST.
The NLMeans macro does not directly support BY group processing (such as for the analysis of multiply imputed data) or processing of domains from a survey analysis. That is, it cannot process results from a modeling procedure that was run using a BY or DOMAIN statement. However, this capability can be provided by the RunBY macro, which can run the NLMeans macro repeatedly for each of the BY groups or domains. Version 1.4 or later of the NLMeans macro, version 1.9 or later of the NLEST macro, and version 1.1 or later of the RunBY macro are required. See the RunBY macro documentation (SAS Note 66249) for details about its use. Additionally, you can use where= to allow NLMeans to process the results of one BY group or domain by specifying an appropriate condition to select that BY group or domain. See the Example 2 in the Results tab above.
Some modeling procedures cannot provide the necessary covariance matrix for some models. 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=. When using inest= and incovb=, incompatibility of the parameter vector and covariance matrix can occur. See "Compatibility error when using inest= and incovb=" above.
For some models, such as those fit by the GENMOD or GLIMMIX procedures, use of the LSMEANS, SLICE, or ESTIMATE statements in the PLM procedure is recommended rather than using those statements in the modeling procedure. Using those statements in PLM requires saving the fitted model using the STORE statement in the modeling procedure.
Note that the NLMeans macro can only use coefficients produced by the LSMESTIMATE statement if the statement defines an individual mean and not if it defines a difference (or some other function) of means.
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 examples below, several more examples of using the NLMeans macro can be found in these notes:
The example titled "Gamma Distribution Applied to Life Data" in the GENMOD documentation (SAS Note 22930) presents failure times of 201 machine parts from two manufacturers, denoted A and B. The following GENMOD statements fit the log-linked gamma model. The STORE statement saves the model in an item store for later use by the NLMeans macro. The LSMEANS statement with the ILINK option is used in PROC PLM to estimate the manufacturer means.
proc genmod data = lifdat; class mfg / param=glm; model lifetime = mfg / dist=gamma link=log; store out=gammod; run; proc plm restore=gammod; lsmeans mfg / e ilink diff exp; ods output coef=coeffs; run;
In the partial results below, the intercept, MFGA, and MFGB estimated model parameters are shown in the "Analysis Of Maximum Likelihood Parameter Estimates" table. The values in the Estimate column in the "MFG Least Squares Means" table are the estimates of the linear combinations of model parameters, Liβ, defined in the "Coefficients for MFG Least Squares Means" table. This table of coefficients is produced by the E option and will be needed by the NLMeans macro. From this table it can be seen that the MFG="A" estimate, 6.1501, is the Row1 linear combination defined as 1*Intercept+1*MFGA. Similarly for MFG="B". 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 or ESTIMATE statement, also estimate the log gamma mean. Therefore, 6.1501 is the estimated log mean for manufacturer A. 6.1302 is the estimated log mean for manufacturer B. 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 (0.01989). The EXP option exponentiates this difference producing in an estimated ratio of means in the Exponentiated column (1.0201). 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.
|
The difference of the manufacturer means can be estimated by the NLMeans macro. Before using the macro, both the NLMeans macro and the NLEST/NLEstimate macro that it calls must be defined in your current SAS session. Use the %INCLUDE statements described in the Usage section of the Details tab.
With the macros defined, you can now call the NLMeans macro. The fitted model, saved by the STORE statement as gammod, is provided by instore= and the data set of coefficients defining the LS-means and saved by the ODS OUTPUT statement is provided by coef=. The link function used for this model is the log function. By default, diff=all meaning that all pairwise differences will be computed. Since there are two estimates, there is only one difference to estimate. A title for the table of results is supplied by title=. Note that quotation marks should not be used in the title.
%NLMeans(instore=gammod, coef=coeffs, link=log, title=Difference of MFG means)
The estimated difference in lifetime means is 468.74 - 459.51 = 9.2309 with 95% large-sample confidence interval (-132.77, 151.23). The Label column displays the contrast applied to the mean estimates showing that the computed difference is MFGA-MFGB. Note that if the MFGB-MFGA difference is desired, then add options=reverse in the macro call.
|
The ratio of means can also be provided by the NLMeans macro. Since it is of interest to test that the ratio equals 1 rather than 0 (the default), null=1 is specified.
%NLMeans(instore=gammod, coef=coeffs, link=log, options=ratio, null=1, title=Ratio of MFG means)
The Label (1 /1) indicates that the computed ratio is MFGA/MFGB. If preferred, the reciprocal of this ratio can be estimated by adding options=reverse in the macro call.
Notice that the estimated ratio, 1.0201, is identical to the estimate provided by the EXP option in the LSMEANS statement. The ratio does not significantly differ from 1, the null hypothesis value specified by null=1 (p=0.8995). A large-sample 95% confidence interval for the ratio is (0.7083, 1.3319).
|
The difference in manufacturer means can also be produced by using the NLEST/NLEstimate macro directly, though this requires writing the difference of means in terms of the model parameter estimates. This is illustrated in the Example in the Results tab of the NLEST/NLEstimate macro description (SAS Note 58775).
The NLMeans macro cannot process results from a modeling procedure that uses a BY or DOMAIN statement. However, the RunBY macro (SAS Note 66249) can be used to run the NLMeans macro on BY groups or domains in the saved results. To process a single BY group, where= can be used. The capabilities shown below require version 1.4 or later of the NLMeans macro, version 1.9 or later of the NLEST macro (SAS Note 58775), and version 1.1 or later of the RunBY macro.
The following uses the insurance data that appears in the example titled "Poisson Regression" in the Getting Started section of the GENMOD documentation (SAS Note 22930). The differences in age rates are to be estimated by car size using separate models fit to the data for each car size. These statements fit and save the models using the STORE statement. The rates for the AGE levels in each BY group (CAR) are estimated by the LSMEANS statement in the PLM procedure. The coefficients defining the LS-means are displayed by the E option and are saved by the ODS OUTPUT statement.
proc sort data=data.insure out=insure; by car; run; proc genmod data=insure; by car; class age; model c=age / dist=poisson offset=ln; store out=insmodel; run; proc plm restore=insmodel; lsmeans age / e ilink; ods output coef=coeffs; run;
The following NLMeans macro call specifies a condition in where= to select the large car model and estimate the difference in age rates in large cars.
%NLMeans(instore=insmodel, coef=coeffs, link=log, where=car='large', title=Difference of age rates for large cars)
The next statements process all of the BY groups using the RunBY macro. The NLMeans macro call is placed in the CODE macro and the condition in where= is changed to use the special macro variables, _BYx and _LVLx. Since the BY groups are defined by the levels of the CAR variable in the INSURE data set, data=insure and by=car are specified in the RunBY macro call. The BYlabel macro variable is specified in title= in NLMeans to label the displayed results with the BY group definition.
%macro code(); %NLMeans(instore=insmodel, coef=coeffs, link=log, where=&_BY1=&_LVL1, title=Difference of Age Rates &BYlabel) %mend; %RunBY(data=insure, by=car)
Domain analysis from the survey analysis procedures is similar to BY processing in that multiple models are fit to the domains identified in the DOMAIN statement. The following uses the data in the example titled "Domain Analysis" in the SURVEYREG documentation (SAS Note 22930). Dichotomized versions of the body weight and age variables, BW2 and AGEGR, are created using the RANK procedure. The SURVEYLOGISTIC procedure then estimates a logistic model in each of the two domains identified by the two levels of the CANCER variable. The fitted models are saved using the STORE statement.
It is of interest to estimate the probability of low birthweight (BW2=0) in each age group and to estimate the relative risk (ratio) of these probabilities in each of the cancer domains. The probabilities are estimated by the ILINK option in the LSMEANS statement. It is necessary to specify the LSMEANS statement in the PLM procedure rather than directly in the survey analysis procedure in order to successfully apply the where= condition. The coefficients defining the LS-means are displayed by the E option and are saved by the ODS OUTPUT statement.
proc rank data=cancer groups=2 out=c2; var bodyweight age; ranks bw2 agegr; run; proc surveylogistic data=c2; class agegr/param=glm; strata strata; cluster psu; weight ObservationWt; model bw2(event="0") = agegr; domain cancer; store out=logdom; run; proc plm restore=logdom; lsmeans agegr / e ilink; ods output coef=c; run;
The following NLMeans macro call estimates the relative risk of the age probabilities in the cancer (CANCER=1) domain. The cancer domain is selected by the where= condition. Specifying options=ratio requests computation of the ratio of estimated means from the LSMEANS statement, and null=1 tests the null hypothesis that the relative risk equals 1.
%NLMeans(instore=logdom, coef=c, where=cancer=1, link=logit, options=ratio, null=1, title=Relative Risk in cancer domain)
The relative risk can be computed for both domains by using the RunBY macro in the same way as for BY processing above.
%macro code(); %NLMeans(instore=logdom, coef=c, where=&_BY1=&_LVL1, link=logit, options=ratio, null=1, title=Relative Risk for &BYlabel) %mend; %RunBY(data=c2, by=cancer)
The analysis of multiply imputed data also involves BY processing to run the intended analysis procedure for each imputation data set. So, the use of the NLMeans macro follows a similar procedure as above to provide the desired estimates for each imputation. The estimates from the NLMeans macro can then be combined using the MIANALYZE procedure.
Suppose that the original data containing missing values is multiply imputed using the MI procedure, resulting in a data set, IMPUT, containing multiple imputations of the original data. The blocks of observations containing the imputations are indexed by the variable, _IMPUTATION_. Further suppose that the intended analysis on the original data involves fitting a log-linked negative binomial model to the repeated measurements on the count response, and the desire is to estimate the difference in mean count between levels of a categorical predictor. Then, given the IMPUT data set of the original data, the following statements conduct the analysis on each imputation data set, saves the fitting model in item store NB, and saves the coefficients on the model parameters defining the LS-means for categorical predictor of interest, A, in data set C. Predictor A might have two or more distinct levels. The ODS EXCLUDE and ODS SELECT options are used to suppress the displayed results from the multiple analyses.
ods exclude all; proc genmod data=imput; by _imputation_; class a subj; model c = x a / dist=negbin link=log; lsmeans a / ilink e; repeated subject=subj; store nb; ods output coef=c; run; ods select all;
Using the above approach for BY processing, the NLMeans macro can now be used inside the RunBY macro to estimate the difference in mean counts for each imputation data set. In the RunBY macro, the _BY1 macro variable in this case is the _IMPUTATION_ variable provided by PROC MI. Its values are 1, 2, 3, ... and are represented by the _LVL1 macro variable. The NONAMES and NOPRINT options in the NLMeans macro are used to suppress all displayed results from the multiple runs of the NLMeans macro. The NLMeans macro automatically saves the results from each run in data set EST. Since this data set does not contain the _IMPUTATION variable and value for each run, it is added in the DATA EST step. The APPEND procedure is used to accumulate the mean difference results in data set ALLDIFF.
%macro code; %nlmeans(instore=nb, coef=c, link=log, where=&_BY1=&_LVL1, options=nonames noprint, title=rate diffs) data est; set est; _imputation_=&_LVL1; run; proc append base=alldiff data=est; run; %mend; %runby(data=imput, by=_imputation_)
Finally, the NLMeans results in data set ALLDIFF from the multiple imputations can be combined into a single estimate for each mean difference using the MIANALYZE procedure. The mean difference estimates are in the variable Estimate and their standard errors are in the variable StandardError. The distinct (and possibly multiple) mean difference estimates from each NLMeans analysis are identified in the Label variable.
proc sort data=alldiff; by Label; run; proc mianalyze data=alldiff; by Label; modeleffects Estimate; stderr StandardError; run;
Right-click on the link below and select Save to save the NLMeans macro definition to a file. It is recommended that you name the file NLMeans.sas.
Type: | Sample |
Topic: | Analytics ==> Regression SAS Reference ==> Macro |
Date Modified: | 2023-07-07 17:08:43 |
Date Created: | 2018-05-23 16:41:36 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | SAS/STAT | Microsoft Windows 8 Enterprise 32-bit | ||
z/OS | ||||
z/OS 64-bit | ||||
OpenVMS VAX | ||||
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 | ||||
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 Server 2016 | ||||
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 |