Written and maintained by CASRAI Editorial Board
Published
On this page
csdid is a community-contributed Stata command that estimates difference-in-differences effects when units are treated at different times, following the Callaway and Sant’Anna (2021) estimator. It computes every feasible two-group, two-period comparison (a group-time average treatment effect, ATT(g,t)) using only never-treated or not-yet-treated units as controls, then lets you aggregate those effects into overall, cohort, calendar-time and event-study summaries. The syntax is csdid depvar [indepvars], time(varname) gvar(varname), with ivar(varname) added for panel data.
What csdid is used for in research
Researchers use csdid when a policy, programme or intervention rolls out in waves, so that some units are treated in 2004, others in 2006 and others never. The classic two-way fixed-effects regression can compare late-treated units with earlier-treated ones, and the csdid help file explains why that matters: with heterogeneous treatment effects, such comparisons violate the parallel-trends assumption and the estimated effects can be severely biased. csdid avoids those comparisons by using only never-treated or not-yet-treated units as controls.
The package description on the Statistical Software Components (SSC) archive records the same idea: csdid implements the Callaway and Sant’Anna estimator, uses the drdid estimators for every 2×2 design, and can be used with panel data and with repeated cross-sections. For the identifying assumptions themselves (parallel trends, no anticipation, what staggered adoption does to ordinary regressions) read the site’s guide on difference-in-differences, parallel trends and staggered adoption first; this page covers the Stata mechanics.
Two design questions decide whether csdid fits your data. Do you follow the same units over time? If so, supply ivar() and csdid uses panel estimators; if you omit it, csdid treats the data as repeated cross-sections and applies repeated cross-section estimators. And do you have a clean cohort variable? Every unit needs a value in gvar() that says when it was first treated, as described in the steps below.
How to install or access csdid
csdid is not part of official Stata; it is a user-written package by Fernando Rios-Avila, Pedro H. C. Sant’Anna and Brantly Callaway. The package README gives this installation command, to be typed in the Stata command window:
net install csdid, from("https://raw.githubusercontent.com/friosavila/csdid_drdid/main/code/") replace
That repository is marked as moved: its README says the authors are relocating csdid to the stpackages repository (github.com/friosavila/stpackages, folder csdid) and drdid to the sibling folder drdid. The same README describes that release as version 0.1 (2021-05-22) and a beta, so check the help files in the newer repository for the current option list. The package also has an SSC record, number S458976, dated 2021-08-04, titled “CSDID: Stata module for the estimation of Difference-in-Difference models with multiple time periods”.
After installing, open the documentation with help csdid and help csdid postestimation. The help file states that csdid generalises drdid and works as a wrapper: all 2×2 comparisons are obtained internally with drdid, so install the drdid package from the same repository as well.
If you work in a shared project, record the install source and date in your do-file header so that collaborators run the same version; the site’s guides on structuring Stata do-files for a reproducible workflow and on Stata code cover that habit.
How to estimate staggered difference-in-differences with csdid step by step
The help file’s own example uses the mpdta teen-employment panel (outcome lemp, covariate lpop, unit identifier countyreal, time year, first-treatment year first_treat). The numbered steps below follow that example so you can run it unchanged, then swap in your own variable names.
- Load the data.
use https://friosavila.github.io/playingwithstata/drdid/mpdta.dta, clear - Check the cohort variable. The
gvar()variable must code never-treated units as 0 and give every treated unit the first period in which it was treated; the help file’s illustration is 0 for never treated, 10 for treated at t=10, 20 for treated at t=20. Once a group is treated, the estimator assumes it stays treated. Tabulate it (tabulate first_treat) and confirm each treated cohort also appears in your time variable. - Check the time variable. The help file says periods need not be consecutive, but
time()should be strictly positive with regular gaps. - Estimate all group-time effects.
csdid lemp lpop, ivar(countyreal) time(year) gvar(first_treat) method(dripw). Thedripwmethod is the default, somethod(dripw)only makes the choice explicit. - Request event-study results. Add an aggregation to the estimation call:
csdid lemp lpop, ivar(countyreal) time(year) gvar(first_treat) method(dripw) agg(event). Theagg()option acceptsattgt(the default),simple,group,calendarandevent. - Use not-yet-treated units as controls when you want them. Add
notyet. By default csdid uses never-treated units only; if there are none, it switches tonotyetautomatically. - Add wild-bootstrap standard errors. Add
wboot rseed(1)(the help file’s example seed). The default is 999 repetitions with the Mammen weights;reps(#)changes the count andwtypecan select the Rademacher weights. - Run the post-estimation commands. After a plain
csdidcall,estat pretrend,estat simple,estat group,estat calendar,estat eventandestat allproduce the pre-trend test and the aggregations;estat event, window(-2 2)limits the event study to periods -2 through +2 relative to first treatment. - Plot.
csdid_plotdraws the most recent result;csdid_plot, style(rarea)changes the style, andcsdid_plot, group(2006)draws the dynamic effects for the cohort first treated in 2006 after a plain csdid call.
The covariate rule
Covariates are used only at their base-period (earlier-period) values, even if the variable changes over time. The help file’s advice is therefore to control for characteristics that are time-constant (it names sex and race as examples) or that were measured before treatment. A covariate that responds to treatment does not belong in indepvars.
Choosing the estimation method
The method() option selects the 2×2 estimator. The table in the data section below lists the keywords as the help file defines them. In short: dripw (Sant’Anna and Zhao 2020 doubly robust estimator based on stabilised inverse probability weighting and ordinary least squares) is the default; drimp is their improved doubly robust estimator; reg is the outcome-regression estimator and is used by default when no covariates are listed because it gives the same point estimates and standard errors there; rc1 is a repeated cross-section variant and is not available with panel data.
Pre-treatment gaps and replicating R
For post-treatment effects csdid uses the period just before treatment (G-1) as the base period. For pre-treatment effects it defaults to short gaps, comparing T-1 with T. The long option requests long gaps instead, and long2 gives the same estimates with the sign flipped, which the help file calls the closest match to standard event-study effects. If you need to reproduce the R did package for not-yet-treated pre-treatment effects, use asinr: the help file explains that R uses all not-yet-treated cohorts while Stata by default uses all cohorts not treated at time G. The pscoretrim(#) option trims control observations with extreme propensity scores; its default is 0.995, matching R, and the help file flags it as a new option.
How to read the csdid output
While csdid runs, it prints one symbol per group-time estimate: a “.” for every ATT(g,t) estimated successfully and an “x” where the estimation failed. A screen of dots is the good case; an “x” means that particular cohort-period comparison could not be estimated, usually for the data reasons listed in the troubleshooting section.
The results table lists each ATT(g,t): the average effect of treatment for cohort g in period t. Pre-treatment rows should be close to zero if parallel trends is plausible; post-treatment rows are the effects of interest. Confidence intervals are 95 percent by default, and level(#) changes that. With wboot, intervals are uniform by default and pointwise requests pointwise intervals instead. By default, standard errors are robust and asymptotic, based on influence functions; cluster(varname) clusters them, and with panel estimators they are already clustered at the panel level, so adding cluster() then amounts to two-way clustering with the panel identifier nested in the cluster variable.
Aggregations
- simple: the ATT for all groups across all periods.
- group: the ATT for each cohort, across all periods.
- calendar: the ATT for each period, across all cohorts.
- event: dynamic effects for each period relative to first treatment, across all cohorts.
The post-estimation help adds cevent, window(#1 #2), a censored event average across the periods from #1 to #2 inclusive; estat cevent, window(0 0) reproduces the effect at T+0.
The pre-trend test
estat pretrend reports a chi-squared statistic for the null hypothesis that all pre-treatment ATT(g,t) are equal to zero; estat pretrend, window(#1 #2) restricts it to a window. A rejection is evidence against parallel trends in the sample, but a failure to reject does not prove the assumption, so keep the event-study plot in view as well.
The wild-bootstrap caveat
This catches many users. Even when csdid was run with wboot, asymptotic standard errors are what the program stores in e(), and estat subcommands always return asymptotic standard errors. To get wild-bootstrap standard errors for other aggregations, save the influence-function file when you estimate and then use csdid_stats:
csdid lemp lpop, ivar(countyreal) time(year) gvar(first_treat) method(dripw) saverif(_rif_) replaceuse _rif_, clearcsdid_stats event, wboot
The wild bootstrap in csdid_stats takes wboot(reps(#)), wboot(wtype(type)) and rseed(#); csdid_rif builds tables from saved influence-function variables and supports cluster(), level(), wboot, reps() and seed().
Troubleshooting csdid
Cohorts and periods that make an ATT(g,t) impossible
- No pre-treatment period for a cohort. Each cohort needs data in the period before its treatment. With annual data and a group treated in 2010, the group needs 2009 data; otherwise its effects cannot be identified.
- Always-treated groups disappear. Groups treated before the first available period are treated as always treated and excluded from the sample.
- A cohort value that is not in the time variable. For every cohort in
gvar()there must be a matching period intime(), or the command errors. - Units that change cohort. In panel data, observations cannot change cohort across time.
- A never-treated group coded with a non-zero value. Never-treated units must be coded 0; any positive value is read as a first-treatment period.
Unbalanced panels
The estimator does not require a strongly balanced panel, but each ATT(g,t) is computed only from observations balanced within its 2×2 design, and csdid warns you when it detects this. That differs from the default in R’s did, which switches to repeated cross-section estimators for unbalanced data. The help file’s example estimates an unbalanced panel two ways: with panel estimators by keeping ivar(), and with repeated cross-section estimators and clustered errors by dropping ivar() and adding cluster(countyreal).
Memory and run time
csdid tries every feasible ATT(g,t), so many cohorts and many periods multiply quickly. The help file’s example is 30 periods and 20 potential groups, which requires 600 separate regressions and at least as many new variables for the influence functions, quickly using a typical computer’s memory. Its advice is to refine the treatment cohorts, because not all may matter for your question, and to keep only the periods you need, for example a few before the first treatment year.
Results that differ from R
Differences with the R did package usually come from the options above: the control group (notyet versus never-treated), the base period (long, long2), how not-yet-treated pre-treatment comparisons are built (asinr), unbalanced-panel handling and the propensity-score trim (pscoretrim). Align those options before concluding the two implementations disagree. For a broader comparison of the two environments, see the site’s comparison of R and Stata for statistical analysis.
Free alternatives to csdid
The table below lists tools that implement the same estimator or its building block, using only what their own documentation says.
| Tool | Environment | What it does | Source |
|---|---|---|---|
| did | R (CRAN package) | Computes group-time average treatment effects with the Callaway and Sant’Anna (2021) methods; aggregates them; tests and plots; GPL-3 licence; version 2.5.1 published 2026-07-08 | CRAN: did |
| drdid | Stata | The doubly robust 2×2 difference-in-differences estimators that csdid calls internally; installed from the same repository as csdid | csdid_drdid README |
The R did package states that its authors are Brantly Callaway and Pedro H. C. Sant’Anna, two of the three authors of csdid, and the csdid help file acknowledges that the Stata command was built using R’s DID command as a benchmark. If you can work in R, the two are designed to be comparable; the options table in the data section shows where defaults differ.
Whichever tool you use, cite the methods papers as the csdid help file asks: Callaway and Sant’Anna (2021), Journal of Econometrics 225(2):200-230, and Sant’Anna and Zhao (2020), Journal of Econometrics 219(1):101-22, along with the software package itself.
Sources
- csdid help file, Rios-Avila, Sant’Anna and Callaway (stpackages repository, read 2026-11-17)
- csdid postestimation help file, version 2.1, 2/23/2023 (read 2026-11-17)
- csdid_drdid GitHub README (installation command and relocation notice, read 2026-11-17)
- CSDID on RePEc/IDEAS, Statistical Software Components S458976, Boston College Department of Economics (read 2026-11-17)
- did: Treatment Effects with Multiple Periods and Groups, CRAN (read 2026-11-17)
Data: csdid method() and key options: definitions from the Stata help file
Compiled from the csdid help file and postestimation help file (stpackages repository): the 2x2 estimator keywords of method() and the main control-group, base-period, standard-error and aggregation options, with their stated defaults.
| Option or keyword | Group | What the help file says it does | Stated default |
|---|---|---|---|
| method(dripw) | Estimator | Sant'Anna and Zhao (2020) doubly robust DiD estimator using stabilised inverse probability weighting and ordinary least squares | Default when covariates are given |
| method(drimp) | Estimator | Sant'Anna and Zhao (2020) improved doubly robust estimator using inverse probability of tilting and weighted least squares | No |
| method(reg) | Estimator | Outcome-regression DiD estimator based on ordinary least squares | Default when no covariates are given |
| method(stdipw) | Estimator | Inverse probability weighting DiD estimator with stabilised weights | No |
| method(ipw) | Estimator | Abadie (2005) inverse probability weighting estimator; may not perform well with unbalanced panel data | No |
| method(rc1) | Estimator | With drimp or dripw, doubly robust but not locally efficient repeated cross-section estimators; not available with panel data | No |
| notyet | Control group | Use never-treated and not-yet-treated observations as controls | Never-treated only; notyet applies automatically if no never-treated units exist |
| long | Base period | Estimate long rather than short gaps for pre-treatment periods | Short gaps |
| long2 | Base period | Same as long with the sign flipped; closest to standard event-study effects | Short gaps |
| asinr | R replication | Use all not-yet-treated cohorts for pre-treatment not-yet-treated ATTGTs, as the R DID package does | Stata uses cohorts not treated at time G |
| pscoretrim(#) | Estimator | Trim control observations with extreme propensity score values | 0.995 |
| wboot | Standard errors | Multiplicative wild-bootstrap standard errors; reps(#) sets repetitions, wtype can select Rademacher weights | Off: robust asymptotic standard errors via influence functions; 999 repetitions with Mammen weights when on |
| agg(aggtype) | Aggregation | Aggregation shown as command output: attgt, simple, group, calendar or event | attgt |
Sources
- csdid help file (stpackages repository) (accessed 2026-11-17)
- csdid postestimation help file, version 2.1 (accessed 2026-11-17)
Compiled by CASRAI from the sources above. Free to reuse under CC BY 4.0 with attribution.







