/********************************************************************
HEAT-RELATED HOSPITAL DISCHARGES USING DAD

BY: Adriana Romero (Health Analyst at HPH)

IMPORTANT

This script assumes that the DAD extract has already been created in
IntelliHealth.

Before running this script, the IntelliHealth extract should:

• be restricted to the health unit of interest (PatientPHU);
• be restricted to the reporting year and month(s) of interest
  (using CYear and CMonth);
• include the AllDxCode4char field (i.e., all diagnosis codes for
  each hospitalization);
• exclude diagnosis records where AllDxPrefix begins with "Q",
  according to the indicator methodology; and
• include a unique DAD Key for each hospitalization.

This script does not perform these extraction steps. Instead, it
imports the IntelliHealth extract, identifies heat-related ICD-10-CA
diagnosis codes, applies the W92 exclusion rule, reviews qualifying
records, identifies hospitalizations with multiple diagnosis codes,
deduplicates records using the DAD Key, and calculates the final number
of unique heat-related hospital discharges.

W92 exclusion rule:

Hospitalizations containing W92 are excluded unless the same DAD Key
also contains X30 and/or X32.

********************************************************************/


/********************************************************************
SECTION 1: Import the Excel dataset

Purpose:
To import the DAD extract created in IntelliHealth.

Instructions:
Replace the example file path below with the location and name of the
Excel file containing the DAD extract.

The firstrow option uses the first row of the Excel file as the variable
names.

The clear option removes any dataset currently open in Stata.
********************************************************************/

* Import dataset (update file path as needed)
import excel using "YOUR_FILE_PATH\YOUR_FILE_NAME.xlsx", firstrow clear


/********************************************************************
SECTION 2: Review the imported dataset

Purpose:
To confirm that the data imported correctly before beginning the
analysis.

Why is this important?
Checking the dataset structure at the beginning helps identify problems
such as missing variables, incorrect variable names, or incomplete data
extracts before any analysis is performed.
********************************************************************/

describe
count


/********************************************************************
SECTION 3: Standardize the diagnosis codes

Purpose:
Diagnosis codes can sometimes contain lower-case letters or extra spaces.
Cleaning the codes ensures that all heat-related diagnoses are identified
consistently.

Expected result:
Diagnosis codes should be in uppercase with no leading or trailing
spaces.
********************************************************************/

replace AllDxCode4char = upper(strtrim(AllDxCode4char))


/********************************************************************
SECTION 4: Keep potentially heat-related ICD-10-CA diagnosis codes

Purpose:
To retain diagnosis records that may be required for the heat-related
hospitalization case definition.

Why is this important?
The DAD extract contains diagnosis records for many conditions. Keeping
only the relevant diagnosis codes reduces the dataset to the records
required for this indicator.

The following ICD-10-CA codes are included:

• T67.0–T67.9  Effects of heat and light
• X30          Exposure to excessive natural heat
• X32          Exposure to sunlight

W92 is also retained temporarily so that the exclusion rule can be
applied at the hospitalization level.

Hospitalizations containing W92 will only be included when the same
DAD Key also contains X30 and/or X32.
********************************************************************/

keep if ///
    substr(AllDxCode4char, 1, 3) == "T67" | ///
    AllDxCode4char == "X30"                  | ///
    AllDxCode4char == "X32"                  | ///
    AllDxCode4char == "W92"


/********************************************************************
SECTION 5: Review the remaining diagnosis codes

Purpose:
To verify that only the expected ICD-10-CA diagnosis codes remain in
the dataset.

Why is this important?
Reviewing the remaining diagnosis codes helps confirm that the filtering
step worked as expected and that no unexpected diagnosis codes were
retained.
********************************************************************/

count

tab AllDxCode4char, missing


/********************************************************************
SECTION 6: Identify hospitalizations with multiple relevant
diagnosis codes

Purpose:
To determine whether more than one relevant diagnosis code has been
assigned to the same hospitalization.

Why is this important?
The DAD extract is organized at the diagnosis-record level. This means
that a single hospitalization identified by DAD Key may appear more than
once when multiple relevant diagnosis codes were assigned.

These hospitalizations should only be counted once in the final result.
********************************************************************/

