% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/derive.R
\name{derive_hes}
\alias{derive_hes}
\title{Derive a binary disease flag from UKB HES inpatient diagnoses}
\usage{
derive_hes(
  data,
  name,
  icd10,
  match = c("prefix", "exact", "regex"),
  disease_cols = NULL,
  date_cols = NULL
)
}
\arguments{
\item{data}{(data.frame or data.table) UKB phenotype data containing HES
fields (\code{p41270} and \code{p41280_a*}).}

\item{name}{(character) Output column prefix, e.g. \code{"disease"} produces
\code{disease_hes} and \code{disease_hes_date}.}

\item{icd10}{(character) ICD-10 code(s) to match.  For \code{"prefix"} and
\code{"exact"}, supply a vector such as \code{c("L20", "L21")}.  For
\code{"regex"}, supply a single regex string.}

\item{match}{(character) Matching strategy: \code{"prefix"} (default)
matches any code starting with the supplied string; \code{"exact"}
requires a full match; \code{"regex"} uses \code{icd10} directly.}

\item{disease_cols}{(character or NULL) Name of the \code{p41270} column.
\code{NULL} = auto-detect.}

\item{date_cols}{(character or NULL) Names of \code{p41280_a*} columns.
\code{NULL} = auto-detect.}
}
\value{
The input \code{data} with two new columns added in-place:
\code{{name}_hes} (logical) and \code{{name}_hes_date} (IDate).
Always returns a \code{data.table}.
}
\description{
Hospital Episode Statistics (HES) inpatient records store ICD-10 diagnosis
codes in field \code{p41270} (single JSON-array column on UKB RAP) and
corresponding first-diagnosis dates in field \code{p41280}
(\code{p41280_a0}, \code{p41280_a1}, \ldots).  The array index in
\code{p41270} and \code{p41280} are aligned: the \emph{N}-th code in the
JSON array corresponds to \code{p41280_aN} (date of first in-patient
diagnosis for that code).
}
\details{
\describe{
\item{\code{{name}_hes}}{Logical flag: \code{TRUE} if any HES record
contains a matching ICD-10 code.}
\item{\code{{name}_hes_date}}{Earliest first-diagnosis date across all
matching codes (\code{IDate}).  \code{NA} if no date is available.}
}
}
\examples{
\donttest{
dt <- ops_toy(n = 100)
derive_hes(dt, name = "htn",      icd10 = "I10")
derive_hes(dt, name = "diabetes", icd10 = c("E10", "E11"))
derive_hes(dt, name = "asthma",   icd10 = "^J4", match = "regex")
}
}
