Estimating the difference in event probability (risk difference or marginal effect) with confidence interval


Since the log odds ( also called the logit ) is the response function in a logistic model, such models enable you to estimate the log odds for populations in the data. A population is a setting of the model predictors. By exponentiating you can estimate the odds. Similarly, the difference between two populations results in an estimated difference in log odds that is equivalent to a log odds ratio. Again, by exponentiating you can estimate the odds ratio comparing the populations. So, simple linear combinations of the model parameters, such as can be specified in or generated by PROC LOGISTIC using the ODDSRATIO, LSMEANS, LSMESTIMATE, SLICE, CONTRAST, or ESTIMATE statements, can provide estimates of odds and odds ratios.

However, the risk difference or difference in population event probabilities (population means) cannot be obtained in this way. To estimate the difference in probabilities, you need to estimate a nonlinear function of the parameters of the logistic model. Alternatively, you could model the probabilities themselves rather than the log odds.

Several methods are illustrated in this note showing how the difference in population probabilities can be estimated and tested in logistic models with either a binary, or a multinomial response. These include the NLMeans, NLEST, and Margins macros, and the NLMIXED, GENMOD, and CATMOD procedures in SAS®9, and in SAS® Viya®, the RISK statement in PROC LOGSELECT (beginning in 2026.05) and the MARGINS statement available in the LOGISTIC and GENMOD procedures.

When the variable whose risk difference is to be estimated is involved in interaction in a model, the risk difference changes depending on the level of the interacting variable(s). When the interacting variable is continuous, the risk difference can be estimated at various levels of the interacting variable and plotted as a function of the interacting variable as discussed in SAS Note 69621.

Other notes illustrate related tasks. To estimate the difference in probabilities with matched pairs data, see SAS Note 46997. For estimating the odds ratio with matched pairs data, see SAS Note 23127. For the simple case of a 2×2 table, a nonmodeling approach to estimating the difference in probabilities can be taken using PROC FREQ as described in SAS Note 22561.

The following data are used to illustrate the various methods of estimating and testing the difference in probabilities. Data set TEST has 100 observations containing two variables: a 0,1 coded binary response variable, Y, with 1 representing the event of interest, and a categorical predictor variable, A, with levels 1, 2, and 3. The following table summarizes the data.

Binary Response — Using the NLMeans macro

See the description of the NLMeans macro (SAS Note 62362) for information about making it available in your SAS® session.

The following statements fit a logistic model and the probabilities for the three A populations are estimated by the ILINK option in the LSMEANS statement. Adding the DIFF option in the LSMEANS statement would estimate the differences in log odds (equivalently, the log odds ratios), not the differences in the probabilities. The E option displays the table of coefficients that defines the LS-means for each level of A. The STORE statement saves the fitted model in an item-store named LogFit. The ODS OUTPUT statement saves the LSMEANS coefficients table in a data set named Coeffs.

proc logistic data=test;
   class a / param=glm;
   model y(event="1") = a;
   lsmeans a / e ilink;
   store LogFit;
   ods output coef=Coeffs;
   run;

In the table displayed by the LSMEANS statement, the estimated population probabilities appear in the column labeled Mean. Notice that the difference in the predicted probabilities for levels 1 and 2 of A is approximately 0.6666 - 0.3966 = 0.2700.

The following call of the NLMeans macro estimates all pairwise differences among the probabilities of the three A populations. Specify the saved model with instore= and the saved LSMEANS coefficients with coef=. Since the link function in a logistic model is the logit, that is specified in link=. An optional title is specified in title=.

  %nlmeans(instore=LogFit, coef=Coeffs, link=logit, title=Differences of A Means)

The Label in the first row of the results table indicates that it corresponds to the difference in the first two levels of A. Note that the estimated difference agrees with the value computed above. The difference is significant (p=0.0353), and a 95% large-sample confidence interval (0.01854, 0.5216) is also provided.

Binary Response — Using the RISK statement

Beginning in SAS® Viya® 2026.05, you can use the RISK statement in PROC LOGSELECT to estimate risk differences (and ratios). See SAS KB0046630, which discusses possible differences in results from the RISK statement and the NLMeans macro based on LS-means.

After starting a CAS session and creating a CAS libref called SASCAS1, these statements fit the same logistic model as fit by PROC LOGISTIC above. The RISK statement requests the pairwise risk difference estimates among the levels of A (DIFF=ALL) as well as estimates of the individual risks used in each difference (SHOWPROBS). Risk ratios (relative risks) rather than differences can be obtained by specifying the TYPE=RELATIVE option:

