Author: Bailey DeBarmoreLearning about a method in class, like inverse probability weighting, is different than implementing it in practice. This post will remind you why we might be interested in propensity scores to control for confounding - specifically inverse probability of treatment weights and SMR - and then show how to do so in SAS and Stata. If you have corresponding code in R that you'd like to add to this post, please contact me.A note about weighting versus multivariable regression:Effect estimate interpretations when you use weighting are marginal effect in the target population. When you adjust for covariates in a regression model, you are interpreting a conditional effect, that is, the effect of the exposure holding (conditional on) the covariates being constant. Conditional estimates are troublesome with time-varying covariates because we run into collider bias and conditioning on mediators, thus weights are preferable. In simpler situations, using weights over multivariable regression can help with convergence issues . Files to Download: .txt file with SAS and Stata code, as well as a PDF version of this post with code (perfect for students) available to download at the end of the post or at my github |

UnstabilizedCreate a pseudopopulation 2x the size of our observed - one where everyone is exposed and one where everyone is unexposed. | StabilizedCreate a pseudopopulation maintaining the original population size, but we adjust the covariate distribution within each strata of exposure group by upweighting and downweighting people to match the overall covariate distribution. |

You can see in Figure 5 versus Figure 3 that we are using different covariate distributions. With SMR where we match the covariate distribution of the exposed or unexposed group (whichever is target). This is why IPTW generates an average treatment effect while SMR generates an average treatment effect in the treated.

## coding IPTW in SAS

******************************************

* Calculating IPTW

*****************************************;

&let data=<data>;

&let y=<outcome>;

&let x=<exposure>;

&let id=<id>;

*Estimate denominator - output a dataset with results of regression called denom, with the resulting probabilities stored in variable d;

proc logistic data=&data desc;

model &x = <covariates>;

output out=denom p=d;

run;

*Generate numerator for stabilized weights - output a dataset with results of regression called num, with the resulting probabilities stored in variable n - note that there is nothing on the right side of the equation because the numerator will simply be P(A=a), where a = observed exposure status;

proc logistic data=&data desc;

model &x=;

output out=num p=n;

run;

*Generate stabilized and unstabilized weights by merging the datasets with regression output (merge on the unique identifier in your dataset, &id);

data <newdata>;

merge &data denom num;

by &id;

if &x=1 then do;

uw = 1/d;

sw = n/d;

end;

*Remember we can use 1 - P(exposed) for the unexposed weight components;

else if &x=0 then do;

uw=1/(1-d);

sw=(1-n)/(1-d);

end;

run;

*Check the distribution of your IPTW - the mean should be 1. Is the sum for uw twice the sum of sw? why? is the range of uw greater than sw? why?;

proc means data=<newdata> mean sum min max;

var uw sw;

run;

*You can check to see if your exposure and covariates are associated in your new pseudopopulation (<newdata>);

proc logistic data=<newdata> desc;

weight sw;

model &x=<covariates>;

run;

*Now you can run your main analyses and apply the weights using the weight statement - use sw variable for stabilized weights, and use uw for unstabilized weights - you can use proc genmod, glm, logistic, etc. I'll show you below with logistic you can see now we're using &y and &x - and we don't need the covariates because the confounder -> x arrow is encompassed in the sw weight statement;

proc logistic data=<newdata> desc;

weight sw;

model &y = &x;

run;

## coding IPTW in Stata

The approach to calculating IPTW is different in Stata than SAS. You'll be using the built in teffects command (reference manual [TE] here) and using options to specify IPTW.

Since using IPTW results in the average treatment effect overall, we'll use option

Since using IPTW results in the average treatment effect overall, we'll use option

**ate**(average treatment effect) instead of option**atet**(average treatment effect on the treated) that we use for SMR.******************************************

* Calculating IPTW

*****************************************;

* Syntax for teffects statement

*teffects ipw (<outcome>) (<exposure> <covariates>), ate

*where <outcome> is your outcome variable, <exposure> is your exposure variable, and <covariates> is a list of your covariates to generate your weights.

*Example: Binary

*Outcome = lowbirthwt

*Exposure = maternalsmoke

*Covariates = maternalage nonwhite

*Use the teffects statement to generate your weights and then apply them in a logistic (default) model all in 1 step

teffects ipw (lowbirthwt) (maternalsmoke maternalage nonwhite), ate

*If your outcome is continuous, you can specify a probit model

*Example: Continuous

*Outcome = birthwt

*Exposure = maternalsmoke

*Covariates = maternalage nonwhite

teffects ipw (birthwt) (maternalsmoke maternalage nonwhite, probit), ate

Download resources here, or get the most up-to-date versions on

**my GitHub** Calculating SMR and IPW - EPICODE - SAS.txt | |

File Size: | 7 kb |

File Type: | txt |

Calculating SMR and IPW - EPICODE - Stata | |

File Size: | 4 kb |

File Type: | txt |

Coding IPW and SMR in SAS and Stata - PDF for teachers | |

File Size: | 551 kb |

File Type: |

**About the author**

Bailey DeBarmore is a doctoral student at the University of North Carolina at Chapel Hill studying epidemiology. Find her on Twitter @BaileyDeBarmore and blogging for the American Heart Association on the Early Career Voice blog. |

0 Comments

## Leave a Reply.

Practical solutions for conducting great epidemiology methods. Transparency in code. Attitude of constant improvement.

Appreciate our stuff?

All

Bailey DeBarmore

Data Visualization

Excel

Paul Zivich

P Values

Python

Regression

SAS

Stata

ZEpid

September 2018

August 2018

July 2018

June 2018

May 2018