Skip to main content

Code

To write a study using OpenSAFELY:

  1. Read the documentation, in particular the Analysis Workflow
  2. Use the Getting Started guide to get set up with GitHub and Codespaces, and write a “toy” study

After that, the ehrQL tutorial includes more in-depth guidance on writing a complex dataset definition (the piece of code that defines your study population, and adds variables of interest).

We recommend using the research template to set up your study repository, and using other OpenSAFELY studies for help. You can find other OpenSAFELY studies within our OpenSAFELY GitHub organisation.

Most studies involve at least one codelist, which is a list of codes for clinical events or demographic characteristics. You can create and share codelists using OpenCodelists. We have created and shared over 370 codelists, which we have used in over 20 published papers.

OpenSAFELY has many components; the most important are the command-line interface; ehrQL (the Electronic Health Record Query Language); and the job runner/job server.

  • Use the command-line interface to control the other components. For example, running opensafely codelists update updates your study’s codelists to match those at OpenCodelists.
  • Use ehrQL to generate fake, or “dummy”, data when writing your study. Real data will be used when your study is run in a secure environment.
  • Submit jobs on the OpenSAFELY Jobs website to run your study code in a secure environment.

You can find the code for these components, and more, within our OpenSAFELY Core GitHub organisation.