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

*PURPOSE:*- The Margins macro fits the specified generalized linear or GEE model and estimates predictive margins and/or average marginal effects for variables in the model. Differences and contrasts of predictive margins and average marginal effects with confidence limits are also available. Margins and effects can be estimated at specified values of other model variables or at computed values such as means or medians.
*HISTORY:*- The version of the Margins macro that you are using is displayed when you specify
**version**(or any string) as the first argument. For example:%Margins(version, ...)

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.

*Version**Update Notes*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 *REQUIREMENTS:*- SAS/STAT
^{®}and SAS/IML^{®}. *USAGE:*- Follow the instructions on the
**Downloads**tab of this sample to save the Margins macro definition. Replace the text within quotation marks in the following statement with the location of the Margins macro definition file on your system. In your SAS^{®}program or in the SAS editor window, specify this statement to define the Margins macro and make it available for use:%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 effect. 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.

**data=***data-set-name*- Specify the name of the input data set to model.
**response=***variable*- Specify the name of the response variable to be modeled. The specified variable must be numeric. Events/trials syntax for aggregated data is not supported.
**model=***model-effects*- Specify the model to be fit by PROC GENMOD. This is the list of model effects that would appear following the equal sign (=) in the MODEL statement of PROC GENMOD. Nested effects are not supported.

The following parameters are optional. Typically,