data sascas1.test;
   set test;
   run;
proc logselect data=sascas1.test;
   class a / param=glm;
   model y(event="1") = a;
   risk a / diff=all showprobs;
   run;

In the results of the RISK statement below, the P1 and P2 columns show the individual risk estimates used in each difference. For example, the A=1 and A=2 risk estimates are 0.6667 and 0.3966 and their difference is 0.2701. To reverse the direction of the difference, specify the REVERSE option in the RISK statement. The standard error, significance test, and confidence interval for the difference are in subsequent columns. These results match those from the NLMeans macro above.

Binary Response — Using the Margins macro

The Margins macro fits the specified model, and estimates the individual population probabilities (the predictive margins), and the differences in probabilities (the marginal effect). See the description of the Margins macro for information about making it available in your SAS® session.

The following call of the Margins macro fits a logistic model and then estimates the predictive margins (probability that Y=1 at each level of A). A test that each equals zero is provided. Differences among the probabilities (marginal effects) are requested with the diff option. The cl option requests confidence intervals for the margins and the differences.

%Margins(data     = test,
         response = y,
         roptions = event='1',
         class    = a,
         model    = a,
         dist     = binomial,
         margins  = a,
         options  = diff cl)

The estimated event probabilities at the levels of A are shown in the Predictive Margins table. The pairwise differences are given in the following table. Note that only the difference between the A=1 and A=2 levels (0.27) is significant (p=0.0353).

Binary Response — Using the MARGINS Statement in SAS Viya

In SAS Viya, you can use the MARGINS statement in PROC LOGISTIC and other SAS/STAT® procedures to estimate predictive margins and their differences (marginal effects) of CLASS variables. The predictive margin for a CLASS variable level is the average predicted response treating all observations as being in that level. In general, predictive margins are not the same as estimates from the LSMEANS statement, but they are equivalent in a single-variable model as in this example. The MARGINS statement is not currently available in PROC LOGSELECT or in SAS 9.

The following statements refit the logistic model above and uses the MARGINS statement to estimate the predictive margins for the levels of A as well as the pairwise differences.

proc logistic data=test;
   class a / param=glm;
   model y(event="1") = a;
   margins a / diff cl;
   run;

The results from the MARGINS statement match those from the Margins macro above.

Binary Response — Using PROC NLMIXED

For details about using PROC NLMIXED, see the NLMIXED documentation.

The differences in means can also be estimated by fitting the logistic model in PROC NLMIXED. With its ESTIMATE statement, you specify the nonlinear functions that estimate the differences. In PROC NLMIXED, you write the model on the event probability, p, and then specify p in the BINARY distribution option in the MODEL statement. The function LOGISTIC(x) = [1 + e-(x)]-1 makes it easy to specify the logistic model:

p = 1/(1+e-(Intercept + b1*A1 + b2*A2))

where A1 and A2 are two indicator variables (A1=1 if A=1, A1=0 otherwise; A2=1 if A=2, A2=0 otherwise). This is equivalent to reference parameterization (PARAM=REF) produced by the CLASS statement in several modeling procedures. The same model can be written in terms of the log odds (logit) as:

logit(p) = log(p/(1-p)) = Intercept + b1*A1 + b2*A2

Under this model, the estimated event probability for the A=1 population is 1/(1+e-(Intercept + b1)), for the A=2 population is 1/(1+e-(Intercept + b2)) and for the A=3 population is 1/(1+e-Intercept). The differences in probabilities between pairs of the three A populations are defined in the ESTIMATE statements. The LOGISTIC function is also used in the ESTIMATE statement to define the individual probabilities. Each ESTIMATE statement provides the estimated difference in probabilities along with its standard error and 95% confidence limits.

proc nlmixed data=test;
   p=logistic(Intercept + b1*(a=1) + b2*(a=2));
   model y ~ binary(p);
   estimate "Prob Diff A1-A2" logistic(Intercept+b1) - logistic(Intercept+b2);
   estimate "Prob Diff A1-A3" logistic(Intercept+b1) - logistic(Intercept);
   estimate "Prob Diff A2-A3" logistic(Intercept+b2) - logistic(Intercept);
   run;

The ESTIMATE statements produce the "Additional Estimates" table below. The estimated difference in probabilities between levels 1 and 2 of A is 0.2701 with a 95% confidence interval of (0.0155, 0.5247). Note these results, which use t-tests, closely match the large-sample results from the NLMeans macro.NOTE

Binary Response — Using the NLEST macro

See the description of the NLEST macro for information about making it available in your SAS® session.