bysort DADKey: generate n_dx = _N

tab n_dx


/********************************************************************
SECTION 7: Review hospitalizations with multiple relevant
diagnosis codes

Purpose:
To review hospitalizations containing more than one relevant diagnosis
code.

Why is this important?
This quality assurance step confirms that repeated DAD Keys represent
multiple diagnosis codes assigned to the same hospitalization rather
than separate hospitalizations.
********************************************************************/

list DADKey AllDxCode4char if n_dx > 1, sepby(DADKey)


/********************************************************************
SECTION 8: Identify hospitalizations containing X30, X32, or W92

Purpose:
To determine whether each hospitalization contains X30, X32, or W92.

Why is this important?
The W92 exclusion recommendation requires hospitalizations containing
W92 to be excluded unless the same DAD Key also contains X30 and/or X32.

The max() function assigns a value of 1 when the code appears at least
once within the hospitalization and 0 when it does not.
********************************************************************/

bysort DADKey: egen has_x30 = ///
    max(AllDxCode4char == "X30")

bysort DADKey: egen has_x32 = ///
    max(AllDxCode4char == "X32")

bysort DADKey: egen has_w92 = ///
    max(AllDxCode4char == "W92")


/********************************************************************
SECTION 9: Review hospitalizations containing W92

Purpose:
To identify hospitalizations containing ICD-10-CA code W92 and determine
whether X30 and/or X32 are also present.

Why is this important?
According to the indicator methodology, hospitalizations containing W92
must be excluded unless the same DAD Key also contains:

• X30, exposure to excessive natural heat; and/or
• X32, exposure to sunlight.

A hospitalization containing W92 and T67, but no X30 or X32, must still
be excluded.
********************************************************************/

list DADKey AllDxCode4char has_x30 has_x32 has_w92 ///
    if has_w92 == 1, sepby(DADKey)


/********************************************************************
SECTION 10: Apply the W92 exclusion rule

Purpose:
To exclude hospitalizations containing W92 unless the same DAD Key also
contains X30 and/or X32.

The following hospitalizations are excluded:

• W92 only; and
• W92 with T67 but without X30 or X32.

The following hospitalizations are included:

• T67 without W92;
• X30 without W92;
• X32 without W92;
• W92 with X30;
• W92 with X32; and
• W92 with both X30 and X32.
********************************************************************/

drop if has_w92 == 1 & has_x30 == 0 & has_x32 == 0


/********************************************************************
SECTION 11: Review the records remaining after applying the W92 rule

Purpose:
To confirm that the W92 exclusion rule was applied correctly.

Any W92 records remaining in the dataset should belong to a DAD Key
that also contains X30 and/or X32.
********************************************************************/

count

tab AllDxCode4char, missing

list DADKey AllDxCode4char has_x30 has_x32 ///
    if has_w92 == 1, sepby(DADKey)


/********************************************************************
SECTION 12: Count unique heat-related hospitalizations

Purpose:
To calculate the number of unique heat-related hospital discharges.

Why is this important?
Because one hospitalization may contain more than one relevant diagnosis
code, the final indicator should be based on unique DAD Keys rather than
the number of diagnosis records.

The tag() function assigns a value of 1 to the first occurrence of each
unique DAD Key. Counting only these tagged records provides the number
of unique heat-related hospitalizations.
********************************************************************/

egen tag = tag(DADKey)

count if tag == 1

local final_count = r(N)


/********************************************************************
SECTION 13: Display the final result

Purpose:
To clearly display the final number of unique heat-related hospital
discharges in the Stata Results window.
********************************************************************/

display "Final number of unique heat-related hospital discharges: " r(N)


/********************************************************************
INTERPRETING THE RESULTS

The final count represents the number of unique hospital discharge
records that met the heat-related hospitalization definition.

Each hospitalization is counted only once, even when multiple relevant
ICD-10-CA diagnosis codes were assigned to the same hospitalization.

Hospitalizations containing W92 were excluded unless the same DAD Key
also contained X30 and/or X32.


This value can be used as the annual or reporting-period count of
heat-related hospital discharges for the selected health unit.
********************************************************************/