**margins=**or**effect=**(or both) are specified. If**margin=**and**effect=**are both omitted, the overall margin is estimated.**margins=***variable(s)*- Margins are estimated for all levels or combinations of levels of the specified variable(s) in the
**data=**data set or, if specified, in the**margindata=**data set. The specified variable(s) must be in the**data=**data set and must also be specified in**model=**. The levels or combinations of levels defining the margins can be reduced using the**margindata=**data set or**margwhere=**. If**margin=**and**effect=**are both omitted, the overall margin is estimated, optionally with variables fixed by**at=**, if specified, or computed only over a subset of observations if**within=**is specified. See**Details**below. **effect=***variable*- Average marginal effects are estimated for the specified continuous variable. Only one variable can be specified and it must not be specified in
**class=**or**offset=**. If**margins=**and/or**at=**are also specified, the average marginal effects for the variable are estimated within each combination of levels of the**margins=**and/or**at=**variables. Marginal effects for categorical variables can be obtained as differences of predictive margins. Specify the**class=**variable in**margins=**instead of**effect=**and request differences with**options=diff**. **margindata=***data-set-name*- Specify a data set containing the
**margins=**variables for defining the margins to be computed. If not specified, the**data=**data set is used. The levels or combinations of levels defining the margins can be reduced using the**margwhere=**option. See**Details**below. **margwhere=***where-condition*- The specified where-condition subsets the
**data=**or**margindata=**data set before determining the levels or combination of levels of the**margins=**variables for which predictive margins will be computed. See**Details**below. **roptions=***response-variable-options*- Specify any options for the response variable as described in the MODEL statement of PROC GENMOD. For the available response variable options, see the GENMOD documentation.
**class=***variable(s)*- Specify a list of any categorical predictors to be placed in a CLASS statement. CLASS statement options available in PROC GENMOD (such as REF=) are not allowed.
**dist=***distribution-name*- Specify the response distribution. Valid distribution names are Normal, Binomial, Poisson, Negbin (for negative binomial), Gamma, Geometric, IGaussian (for inverse gaussian), or Tweedie. Default: Normal.
**link=***link-function-name*- Specify the link function. Valid link function names are Identity, Log, Logit, Probit, CLL, or Power(p), where p is a numeric power value. The default link function is the canonical link for the specified distribution as shown in the description of the DIST= option in the GENMOD documentation.
**offset=***variable*- Specify the offset variable if needed. Typically used for Poisson or negative binomial models when modeling a rate, in which case the offset variable should be the log of the rate denominator. This variable should not be specified in the
**effect=**option. **modelopts=***model-options*- Specify any options to appear in the PROC GENMOD MODEL statement other than DIST=, LINK=, OFFSET=, or SINGULAR=.
**at=***variable(s)*- The margins requested in
**margins=**will be estimated at each level or combination of levels of the specified variable(s) in the**data=**data set or, if specified, the**atdata=**data set. The specified variable(s) must be in the**data=**data set and must also be specified in**model=**. The levels or combinations of levels at which margins will be computed can be reduced using the**atdata=**data set or**atwhere=**. See**Details**below. **atdata=***data-set-name*- Specify a data set containing the
**at=**variables at which the requested margins will be computed. If not specified, the**data=**data set is used. The levels or combinations of levels at which margins will be computed can be reduced using the**atwhere=**option. See**Details**below. **atwhere=***where-condition*- The specified where-condition subsets the
**data=**or**atdata=**data set before determining the levels or combination of levels of the**at=**variables at which predictive margins or marginal effects will be computed. If the where-condition must involve variables not in the model, consider using**within=**. See**Details**below. **within=***where-condition*- After fitting the model, margins and marginal effects are computed by averaging only over the observations meeting the specified condition. The
*where-condition*can involve variables not in**model=**. If quotes appear in the*where-condition*, use single quotes ('), not double quotes ("). Unlike**margins=**and**at=**,**within=**does not fix any variables in the model. It also does not affect the model fit, which is always fit on the complete**data=**data set (minus observations with missing values - see**Missing Values**below). Any statistics options (**options=atmeans**,**mean=**, and others) used to fix variables are also computed on the complete**data=**data set. See**Details**below. **contrasts=***data-set-name*- Specify a data set containing labels and contrast coefficients defining contrasts of predictive margins and/or average marginal effects to be estimated. Note that coefficients should be given for all estimates, not just those within a combination of
**at=**variable values. The data set must contain two character variables, LABEL and F. Each observation of the data set defines one contrast which can be a multi-row contrast. LABEL contains the labels that will identify the contrasts in the results. F contains the coefficients defining each contrast. If the contrast has multiple rows, use commas to separate the sets of coefficients in the rows. **geesubject=***variable or model-effect*- Specifies the effect that defines correlated clusters of observations in GEE models when fitting the model in PROC GENMOD. Required when fitting a GEE model. See the description of the SUBJECT= option in the REPEATED statement in the GENMOD documentation.
**geewithin=***variable*- Optionally specifies the order of measurements within correlated clusters of observations in GEE models when fitting the model in PROC GENMOD. See the description of the WITHIN= option in the REPEATED statement in the GENMOD documentation.
**geecorr=***structure-name*- Specifies the correlation structure when fitting a GEE model in PROC GENMOD. For valid structure names, see the description of the TYPE= option in the REPEATED statement in the GENMOD documentation. Default: IND (the independence structure).
**freq=***variable*- Specifies a frequency variable used when fitting the model in PROC GENMOD. Each observation is treated as if it appears n times, where n is the value of the
**freq=**variable for the observation. **weight=***variable*- Specifies a weight variable used when fitting the model in PROC GENMOD. See the description of the WEIGHT statement in the GENMOD documentation.
**mean=***variable(s)*

**median=***variable(s)*

**q1=***variable(s)*

**q3=***variable(s)*- Use these statistic options to fix model variables at computed values when estimating margins or marginal effects. The specified variables should not appear in
**margins=**or**at=**but must appear in**model=**. Only numeric variables not specified in**class=**should appear in**median=**,**q1=**, or**q3=**. Variables in**mean=**can be specified in**class=**or not. For a**class=**variable specified in**mean=**, the observed proportions are used as values of the dummy variables that represent the variable in the model. **balanced=***variable(s)*- The specified variables must also be specified in
**model=**and**class=**and not in**margins=**or**at=**. For a specified variable with k levels, the values of the dummy variables representing it in the model are all fixed at 1/k when computing predictive margins. **alpha=***value*- Specify the alpha level for confidence intervals with confidence level 1-alpha. Value must be between 0 and 1. Default: 0.05.
**singular=***value*- Specify a singularity criterion for use in PROC GENMOD. Value must be between 0 and 1. See the description of the SINGULAR= option in the MODEL statement in the GENMOD documentation.
**options=***list-of-options*- Specify desired options separated by spaces. Valid options are:
**desc**- Adds the DESCENDING option in the PROC GENMOD statement to model the higher response level in binomial models. However, it is better to explicitly specify the response level to model using the EVENT= response variable option. For example, to model the probability that the response=1, specify
**roptions=event="1"**. **diff**- Estimate all pairwise differences among the margins and/or marginal effects. If
**at=**is specified, differencing is done within each unique combination of the**at=**variable levels. For differencing across the**at=**combinations, use**contrasts=**or specify the**at=**variables in**margins=**rather than in**at=**and specify**options=diff**. **cl**- Provide confidence intervals for predictive margins, average marginal effects, and differences.
**nomodel**- Do not display the fitted model.
**noprint**- Suppress all displayed results. Note that results are always saved in data sets as shown in the Notes section below.
**noprintbyat**- Does not display predictive margins, average marginal effects, and differences in separate tables defined by the
**at=**variables as is done by default. Instead, all margins are displayed in one table (similarly for marginal effects and differences) and the**at=**variable values are included in the table. **reverse**- Reverse the direction of margin and effect differences.
**atmeans**- Compute predictive margins at the means of all other model variables except for those specified in
**at=**. For marginal effects, all variables other than those in**margins=**or**at=**are fixed at their means. The**mean=**,**median=**,**q1=**,**q3=**, and**balanced=**options are ignored. For a**class=**variable, its overall observed proportions are used as values for the dummy variables that represent the variable in the model.

*DETAILS:*- Predictive margins are estimates of the response mean and are typically used when fixing some, but not all, predictors in the model at specified values. The marginal effect of a continuous predictor at an observation estimates the slope of the mean response curve at that observation's setting of the predictors. It is computed as the partial derivative of the mean with respect to the predictor. As such, it is the instantaneous rate of change of the response mean at that point. The average of the marginal effects over the observations (AME) is often used as a measure of the effect of the continuous predictor on the response mean. A similar measure is the marginal effect estimated at the mean of the other predictors (MEM). For small samples, the AME is considered the better measure. A measure of the effect of a categorical predictor on the response mean can similarly be obtained as the difference in predictive margins at two of its levels. This is often considered the "marginal effect" of a binary categorical predictor.
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

**options=diff**. 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. 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. Note that 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.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.#### BY group processing

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 for details on its use. Also see the example titled "BY group processing" on the

**Results**tab above.#### Output data sets

The following data sets containing results are available after successful completion of the macro:

If

**margins=**is specified:**_Margins**- contains the estimated margins, standard errors, tests, and confidence intervals if requested.
**_CovMarg**- contains the estimated covariance matrix of the margins.
**_Diffs***(if***options=diff***is specified)*- contains the differences of the margins with standard errors, tests, and confidence intervals (if requested).
**_Contrasts***(if***contrasts=***is specified)*- contains the tests of the specified contrasts or predictive margins.

If

**effect=**is specified:**_MEffect**- contains the estimated average marginal effects, standard errors, tests, and confidence intervals if requested.
**_CovMeff**- contains the estimated covariance matrix of the average marginal effects.
**_DiffME***(if***options=diff***is specified)*- contains the differences of the average marginal effects with standard errors, tests, and confidence intervals (if requested).
**_ContrastME***(if***contrasts=***is specified)*- contains the tests of the specified contrasts of average marginal effects.

*LIMITATIONS:*- The Margins macro can be used only with a subset of the models available in PROC GENMOD since that procedure is used to fit the specified model. It cannot be used with multinomial or zero-inflated models available in GENMOD. Models for survey data, or for survival data, or models containing random effects or effects constructed by the EFFECT statement are likewise exempted.
Events/trials syntax is not allowed for analysis of aggregated binomial data. Instead, convert the data to use the

**freq=**option.If used in the SAS

^{®}Enterprise Guide^{®}interface, click**Tools**►**Options**►**Data General**. In the**Naming Options**section, set the**Valid variable names**option to**Basic variable names (V7)**. If this is a change, you will need to disconnect from and reconnect to the server. *MISSING VALUES:*- Observations with missing values in any of the model variables are omitted from the analysis. However, observations that are missing only on the response are used. Note that predicted values can be computed for observations missing only on the response and therefore they contribute in the computation of predictive margins and marginal effects. When
**options=atmeans**is specified, these observations also contribute to the means. *SEE ALSO:*- The NLEstimate macro estimates and tests linear or nonlinear combinations of model parameters and can be used to estimate predictive margins when all predictors are fixed. The NLMeans macro can perform 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 NLEstimate macro to do the computations. This 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 PROC QLIM 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.4 TS1M6, marginal effects computed in PROC QLIM are only valid 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.

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 example, several more examples of using the Margins macro can be found in these notes:

- Marginal effect estimation for predictors in logistic and probit models
- Estimating differences in probabilities (marginal effects) with confidence interval
- Estimating the difference in differences of means
- Estimating the risk (proportion) difference for matched pairs data with binary response
- Using the ESTIMATE or CONTRAST statement or Margins macro to assess continuous variable effects in interactions and splines

**EXAMPLE 1: Predictive margins in a binary logistic model**- The following statements estimate and test predictive margins for the Neuralgia data set presented in the example titled "Logistic Modeling with Categorical Predictors" in the LOGISTIC documentation. Treatment predictive margins are computed for males and females. Since the Margins macro requires a numeric response variable, the NoPain variable is created with value 1 when Pain='No' and 0 otherwise. The probability of no pain is modeled as a result of
**roptions=event='1'**. Confidence intervals are requested by**options=cl**.data Neur; set Neuralgia; NoPain=(pain='No'); run; %Margins(data = Neur, class = Treatment Sex, response = NoPain, roptions = event='1', dist = binomial, model = Treatment Sex Treatment*Sex Age Duration, margins = Treatment, at = Sex, options = cl )

The predictive margins are presented in the following tables. For example, the estimated probability of no pain for females in Treatment A is 0.82, and 0.62 for males. Notice that Treatment P produces a noticeably smaller probability than Treatments A or B, and females have higher probabilities in all Treatments.

Adding the

**atmeans**option fixes all other predictors in the model at their mean values. With this option, all predictors in the model are fixed either by**margins=**or**atmeans**. The**nomodel**option prevents displaying the fitted model again.%Margins(data = Neur, class = Treatment Sex, response = NoPain, roptions = event='1', dist = binomial, model = Treatment Sex Treatment*Sex Age Duration, margins = Treatment, at = Sex, options = cl nomodel atmeans)

Following are the predictive margins at the means of Age and Duration. The mean values are shown preceding the margins table. The margins at the means are roughly similar to the previous values.

In the analysis with

**options=atmeans**, all predictors are fixed. In this case, the same results can be obtained using the LSMEANS statement in PROC LOGISTIC as shown below.proc logistic data=Neur; class Treatment Sex / param=glm; model NoPain(event='1') = Treatment Sex Treatment*Sex Age Duration; lsmeans Treatment*Sex / ilink cl at means; run;

The Mean column displays the predictive margins which match those in the previous analysis from the Margins macro.

Differences among the treatment predictive margins, computed within the sexes, can be estimated and tested by adding the

**diff**option. This can be done with or without fixing the other model predictors at their means with the**atmeans**option.%Margins(data = Neur, class = Treatment Sex, response = NoPain, roptions = event='1', dist = binomial, model = Treatment Sex Treatment*Sex Age Duration, margins = Treatment, at = Sex, options = cl diff nomodel)

The results show that both the A and B Treatments produce significantly higher probability than Treatment P, in both sexes. Treatments A and B do not differ significantly.

**EXAMPLE 2: Marginal effects in a binary logistic model**- Using the same data as the previous example, the following estimates the marginal effect for Sex at the means of Treatment, Age and Duration. Since Sex is a binary CLASS variable, its marginal effect is computed as the difference in predictive margins. Since Treatment is also a CLASS variable, its mean is represented in the model by using the overall observed proportions of the Treatments in its dummy variables.
%Margins(data = Neur, class = Treatment Sex, response = NoPain, roptions = event='1', dist = binomial, model = Treatment Sex Treatment*Sex Age Duration, margins = Sex, options = diff cl nomodel atmeans )

The Treatment, Age, and Duration means used in the computations are shown first. Since the Treatments appear equally in the data, the proportions are balanced in the same way as if

**balanced=Treatment**had been specified. Next are the predictive margins for Sex, followed by the marginal effect for Sex, computed as the difference in predictive margins. The marginal effect of Sex, 0.41, is significant (*p*=0.0095) indicating that the probability for Females is significantly larger than for Males. Note that the direction of the difference is indicated by the index values shown in the Difference column ("1-2"). If the Male-Female difference is desired, specify**options=reverse**.The following estimates the average marginal effect of the continuous predictor, Age. The model is expanded to allow for the Age effect to vary with Treatment by including the Age*Treatment interaction. Note that the vertical bar ("|") between variables is equivalent to specifying both main effects and the interaction. The marginal effect computation uses the observed values of the predictors rather than their means since the

**atmeans**option is omitted.%Margins(data = Neur, class = Treatment Sex, response = NoPain, roptions = event='1', dist = binomial, model = Treatment|Sex Treatment|Age Duration, effect = Age, options = cl nomodel )

The estimated margin, -0.036, indicates that increasing Age significantly decreases the probability (

*p*=0.0011).The marginal effect of Age can be compared between the Treatments by specifying a contrast. The following DATA step creates the appropriate data set. The data set must always include a LABEL variable containing labels for the contrasts, and a variable F which contains the contrasts coefficients. Both variables must be character variables. The rows of the specified contrast request the three pairwise differences among the marginal effects for Age in the three Treatments. A joint test of the differences, with 2 degrees of freedom, is also provided. Estimates of the marginal effect of Age at each Treatment are requested with

**at=Treatment**. The**noprintbyat**option arranges the three marginal effects in a single table instead of separate tables labeled by the Treatment.data C; length label f $32767; infile datalines delimiter='|'; input label f; datalines; Treatment | 1 -1 0, 1 0 -1, 0 1 -1 ; %Margins(data = Neur, class = Treatment Sex, response = NoPain, roptions = event='1', dist = binomial, model = Treatment|Sex Treatment|Age Duration, effect = Age, at = Treatment, contrasts= C, options = cl nomodel noprintbyat)

The results indicate that Age significantly decreases the probability of neuralgia in Treatments A and B, but not P. Further, the effect of Age differs significantly only between Treatments A and P (

*p*=0.0429) but the joint test shows no overall difference.The same results can be obtained without the need for

**contrasts=**by specifying**margins=Treatment**and the**diff**option.%Margins(data = Neur, class = Treatment Sex, response = NoPain, roptions = event='1', dist = binomial, model = Treatment|Sex Treatment|Age Duration, effect = Age, margins = Treatment, options = diff cl nomodel )

With this approach, the Treatment predictive margins and their differences are also given. Additionally, the values of the estimated differences between the average marginal effects are provided rather than just tests.

**EXAMPLE 3: Marginal effects in a Poisson model**- This example uses the data in the Getting Started section of the COUNTREG chapter in the SAS/ETS User's Guide.
A Poisson model is fitted and an offset is included to account for differing population sizes or exposure amounts.
Predictive margins and the average marginal effect for the sexes (Fem) are requested.
Since Fem is a CLASS predictor, its marginal effect is computed as a difference between males and females.
90% confidence intervals are requested by
**alpha=0.1**and**options=cl**.%Margins(data = long97data, class = Fem Mar, response = Art, model = Fem|Mar Ment, offset = Lnart, dist = poisson, margins = Fem, alpha = 0.1, options = diff cl )

The results show that the mean count for females is 1.72 and 1.66 for males. Since FEM=0 for males, the Male-Female difference estimates the marginal effect, 0.062, which is not significant (

*p*=0.4937).The average marginal effect for the continuous predictor, Ment, is obtained by the following.

%Margins(data = long97data, class = Fem Mar, response = Art, model = Fem|Mar Ment, offset = Lnart, dist = poisson, effect = Ment, alpha = 0.1, options = cl )

The average marginal effect of Ment, 0.009, is significant (

*p*=0.0133). **EXAMPLE 4: Margins and marginal effects in a GEE model**- This example uses the data in the Generalized Estimating Equations (GEE) example in the Getting Started section of the GENMOD documentation. This call of the Margins macro estimates a logistic GEE model for the probability of Wheezing. Then it estimates and tests the predictive margins for the City levels and the average marginal effect of Smoke in each City. The
**diff**option estimates the marginal effect of City as the the difference in its predictive margins. The difference in the Smoke marginal effects is also provided.%Margins(data = Six, class = Case City, response = Wheeze, roptions = event='1', model = City Age Smoke, dist = binomial, geesubject = Case, geecorr = exch, margins = City, effect = Smoke, options = cl diff)

The results show that the estimated probabilities of wheezing in the two cities are both near 0.3 and the difference, the marginal effect of City, is not significant (

*p*=0.8593). The average marginal effect of Smoke is also shown to not differ significantly (*p*=0.8721) between the cities and, in fact, the Smoke effect on the probability of wheezing is not significant in either city. **EXAMPLE 5: Marginal effect in a log-linked gamma model**- The following example appears in the NLMeans macro documentation where that macro is used to estimate the difference in mean failure times for two manufacturers. This can also be considered the marginal effect of the binary manufacturer variable, MFG, and can be estimated using the Margins macro.
The following macro call estimates the marginal effect as the difference in predictive margins for MFG. The model specified by

**class=**,**response=**,**dist=**, and**model=**is a log-linked gamma model. The predictive margins for the manufacturers and their difference is requested by**margins=**and**options=diff**.%Margins(data = lifdat, class = mfg, response = lifetime, dist = gamma, model = mfg, margins = mfg, options = diff cl)

These results duplicate those from the LSMEANS statement and the NLMeans macro in the NLMeans macro documentation. Each manufacturer's mean failure time is significantly different from zero (

*p*<0.0001). The estimated marginal effect for MFG is 9.23 and is not significant (*p*=0.8986). **EXAMPLE 6: BY group processing**- While the Margins macro does not support BY processing directly, the RunBY macro can be used to run the macro on BY groups in the data. The following uses the Neur data set in the examples above. In the statements below, a DATA step is used to subset the Neur data set to each BY group in turn. This is done with a WHERE statement that specifies the special macro variables, _BYx and _LVLx, which are used by the RunBY macro to process each BY group. The BYlabel macro variable is also used to label the displayed results with the BY group definition. Since the Margins macro writes its own titles, a FOOTNOTE statement is used instead of a TITLE statement to provide the label.
%macro code(); data subset; set Neur; where &_BY1=&_LVL1; run; footnote "Above for &BYlabel"; %margins(data=subset, class=treatment, response=NoPain, roptions=event='1', dist=binomial, model=treatment age duration, margins=treatment, options=nomodel) footnote; %mend; %RunBY(data=Neur, by=sex)

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