The differences in probabilities can also be estimated with the NLEST macro. When fitting the model in PROC LOGISTIC, include the STORE statement to save the model. Create a data set with an observation for each function to be estimated. See the description of the NLEST macro for details.

These statements produce the same results as PROC NLMIXED above. Note that a large-sample (Wald) test and confidence interval can be obtained by simply removing the DF= option.NOTE

proc logistic data=test;
   class a / param=glm;
   model y(event="1") = a;
   lsmeans a / ilink;
   store logfit;
   run;
data fd; 
   length label f $32767; 
   infile datalines delimiter=',';
   input label f; 
   datalines;
Prob Diff A1-A2,logistic(B_p1+b_p2) - logistic(B_p1+b_p3)
Prob Diff A1-A3,logistic(B_p1+b_p2) - logistic(B_p1)
Prob Diff A2-A3,logistic(B_p1+b_p3) - logistic(B_p1)
;
%nlest(instore=logfit, fdata=fd, df=100)

Binary Response — Using PROC GENMOD or PROC CATMOD

Another approach fits a linear probability model with PROC GENMOD (using maximum likelihood estimation) or PROC CATMOD (using weighted least squares estimation). Note that some data might not be well fit by a linear probability model. Various error conditions, such as invalid mean parameter, can occur during model fitting. In PROC GENMOD, use the DIST=BINOMIAL and LINK=IDENTITY options to model the binomial probabilities directly, rather than the logits. For details about GENMOD, see the GENMOD documentation.

proc genmod data=test descending;
   class a;
   model y = a / dist=binomial link=identity;
   lsmeans a / diff cl;
   run;

The "a Least Squares Means table" shows the estimated probabilities for A along with confidence intervals. The "Differences of a Least Squares Means" table provides the estimated differences in response probabilities. Confidence intervals for the differences are also provided. The estimated differences in probabilities closely match the NLMIXED results.NOTE

The same linear probability model can be fit in PROC CATMOD. The RESPONSE statement specifies a linear combination of the ordered response probabilities, Pr(Y=0) and Pr(Y=1). So, the 0 1 specification causes CATMOD to model Pr(Y=1). Predictors are treated as categorical by default. The PARAM=REF option specifies reference parameterization for the resulting indicator variables. For details about CATMOD, see the CATMOD documentation.

proc catmod data=test;
   response 0 1;
   model y = a / param=ref clparm;
   contrast "Prob Diff A1-A2" a 1 -1 / estimate=parm;
   contrast "Prob Diff A1-A3" a 1  0 / estimate=parm;
   contrast "Prob Diff A2-A3" a 0  1 / estimate=parm;
   run; quit;

The CONTRAST statements estimate the differences in probabilities for pairs of the levels of A as indicated in the labels. The estimated differences again closely match the results above.NOTE

Multinomial Response — Using PROC FREQ

The following data are described in the example titled "Nominal Response Data: Generalized Logits Model" in the PROC LOGISTIC documentation . The three levels of Style are the levels of a multinomial response observed in each of three schools. The three schools are compared on their probabilities of choosing each style.

data School;
   length Program $ 9;
   input School Program $ Style $ Count @@; 
   datalines; 
1 regular   self 10  1 regular   team 17  1 regular   class 26
1 afternoon self  5  1 afternoon team 12  1 afternoon class 50 
2 regular   self 21  2 regular   team 17  2 regular   class 26
2 afternoon self 16  2 afternoon team 12  2 afternoon class 36 
3 regular   self 15  3 regular   team 15  3 regular   class 16
3 afternoon self 12  3 afternoon team 12  3 afternoon class 20 
; 
proc freq; 
   weight Count; 
   table School*Style / nopercent nocol; 
   run;

A nonmodeling approach can be taken using PROC FREQ to construct appropriate 2×2 subtables. The RISKDIFF option provides the estimate of the difference in probabilities along with a confidence interval. To construct each subtable, use formats to combine Styles (columns) and a WHERE statement to drop Schools (rows) as needed in each case.

proc format;
   value $class "class"="1:class" "self","team" ="2:other";
   value $self  "self" ="1:self"  "class","team"="2:other";
   value $team  "team" ="1:team"  "class","self"="2:other";
   run;

The following statements create and analyze a 2×2 table for the comparison of schools 1 and 2 on their probabilities of choosing each style. The WHERE statement in each case omits the data from school 3. Similar statements can be used to do comparisons of the other pairs of schools.

This table compares schools 1 and 2 on Pr(Style=class) after combining the self and team columns.

