Skip to content

Fix off-by-one in drug era gap_days calculation (#761) - #822

Open
DG47 wants to merge 2 commits into
OHDSI:mainfrom
DG47:fix/drug-era-gap-days
Open

DG47 wants to merge 2 commits into
OHDSI:mainfrom
DG47:fix/drug-era-gap-days

Conversation

@DG47

@DG47 DG47 commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #761

Second of two stacked PRs: this one depends on #824, which moves the drug era script into inst/sql/sql_server/OMOP_CDM_drug_era.sql unchanged and adds buildDrugEra(). Until #824 is merged, the diff here also shows its commit; the change in this PR is the last commit only.

The drug era script on the SQL Scripts page computes days_exposed and the era length as datediff(day, start, end), which counts an exposure from day 1 to day 5 as 4 days. Every sub-exposure in an era therefore contributes one spurious gap day: for the four exposures in the issue (days 1-5, 3-7, 11-13, 16-20) the script reports gap_days = 7 instead of 5, and two back-to-back exposures (days 1-5 and 6-10) report gap_days = 1 although the person is exposed every day.

Change

  • inst/sql/sql_server/OMOP_CDM_drug_era.sql: the two + 1 changes agreed in Drug era calculation gap_days calculation #761 (days_exposed and the era length are now counted inclusively). Nothing else in the script changes.
  • docs/sqlScripts.html: the same two lines, from re-rendering the page.
  • tests/testthat/test-buildDrugEra.R: the buildDrugEra() tests from Move the drug era script into its own SQL file and add buildDrugEra() #824 now also check gap_days (5 for the four exposures, 0 for back-to-back exposures, 0 for the single-exposure eras). Without the fix they fail for every CDM version, with 7 instead of 5 and 1 instead of 0.

The fallback end date dateadd(day, days_supply, start), used when drug_exposure_end_date is NULL, is still exclusive. I left it alone to keep to the agreed fix.

Testing

R CMD INSTALL --library=<tmp> .
testthat::test_dir("tests/testthat")   # 90 expectations passed, 0 failed, 1 test skipped (executeDdl needs database env vars)
R CMD check --no-manual               # Status: OK

@DG47

DG47 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

Closing and reopening to rerun CI: the ubuntu job failed on a transient curl error while installing dependencies (macOS and Windows passed).

@DG47 DG47 closed this Oct 29, 2026
@DG47 DG47 reopened this Oct 29, 2026
@DG47

DG47 commented Oct 29, 2026

Copy link
Copy Markdown
Contributor Author

For the record, the "Check for spelling errors" failure is not from this PR: codespell flags words inside extras/archive/OMOP_CDM_v6_0test.pdf and extras/archive/testSpec.html, and the same job fails on the two most recent pushes to main. A skip entry for extras/archive in a .codespellrc would clear it for every PR; happy to open that separately if useful.

@MaximMoinat

Copy link
Copy Markdown
Collaborator

@DG47 Thanks! I like that the drug era script is taken out as its own file. Next step would be to create an R function out of it, e.g. buildDrugEra. For transparency even better as two stacked PR: one just refactoring without changing the sql, one addressing the #761 issue.

DG47 added 2 commits October 30, 2026 16:22
The drug era example script on the SQL Scripts page now lives in
inst/sql/sql_server/OMOP_CDM_drug_era.sql, unchanged, and the page includes it from there
(engine: knitr, so the page renders the sql chunk without Jupyter).

buildDrugEra() renders that script for a CDM schema, translates it with SqlRender and runs it with
DatabaseConnector, like executeDdl(). The tests run it on a SQLite CDM for every supported CDM
version with the scenarios from OHDSI#761 and check the era dates and exposure counts.

docs/sqlScripts.html only has the changes from re-rendering the page.
days_exposed and the era length both counted the days between two dates, while drug exposure
start and end dates are inclusive, so every sub-exposure lost a day and gap_days came out too
high. Both now add 1, as agreed in OHDSI#761.

The buildDrugEra() tests now check gap_days as well; without this change they fail for every
CDM version (7 instead of 5 gap days in the first scenario, 1 instead of 0 for consecutive
exposures).
@DG47

DG47 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Thanks, will do. I have both ready locally. The first moves the script into its own file without touching the SQL and adds buildDrugEra(), with tests on SQLite for 5.3, 5.4 and 5.5 that check era dates and exposure counts. The second puts only the #761 change on top, the two + 1 lines plus gap_days assertions in the same tests, and I'd turn this PR into that one.

Before I push: buildDrugEra() takes connectionDetails and cdmDatabaseSchema like executeDdl(), plus tempEmulationSchema, because the script writes to a temp table. The script appends to DRUG_ERA, so calling buildDrugEra() twice duplicates the eras. Should the function clear DRUG_ERA first, or should that stay the caller's job?

@DG47

DG47 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Pushed both. #824 moves the drug era script into inst/sql/sql_server/OMOP_CDM_drug_era.sql unchanged and adds buildDrugEra(). This PR is now the second one: its last commit is the #761 change alone, the two + 1 lines and the gap_days assertions. Until #824 is merged the diff here also shows its commit.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Drug era calculation gap_days calculation

2 participants