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.
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:
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 diff=all. 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, diff = all, options = cl 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.
%Margins(data = Neur, class = Treatment Sex, response = NoPain, roptions = event='1', dist = binomial, model = Treatment Sex Treatment*Sex Age Duration, margins = Sex, diff = all, options = 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. 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 three pairwise comparisons 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).
However, a note in the log indicates that the joint test for the contrast rows could not be provided resulting in missing statistic and p-value in the first row of the table. Because Treatment has three levels and therefore only two degrees of freedom, only two comparisons are independent. Consequently, in order to conduct the joint test, the contrast should contain only two of the three pairwise comparisons. By keeping only the first two of the three observations in data set C, the following table is produced, which provides the joint test which shows no overall difference.
The same pairwise comparison results can be obtained without the need for contrasts= by specifying margins=Treatment and diff=all.
%Margins(data = Neur, class = Treatment Sex, response = NoPain, roptions = event='1', dist = binomial, model = Treatment|Sex Treatment|Age Duration, effect = Age, margins = Treatment, diff = all, options = cl nomodel)
With this approach, the Treatment predictive margins and their differences are also given.
The following macro call models the incidence rate and estimates and tests the marginal effect of the continuous YEAR variable on the rate. The first levels of the categorical TYPE and PERIOD predictors are specified as reference levels in the model with classgref=first. LOGMONTHS is specified as the offset. effect=year and options=rate cl together request estimation of the rate marginal effect of YEAR along with a confidence interval. If rate is not specified, the macro estimates the marginal effect of YEAR on the mean incidence count rather than the incidence rate.
%Margins(data = ship, class = type period, classgref= first, response = y, offset = logmonths, model = type year period, dist = poisson, effect = year, options = rate cl)
The estimated marginal effect of YEAR on the incidence rate is 0.00068 and differs significantly from zero (p=0.0024).
%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, diff = all, options = cl)
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.
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 diff=all.
%Margins(data = lifdat, class = mfg, response = lifetime, dist = gamma, model = mfg, margins = mfg, diff = all, options = 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).
To do this with margins or marginal effects, specify options=covout to create a separate data set containing the covariance matrix of the margins (_CovMarg) or marginal effects (_CovMeff). Estimates of the margins or marginal effects are automatically saved in data set _Margins or _MEffect. Specify the estimates in inest= and their covariance matrix in incovb= in the NLEST macro. Then specify the desired function of those estimates in f=. In this function, the estimates are referred to using the names b_p1, b_p2, b_p3, and so on in the order presented by the macro. The result can optionally be labelled by specifying label=. Other options are available and are described in the NLEST macro documentation (SAS Note 58775).
The following example estimates the relative risk of the predictive margins of Sex levels estimated using a logistic model on the binary NoPain variable. The predictive margins are estimated event probabilities. The relative risk is a ratio of these probability estimates. While the Margins macro can estimate the margins and their difference, as done below with diff=, it cannot directly estimate the ratio of margins. The following Margins macro call creates the _Margins data set containing the estimates of the margins, and options=covout creates the _CovMarg data set containing their covariance matrix. These are then specified in the NLEST macro. The function in f= specifies the ratio of the Sex='M' margin, referred to as b_p2 since it is the second estimate, to the Sex='F' margin, referred to as b_p1. To test that this ratio is equal to 1 rather than equal to 0 as is the default, null=1 is also specified. A label is specified in label=.
%Margins(data = Neur, class = Treatment Sex, response = NoPain, roptions = event='1', dist = binomial, model = Treatment Sex Treatment*Sex Age Duration, margins = Sex, diff = all, options = covout cl nomodel) %nlest(inest=_Margins, incovb=_CovMarg, f=b_p2/b_p1, null=1, label=Rel. Risk)
The estimated Sex margins and difference are provided by the Margins macro. The NLEST macro confirms the names assigned to the margins followed by the estimated relative risk (0.6540), which differs significantly from 1 (p=0.0022). A 95% confidence interval is also provided.
%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)
To illustrate, the statements below run the Margins macro for each of two variables, Job and Reason, to estimate predictive margins separately for each. The DATA step creates data set MargVars with a variable named MargVar containing these two variable names. The appropriate Margins macro call is placed in the special macro, CODE, which the RunBY macro runs once for each level of the by= variable in the data= data set. By specifying data=MargVars and by=MargVar, RunBY runs the code in the CODE macro for each of the two variables. In each run, each variable name in turn is stored in the special macro variable _LVL1, so this macro variable is specified in margins=. To avoid repeatedly displaying the fitted model, options=nomodel is also specified. Since the variable name specified in margins= does not need to be quoted, lvlquote=no is specified in RunBY. To run Margins on the variables in the order specified in the MargVars data set, order=data is also included.
data MargVars; length MargVar $20; input MargVar $ @@; datalines; job reason ; %macro code(); %margins(data=sampsio.hmeq, response=bad, roptions=event='1', dist=binomial, class=job reason, model=job reason Delinq Derog, margins=&_LVL1, options=cl nomodel) %mend; %RunBY(data=MargVars, by=MargVar, lvlquote=no, order=data)
If you want to create a data set containing all of the margins from both variables rather than have them displayed, the following variation can be used. All displayed results from the Margins macro are suppressed by options=noprint. Since the _Margins data set created by the Margins macro does not contain the name of the margins= variable, that is added by the DATA step prior to the PROC APPEND step, which accumulates the _Margins data sets into data set AllMargins. To avoid problems caused by varying numbers of levels and differing names of the variable containing the level names, the DROP= and RENAME= options are used in the APPEND step.
%macro code(); %margins(data=sampsio.hmeq, response=bad, roptions=event='1', dist=binomial, class=job reason, model=job reason Delinq Derog, margins=&_LVL1, options=cl noprint) data _Margins; set _Margins; length MargVar $20; MargVar="&_LVL1"; run; proc append base=AllMargins data=_Margins(drop=cov: rename=(&_LVL1=Level)); run; %mend; %RunBY(data=MargVars, by=MargVar, lvlquote=no, order=data) proc print label; id margvar level; var estimate stderrpm lower upper chisq pr; title "Predictive Margins"; run;
The following is the AllMargins data set displayed by PROC PRINT.
The same approaches as above can be used for multiple marginal effects variables. In this case, the DROP= and RENAME= options in the APPEND step are not necessary.
Right-click 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.
A fix for this issue for SAS/STAT 15.3 is available at:
https://tshf.sas.com/techsup/download/hotfix/HF2/L9L.html#63038Type: | Sample |
Topic: | Analytics ==> analytics SAS Reference ==> Macro |
Date Modified: | 2023-12-13 14:17:55 |
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 |