proc freq order=formatted; 
   where School ne 3;
   format Style $class.;
   weight Count; 
   table School*Style / riskdiff nopercent nocol; 
   run; 

This table compares schools 1 and 2 on Pr(Style=self) after combining the class and team columns.

proc freq order=formatted; 
  where School ne 3;
  format Style $self.;
  weight Count; 
  table School*Style / riskdiff nopercent nocol; 
  run;

This table compares schools 1 and 2 on Pr(Style=team) after combining the class and self columns.

proc freq order=formatted; 
   where School ne 3;
   format Style $team.;
   weight Count; 
   table School*Style / riskdiff nopercent nocol; 
   run;

One criticism of this approach is that not all of the data are used in each analysis. The model-based analyses that follow avoid this problem. However, in this simple case in which School is treated as a nominal effect and there are no covariates, the results from a modeling approach matches the nonmodeling analysis.

Multinomial Response — Using the NLMeans macro

The following statements fit a logistic model, using generalized logits, to the nominal multinomial response. As with the binary example, the LSMEANS / ILINK E; statement provides estimated probabilities and displays the coefficients that define the LS-means. Estimated probabilities are provided for each School on the first two Style levels. The third Style level is not included in the results since that level is redundant given the first two by the constraint that the Style probabilities must sum to one.

  proc logistic data=School;
    freq Count;
    class School / param=glm;
    model Style(ref="team")=School / link=glogit;
    lsmeans School / ilink e;
    store out=MultMod;
    ods output coef=Coeffs;
    run; 

The estimated probabilities below (Mean column) for each School on the first two Style levels are produced by the ILINK option in the LSMEANS statement.

This NLMeans macro call provides pairwise differences among the three Schools on all of the Style probabilities.

  %nlmeans(instore=MultMod, coef=Coeffs, link=glogit,
           title=Differences of School Means)

As indicated by Label, the first three rows are the pairwise differences among the Schools on the first response probability (Style=class). The pairwise differences among the Schools on the next two Style probabilities follow. The results for the School 1 vs. 2 difference agree with the PROC FREQ results above.

Multinomial Response — Using the RISK statement

The following statements, run in a CAS session, fit the same multinomial model using PROC LOGSELECT. The RISK statement requests risk (probability) estimates and differences.

data sascas1.school;
   set school;
   run;
proc logselect data=sascas1.School;
   freq Count;
   class School / param=glm;
   model Style(ref="team")=School / link=glogit;
   risk school / diff=all showprobs;
   run;

Again, the P1 and P2 columns show the individual probability estimates in each difference as indicated in the Description column. The estimated differences appear in the Estimate column. The results match those from the NLMeans macro above.

Multinomial Response — Using the MARGINS Statement in SAS Viya

While the Margins macro does not support the multinomial model, the MARGINS statement available in SAS Viya does. These statements fit the same logistic model as above. The MARGINS statement estimates the predictive margins for each School in each of the Class and Self Style levels. Pairwise differences within each of those two Style levels are also provided. As with the LSMEANS statement, estimates for the redundant Team level are not provided.

proc logistic data=School;
   freq Count;
   class School / param=glm;
   model Style(ref="team")=School / link=glogit;
   margins School / diff cl;
   run;

The results from the MARGINS statement match those above for the nonredundant Style levels.

Multinomial Response — Using the NLEST macro

To estimate these differences with the NLEST macro, create a data set with an observation for each function to be estimated. See the description of the NLEST macro for details. These statements produce the same results as PROC NLMIXED below.

  proc logistic data=School;
     freq Count;
     class School / param=glm;
     model Style(ref="team")=School / noint link=glogit;
     store out=MultMod;
     run;
  data fd; 
     length label f $32767; 
     infile datalines delimiter=',';
     input label f; 
     datalines;
  School 1 vs 2 on Pr(class),exp(b_p1)/(1+exp(b_p1)+exp(b_p2))-exp(b_p3)/(1+exp(b_p3)+exp(b_p4))
  School 1 vs 2 on Pr(self),exp(b_p2)/(1+exp(b_p1)+exp(b_p2))-exp(b_p4)/(1+exp(b_p3)+exp(b_p4))
  School 1 vs 2 on Pr(team),1/(1+exp(b_p1)+exp(b_p2))-1/(1+exp(b_p3)+exp(b_p4))
  ;
 %nlest(instore=MultMod, fdata=fd)

Multinomial Response — Using PROC CATMOD

