Select Your Region
Americas
Europe
Middle East & Africa
Asia Pacific
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
Contents: | Purpose / History / Requirements / Usage / Details / Limitations / Missing Values / See Also |
%margins(v)
The Margins 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:
NOTE: Unable to check for newer version of Margins macro.
The computations performed by the macro are not affected by the appearance of this message. However, this check can be avoided by specifying nochk as the first macro argument. This can be useful if your machine has no connection to the internet.
Version
|
Update Notes
|
2.02 | In options=, added nonobs. |
2.0 | Removed SAS/IML® requirement. Added diff= and classgref=. In options=, added rate, nogencheck, and covout. nochk can be specified as the first (version) parameter. Removed freq= (see weight=). |
1.08 | Fixed bug causing errors from within= option. |
1.07 | Fixed bugs: mislabeled estimates, errors, or bad margin estimate if level does not exist or is dropped due to all responses missing. |
1.06 | Fixed problem with truncated parameter names. |
1.05 | Fixed problems with formatted CLASS variables. |
1.03 | Add within=. |
1.02 | Provide estimates and tests for each row in contrasts. |
1.01 | Allow effect= variable to be in other options. |
1.0 | Initial coding |
%inc "<location of your file containing the Margins macro>";
Following this statement, you can call the Margins macro.
The Margins macro both fits the model and estimates the requested predictive margins and/or marginal effects. The macro cannot be used to compute margins or marginal effects for previously fitted models unless the data are available and the model can be specified in the macro to be refitted.
To estimate predictive margins for the levels of a variable (or combinations of levels of multiple variables) in the model, specify the options needed to fit the desired model and specify the variable(s) in margins=. To estimate the marginal effect of a continuous variable in the model, specify the options needed to fit the desired model and specify the variable in effect=.
See the Results tab for examples.
The following parameters are required when using the Margins macro.
The following parameters are optional. Typically, margins= or effect= (or both) are specified. If margin= and effect= are both omitted, the overall margin(s) is estimated.
The Margins macro estimates and tests predictive margins and marginal effects (AMEs and MEMs). Estimates and tests of differences of predictive margins and marginal effects are available with diff=, which can provide all pairwise differences, sequential differences, or differences with a specified control level. Tests of contrasts of predictive margins and marginal effects are available with contrasts=.
Note that when all model predictors are fixed at specified values, the predictive margin equals the conditional predicted mean at that setting of the predictors. The MEM, with predictors fixed at their means, is an example of this. In this case the margin can be estimated in various ways such as with the PRED= option in the OUTPUT statement of the modeling procedure, or by using the appropriate coefficients in an ESTIMATE statement with the ILINK option. When the data are balanced and the margins= variable is not involved in interactions with other predictors, the predictive margin can also be obtained with the LSMEANS statement by including the ILINK option and possibly the AT and OM options. Also in this case, the marginal effect of a categorical predictor, computed as the difference in means, can be obtained using the NLMeans macro (SAS Note 62362). However, when at least one predictor is not fixed, the Margins macro is needed to compute predictive margins.
By default, a complete replicate of the data= data set is created for each combination of levels of the margins= and/or at= variables in the data= data set. Each replicate fixes the margins= and/or at= variables for all observations at one combination of levels. The predictive margin for each combination is computed as the average predicted value in that combination's replicate. Similarly, the average marginal effect is the average of the marginal effects computed for the observations in that combination's replicate. If within= is specified, the averaging is done only over the observations that meet the specified condition. The data set containing all replicates can become very large if the input data set is large, or if there is a large number of combinations, or both. Consequently, specifying a variable in margins= or at= that has a large number of levels is not recommended unless the number of levels is constrained using one or more of margindata=, margwhere=, atdata=, and atwhere=.
Note that the distinction between specifying some variables in at= as compared to adding them to the variable(s) in margin= is generally minimal, only amounting to a difference in the way the estimates are presented in the displayed results. The same is true if the margins= variables were instead added in the at= list. That is because data replicates for the same combinations of variable levels are created in these cases. However, this will not be true if some of the combinations do not actually occur in the data. In that case, using both margins= and at= can result in additional estimates that do not appear using only margins= or only at=.
You can use the margindata= and/or atdata= option to specify the levels for or at which predictive margins (and marginal effects, if requested) will be computed. This is particularly useful when one or more desired levels does not occur in the data= data set. The margwhere= and/or atwhere= option can be used to subset the data= data set (or the corresponding margindata= or atdata= data set, if specified) before determining the levels.
Note that if neither margins= nor at= are specified, then no replication of the data= data set is done and the overall predictive margin or marginal effect is computed only using the data= data set. When margins= is not specified, the predictive margins are labeled as "Overall" margins.
In addition to fixing the values of any margins= or at= variables as described above, other predictors can be fixed at computed values using the statistics options (mean=, median=, q1=, and q3=) or balanced= or options=atmeans. Variables affected by these options are fixed at the computed statistic value in all observations in all replicates. When options=atmeans is specified, all other predictors are fixed at their means. Note that only mean= and balanced= can be used with variables specified in class=.
The delta method is used to determine the standard errors for predictive margins and marginal effects. If options=cl is specified, large-sample (Wald) tests and confidence intervals are provided.
While the Margins macro does not directly support BY group processing, this capability can be provided by the RunBY macro, which can run the Margins macro repeatedly for each of the BY groups in your data. See the RunBY macro documentation (SAS Note 66249) for details about its use. Also see the example titled "BY group processing" on the Results tab above.
The following data sets containing results are available after successful completion of the macro:
If margins= is specified:
If effect= is specified:
Events/trials syntax, as used in several procedures, is not supported for the analysis of aggregated binomial data. Instead, modify the data so that each events/trials observation becomes two observations with a variable indicating the response level and a variable containing the observed count for that response level. Then specify the first variable in response= and the count variable in weight=.
The NLMeans macro (SAS Note 62362) can perform multiple comparisons among the levels of a model effect on the mean scale. It can be used to estimate differences of predictive margins when all predictors are fixed.
Estimates of marginal effects at the observation level are also available in the QLIM procedure in SAS/ETS® for the models that procedure fits. Use the MARGINAL option in the OUTPUT statement of PROC QLIM. Standard errors for observation marginal effects are not available. As of SAS® 9.4M6 (TS1M6), marginal effects computed in PROC QLIM are valid only for predictors that are not involved in higher-order model effects such as interactions.
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.