Estimate and test predictive margins and average marginal effects in generalized linear and GEE models, optionally at specified values of other model variables. Differences and contrasts with confidence limits are also available.

#### Operating System and Release Information

Type: | Sample |

Topic: | Analytics ==> analytics SAS Reference ==> Macro |

Date Modified: | 2021-07-21 13:16:02 |

Date Created: | 2018-10-08 13:34:26 |

Product Family | Product | Host | SAS Release | |

Starting | Ending | |||

SAS System | SAS/STAT | Windows 7 Professional x64 | ||

Windows 7 Professional 32 bit | ||||

Windows 7 Home Premium x64 | ||||

Windows 7 Home Premium 32 bit | ||||

Windows 7 Enterprise x64 | ||||

Windows 7 Enterprise 32 bit | ||||

Microsoft Windows Server 2016 | ||||

Microsoft Windows XP Professional | ||||

Microsoft Windows Server 2012 Std | ||||

Microsoft Windows Server 2012 R2 Std | ||||

Microsoft Windows Server 2012 R2 Datacenter | ||||

Microsoft Windows Server 2012 Datacenter | ||||

Microsoft Windows Server 2008 for x64 | ||||

Microsoft Windows Server 2008 R2 | ||||

Microsoft Windows Server 2008 | ||||

Microsoft Windows Server 2003 for x64 | ||||

Microsoft Windows Server 2003 Standard Edition | ||||

Microsoft Windows Server 2003 Enterprise Edition | ||||

Microsoft Windows Server 2003 Datacenter Edition | ||||

Microsoft Windows NT Workstation | ||||

Microsoft Windows 2000 Professional | ||||

Microsoft Windows 2000 Server | ||||

Microsoft Windows 2000 Datacenter Server | ||||

Microsoft Windows 95/98 | ||||

Microsoft Windows 2000 Advanced Server | ||||

Microsoft Windows 10 | ||||

Microsoft Windows 8.1 Pro x64 | ||||

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

Microsoft Windows 8.1 Enterprise x64 | ||||

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

Microsoft Windows 8 Pro x64 | ||||

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

Microsoft Windows 8 Enterprise x64 | ||||

OS/2 | ||||

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

Microsoft® Windows® for x64 | ||||

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

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

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

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

OpenVMS VAX | ||||

z/OS 64-bit | ||||

z/OS | ||||

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 |