As in the binary response case above, PROC CATMOD can fit a linear probability model using weighted least squares estimation. The linear combination of ordered response probabilities in the RESPONSE statement below selects the first two probabilities, Pr(Style=class) and Pr(Style=self), which are labeled as Function Number in the results. Again, only two of the three can be modeled since the constraint that they must sum to one limits the number of independent probabilities to two. The NOINT and PARAM=REF options cause the estimated parameters of the model to be the estimated probabilities for schools 1 and 2 on the first two probabilities.

  proc catmod data=school;
     weight Count;
     response 1 0 0, 0 1 0;
     model Style=School / clparm noint param=ref;
     contrast 'School 1 vs 2 on Pr(class)' all_parms  1  0 -1  0 / estimate=parm;
     contrast 'School 1 vs 2 on Pr(self)'  all_parms  0  1  0 -1 / estimate=parm;
     contrast 'School 1 vs 2 on Pr(team)'  all_parms -1 -1  1  1 / estimate=parm;
     run; quit;

The CONTRAST statements estimate the differences between Schools 1 and 2 on each Style. The ALL_PARMS keyword enables you to specify a list of multipliers that define a linear combination of model parameters. For example, in the first CONTRAST statement, the linear combination is (1)(0.6333)+(-1)(0.4844)=0.1490, which compares Schools 1 and 2 on Pr(Style=class). The second CONTRAST statement does similarly for the comparison on Pr(Style=self). The third CONTRAST statement estimates the difference on Pr(Style=team) using the fact that the sum of the Style probabilities within each school must sum to one. Similar sets of CONTRAST statements could be written to compare the other pairs of schools.

Multinomial Response — Using PROC NLMIXED

PROC NLMIXED can fit a nominal, multinomial logistic model using maximum likelihood estimation. You can use its ESTIMATE statement to estimate differences in probabilities. However, since the multinomial distribution is not supported in the MODEL statement, it must be defined via the GENERAL distribution. The statements below define a generalized logit model:

log(OddsCT) = log(Pr(Style=class)/Pr(Style=team)) = βc1S1 + βc2S2 + βc3S3
log(OddsST) = log(Pr(Style=self)/Pr(Style=team)) = βs1S1 + βs2S2 + βs3S3

where S1=1 if School=1 and 0 otherwise, and similarly for S2 and S3. Under this model, the estimate of Pr(Style=team) in the School=1 population is 1/(1+OddsCT+OddsST), where OddsCT=exp(βc1) and OddsST=exp(βs1). The estimates for Pr(Style=class) and Pr(Style=self) simply multiply Pr(Style=team) by OddsCT or OddsST respectively.

The ESTIMATE statements produce estimates of the probability differences between Schools 1 and 2 on each Style. Similar ESTIMATE statements can be written to compare the other pairs of Schools.

proc nlmixed data=school;
   /* Create numeric response variable */
   if Style='class' then y=1;
   else if Style='self' then y=2;
   else y=3;

   /* Specify the nominal multinomial logistic model */
   OddsCT=exp( (School=1)*bc1 + (School=2)*bc2 + (School=3)*bc3 );
   OddsST=exp( (School=1)*bs1 + (School=2)*bs2 + (School=3)*bs3 );
   PrT=1/(1+OddsCT+OddsST); 
   PrS=PrT*OddsST;
   PrC=PrT*OddsCT;
   if      y=1 then p=PrC;
   else if y=2 then p=PrS;
   else             p=PrT;
  
   /* Define multinomial log likelihood */
   ll = Count*log(p);
   model y ~ general(ll);
  
   /* Compare schools 1 and 2 */
   estimate 'School 1 vs 2 on Pr(class)'
       exp(bc1)/(1+exp(bc1)+exp(bs1))-
       exp(bc2)/(1+exp(bc2)+exp(bs2));
   estimate 'School 1 vs 2 on Pr(self)'
       exp(bs1)/(1+exp(bc1)+exp(bs1))-
       exp(bs2)/(1+exp(bc2)+exp(bs2));
   estimate 'School 1 vs 2 on Pr(team)'
       1/(1+exp(bc1)+exp(bs1))-
       1/(1+exp(bc2)+exp(bs2));
   run;

Estimates are the same as from the other methods.NOTE

_______

Note: The confidence limits produced by NLMIXED differ slightly from those produced by CATMOD, GENMOD, or the default (df= is not specified) from the NLMeans or NLEST macros because NLMIXED uses a quantile from the t distribution to multiply the standard error of the predicted value in forming the interval, while FREQ, GENMOD, and CATMOD use a quantile from the standard normal distribution. This difference diminishes as the sample size increases. To more closely match the results from the NLMeans or NLEST macros to those from NLMIXED, specify df= in the macro to use the same degrees of freedom as NLMIXED.