*! version 1.5 18oct2025

*! version 1.0 10sep2025

flexdid -- Flexible estimation of difference-in-differences regression with staggered timing of treatment

Syntax

flexdid
ovar
covarlist
[if] [in]
[weight],
tx(varname:txvar)
group(varname:groupvar)
time(varname:timevar)
[options]

ovar is the outcome of interest.

covarlist specifies the covariates in the model and may contain
factor variables; see fvvarlist.

txvar is a binary variable indicating observations subject to
treatment.

groupvar is a categorical variable that indicates the group level at
which the treatment occurs.

timevar is an integer-valued time variable.

Model
* tx(varname:txvar) specify the treatment variable

* group(varname:groupvar) specify the group variable for group-fixed effects and,
by default, the groups for which which treatment effects are estimated

* time(varname:timevar) specify the time variable for time-fixed effects and
the time periods for which treatment effects are estimated

specification(flexdid##mtype:stype) specify the regression;
default is specification(lagsonly)

txgroup(varname:txgroupvar) specify an alternative treatment group variable;
group fixed effects are estimated at the groupvar level

xinteract(varlist:xintvarlist) specify covariate interactions;
default is all covariates interacted with treatment, group and time indicators

noxinteract specify no covariate interactions; cannot be combined with
xinteract(varlist:xintvarlist)

usercohort(varlist:cohortvar) specify the cohort variable if the internally generated cohort
variable is inappropriate, e.g., when some time periods are missing

verbose display output of underlying regression

SE/Robust
vce(varlist:vcetype) vcetype may be cluster clustvar
or robust; default is cluster groupvar

stype

lagsonly specify the regression to include only lags parameters; the default

lagsandleads specify the regression to include lags and leads parameters

* tx() group() and time() are required.

covarlist may contain factor variables; see fvvarlist.

fweights and pweights are allowed;
see weight.

See flexdid postestimation for aggregation features available after estimation.
Also see regress postestimation for additional features available after OLS regression.

Description

flexdid estimates average treatment effects on the treated (ATETs) in difference-in-differences designs with
staggered implementation of treatment using a flexible linear model estimated by pooled OLS with covariates, (FLEX),
as described in Deb et al. (2025). flexdid estimates treatment effects at the group X time period level using
a flexible linear regression specification. flexdid then aggregates those effects to an overall ATET as well
as, via postestimation commands, ATETs at various levels of aggregation that are more disaggregated than the
overall ATET.

In the formal design, there must be at least one time-period in which all units are
untreated, i.e., there cannot be any always-treated units. In addition, the typical design includes a set of
never-treated units. FLEX can be specified using lags only parameters. In this case, the FLEX regression
produces group by time effects that are identical to those producted by the estimator described in Borusyak et al. (2021).
FLEX can also be specified using lags and leads parameters. In this case, when there are no covariates, the
FLEX regression produces cohort by time effects that are identical to those produced by the regression adjustment
estimator described in Callaway and Sant'Anna (2021).

flexdid allows for specification flexibility in a number of dimensions. The basic specification interacts
all covariates with treatment, group, and time indicators. Optionally, the user can select a subset of covariates to
be included in the interactions, with a larger set of covariates entering the regression in the typical additive manner.
flexdid also allows the treatment group indicators to be disaggregates of cohorts, and for the group-level fixed
effects in the regression to be different from (typically disaggregates of) the treatment group indicators. flexdid
can handle designs with no never-treated units with additional identification assumptions. flexdid can also
handle designs in which data are missing in some time-periods (periods in which cohorts of treatment started but are
unobserved).

Options

Model

tx(varname:txvar) specifies a binary treatment variable that indicates whether
an observation is treated in a given time period.

group(varname:groupvar) specifies a numeric group variable for group-fixed effects and,
by default, the groups for which which treatment effects are estimated.

time(varname:timevar) specifies a numeric time variable for time-fixed effects and
the time periods for which which treatment effects are estimated.

specification(stype) specifies the regression, either lagsonly
or lagsandleads; the default is specification(lagsonly).

lagsonly specifies the estimation of the lags only specification. In the
lags only specification all pre-period effects are set to zero.

lagsandleads specifies the estimation of the lags and leads specification. In the
lags and leads specification, only the effect in the period preceding the first period of treatment
for each cohort is set to zero.

txgroup(varname:txgvar) specifies an alternative treatment group variable,
overriding group(varlist:groupvar) as the groups for which treatment effects
are estimated; fixed effects remain at groupvar level.

xinteract(varlist:xintvarlist) specifies covariate interactions to include
in the regression. If unspecified the default is to interact all
the covariates in the model with treatment group, and group and time indicators.
Typically, xintvarlist is a strict subset of covarlist.

noxinteract specifies no covariate interactions in the regression, covariates are
only included in the regression additively. It cannot be used if xinteract(varlist:xintvarlist)
is specified.

usercohort(varname:cohortvar) specifies a user-defined cohort variable, overriding
the internal cohort variable calculated using tx(varlist:txvar) and time(varlist:timevar).
It is useful in situations where the internal cohort variable is incorrect or inappropriate,
e.g., when some time periods of observational-level data are missing.

verbose displays the output of the underlying OLS regression. Allows the user to see
the table of flexdid regression estimates.

SE/Robust

vce(vcetype) specifies the type of standard errors reported, which includes vce(cluster clustvar}
that allows for intragroup correlation, and vce(robust) that allows for heteroskedasticity.
vce(cluster groupvar} is the default.

Examples

Setup

. webuse hhabits

Estimate the FLEX regression of treatment, hhabit, on outcome body mass index, bmi,
for school districts by year; using the lags only specification. Reports the overall ATET.

. flexdid bmi, tx(hhabit) group(schools) time(year)

As above but include medu, sports and girl as covariates. Covariates are included additively
and as interactions with the treatment group, group and time indicators. Reports the overall ATET. Also displays the
underlying regression with the verbose option.

. flexdid bmi medu sports girl, tx(hhabit) group(schools) time(year) verbose

Estimate the FLEX regression of treatment, hhabit, on outcome body mass index, bmi
for school districts over year; using the lags and leads specification and
medu, sports and girl as covariates. Reports the overall ATET.

. flexdid bmi medu sports girl, tx(hhabit) group(schools) time(year)
specification(lagsandleads)

As above but treatment is estimated at the cohort by year level, txgroup(chrt), while
the regression fixed effects are estimated at the group level and the standard errors
are clustered at the group-level, group(schools). Reports the overall ATET.

Setup - manually create cohort variable

. egen chrt = min(year/hhabit), by(schools)

. replace chrt = 0 if chrt==.

. flexdid bmi, tx(hhabit) group(schools) txgroup(chrt) time(year)
vce(cluster schools) specification(lagsandleads)

As above with treatment estimated at the cohort by year level, regression fixed effects at the
cohort level and standard errors clustered at the group level. The cohort by year
effects are identical to those obtained using hdidregress ra

. flexdid bmi, tx(hhabit) group(chrt) time(year) vce(cluster schools)
specification(lagsandleads) verbose

. hdidregress ra (bmi) (hhabit), group(schools) time(year) basetime(common)

Stored results

flexdid stores the following in e():

Scalars

e(N) number of observations

e(sum_w) sum of weights

e(N_clust) number of clusters

e(rank) rank of e(V)

e(rss) residual sum of squares

e(mss) model sum of squares

e(df_r) residual degrees of freedom

e(df_m) model degrees of freedom

e(F) F statistic

e(ll) log likelihood, assuming i.i.d. normal errors

e(ll_0) log likelihood, constant-only model

Macros

e(cmd) flexdid

e(specification) model specification either lagsonly or lagsleads

e(cmdline) command as typed

e(predict) program used to implement predict

e(estat_cmd) program used to implement estat

e(model ols

e(depvar) name of outcome variable

e(tx) name of txvar, binary treatment variable

e(group) name of groupvar, group variable

e(time) name of timevar, time variable

e(txgroup) name of treatment group variable

e(usercohort) name of user-specified cohort variable

e(title) title in estimation output when vce() is not ols

e(vce) vcetype specified by vce()

e(clustvar) name of cluster variable

e(vcetype) title used to label Std. err.

e(wtype) weight type

e(wexp) weight expression

e(properties) b V

e(marginsok) predictions allowed by margins

Matrices

e(b) coefficient vector

e(V) variance-covariance matrix of the estimators

e(V_modelbased) model-based variance

Functions

e(sample) marks estimation sample

In addition to the above, the following is stored in r():

Macros

r(atettype) ATET option specified

Matrices

r(table) matrix containing the coefficients with their standard errors,
test statistics, p-values, and confidence intervals

r(b) coefficient vector

r(V) variance-covariance matrix of the estimators

In addition to the above, the following are added to the dataset:

Variables*

_Chrt internally generated cohort variable

_Grp internally generated group variable

_Tx internally generated treatment variable

*flexdid will overwrite variables in the dataset with the same names.
Therefore, any such variables should be dropped or renamed before using flexdid.

Authors

Partha Deb

Hunter College, CUNY and NBER

partha.deb@hunter.cuny.edu

Acknowledgments

The flexible regression (FLEX) that estimates average treatment effects on the treated (ATETs)
in difference-in-differences designs with staggered implementation is conceived, described and
implemented in Deb et al. (2025). Thus, without Edward Norton, Jeff Wooldridge and Jeff Zabel
there would be no flexdid. In addition, Anjelica Gangaram helped write the help file and
Jacky Wu helped with initial coding of the package.

References

Borusyak, K., Jaravel, X., & Spiess, J. (2024). "Revisiting Event-study Designs:
Robust and Efficient Estimation", Review of Economic Studies, 91(6), 3253-3285.

Callaway, B. and Sant'Anna, P. H. C. (2021). "Difference-in-Differences
with multiple time periods", Journal of Econometrics, 225(2):200-230.

Deb, P., Norton, E. C., Wooldridge, J. M., Zabel, J. E. (2025), "A Flexible,
Heterogeneous Treatment Effects Difference-in-Differences Estimator for Repeated
Cross-Sections", National Bureau of Economic Research, Working Paper Series, No. 33026.