**********************************************************
* Program Name: Calculating SMR and IPTW in SAS
* Original date: April 14 2019
* Updated: Feb 22 2021 for categorical exposure
* Author: Bailey DeBarmore
* Software: Stata
* Downloaded from www.baileydebarmore.com/epicode
**********************************************************;
******************************************
* Calculating SMR weights
*****************************************;
* Syntax for teffects statement
*teffects ipw () ( ), atet
*where is your outcome variable, is your exposure variable, and
* 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), atet
*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), atet
******************************************
* Calculating IPTW
*****************************************;
* Syntax for teffects statement
*teffects ipw () ( ), ate
*where is your outcome variable, is your exposure variable, and
* 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
*---------------------------------------------------------------------*
*If your treatment (exposure) is categorical, you can't use teffects.
*Instead, use mlogit to estimate the probability of treatment
*adjusted for your variables and then use postestimation 'predict'
*to create a denominator variable for each value of treatment
*using option outcome
*Example code is shown below for a treatment (exposure) with values 0, 1, 2, 3
mlogit
predict p0, outcome(0)
predict p1, outcome(1)
predict p2, outcome(2)
predict p3, outcome(3)
*Then create your unstabilized iptw variable by exposure level using your new denominators
gen iptw=.
replace iptw=1/p0 if ==0
replace iptw=1/p1 if ==1
replace iptw=1/p2 if ==2
replace iptw=1/p3 if ==3
*To create stabilized weights, tabulate your treatment (exposure) varaible to get
*the proportion of your dataset in each category. Then plug in those values as
*the numerator for stabilized weights. Code below is shown for an example where
60% are in X=0, 14% in X=1, 20% in X=2, and 6% in X=3.
gen siptw=.
replace siptw=0.6/p0 if ==0
replace siptw=0.14/p1 if ==1
replace siptw=0.2/p2 if ==2
replace siptw=0.06/p3 if ==3