Derives per‑patient hospital entry (hospital_in) and exit (hospital_out)
dates by reconciling A&E (ECDS) attendances and inpatient (HES/SUS) spells.
Applies a simple ranking to determine the most relevant hospital period around
an index event date (e.g., a specimen collection date).
Current admissions take priority
When conflicting on the same day, inpatient admissions take priority over A&E emergency care data
Where a patient has a linked A&E admission to a hospital inpatient stay, the A&E admission date is used
Where a patient has a positive test between two hospital stays the most recent completed hospital stay prior to the test is retained except if the time between these events is greater than 14 days, then the first admission following the test is retained
hospital_in_out_dates(
data,
person_id = "id",
hospital = list(org_code = "organisation_code_of_provider", event_date = "ev_date",
ae_arrive = "arrival_date", ae_depart = "departure_date", ae_discharge =
"ecds_discharge", in_spell_start = "spell_start_date", in_spell_end =
"spell_end_date", in_discharge = "discharge_destination")
)A data.table equal to data with additional columns:
hospital_inDerived hospital admission date for the relevant stay.
hospital_outDerived hospital discharge date for the relevant stay.
hospital_event_rankRank of suitability of the hospital window for the given person/event (1 = most suitable).
A linked table containing A&E and inpatient records (typically the
output of link_ae_inpatient()), including person/event identifiers and
date fields.
Quoted column name for the unique patient identifier.
A named list specifying column names (all quoted) for:
org_codeOrganisation code (optional; used to scope grouping).
event_dateIndex date to compare against (e.g., specimen_date).
ae_arriveECDS arrival date.
ae_departECDS departure date.
ae_dischargeECDS discharge status (use grouped values if available).
in_spell_startInpatient spell start date.
in_spell_endInpatient spell end date.
in_dischargeInpatient discharge destination (grouped recommended).
Use hospital_in_out_dates() after:
Linking A&E to inpatient spells (e.g., via link_ae_inpatient()),
Constructing spells (e.g., group_time() or cip_spells()),
Optional code standardisation (e.g., discharge groups
via lookup_recode())
epidm::lookup_recode()
epidm::group_time()
epidm::cip_spells()