library AdvancedIllnessandFrailtyExclusionQICore4 version '0.3.000' using QICore version '4.1.1' include FHIRHelpers version '4.2.000' called FHIRHelpers include QICoreCommon version '1.1.000' called QICoreCommon include CumulativeMedicationDurationQICore4 version '0.3.000' called CMD include StatusFHIR version '0.1.000' called Status include TestMATGlobalCommonFunctionsQICore4 version '0.1.000' called Global codesystem "LOINC": 'http://loinc.org' codesystem "SNOMEDCT": 'http://snomed.info/sct' valueset "Acute Inpatient": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.101.12.1083' valueset "Advanced Illness": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.110.12.1082' valueset "Dementia Medications": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.196.12.1510' valueset "Emergency Department Visit": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.101.12.1010' valueset "Frailty Device": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.118.12.1300' valueset "Frailty Diagnosis": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.113.12.1074' valueset "Frailty Encounter": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.101.12.1088' valueset "Frailty Symptom": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.113.12.1075' valueset "Nonacute Inpatient": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.101.12.1084' valueset "Observation": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.101.12.1086' valueset "Outpatient": 'http://cts.nlm.nih.gov/fhir/ValueSet/2.16.840.1.113883.3.464.1003.101.12.1087' code "Housing status": '71802-3' from "LOINC" display 'Housing status' code "Lives in a nursing home (finding)": '160734000' from "SNOMEDCT" display 'Lives in a nursing home (finding)' code "Medical equipment used": '98181-1' from "LOINC" display 'Medical equipment used' parameter "Measurement Period" Intervaldefault Interval[@2023-01-01T00:00:00.000Z, @2024-01-01T00:00:00.000Z ) context Patient define "Has Criteria Indicating Frailty": exists ( ( Status."Completed or Ongoing Device Request" ( [DeviceRequest: code in "Frailty Device"] ) ) FrailtyDeviceOrder where QICoreCommon."ToInterval" ( FrailtyDeviceOrder.authoredOn ) during day of "Measurement Period" ) or exists ( ( Status."Final Survey Observation" ( [Observation: "Medical equipment used"] ) ) FrailtyDeviceApplied where FrailtyDeviceApplied.value as Concept in "Frailty Device" and QICoreCommon."ToInterval" ( FrailtyDeviceApplied.effective ) ends during day of "Measurement Period" ) or exists ( [Condition: "Frailty Diagnosis"] FrailtyDiagnosis where QICoreCommon."ToPrevalenceInterval" ( FrailtyDiagnosis ) overlaps "Measurement Period" ) or exists ( ( Status."Finished Encounter" ( [Encounter: "Frailty Encounter"] ) ) FrailtyEncounter where QICoreCommon."ToInterval" ( FrailtyEncounter.period ) overlaps "Measurement Period" ) or exists ( ( Status."Initial or Final Observation" ( [Observation: "Frailty Symptom"] ) ) FrailtySymptom where QICoreCommon."ToInterval" ( FrailtySymptom.effective ) overlaps "Measurement Period" ) define "Is Age 66 or Older with Advanced Illness and Frailty": CalculateAgeInYearsAt(Patient.birthDate, date from end of "Measurement Period" ) in Interval[66, 80] and "Has Criteria Indicating Frailty" and ( "Has Two Outpatient Encounters with Advanced Illness on Different Dates of Service" or "Has Inpatient Encounter with Advanced Illness" ) define "Outpatient Encounters with Advanced Illness": ( Status."Finished Encounter" ( [Encounter: "Outpatient"] union [Encounter: "Observation"] union [Encounter: "Emergency Department Visit"] union [Encounter: "Nonacute Inpatient"] ) ) OutpatientEncounter where exists ( ( Global.EncounterDiagnosis ( OutpatientEncounter ) ) Diagnosis where Diagnosis.code in "Advanced Illness" ) and QICoreCommon."ToInterval" ( OutpatientEncounter.period ) starts during day of Interval[start of "Measurement Period" - 1 year, end of "Measurement Period"] define "Has Two Outpatient Encounters with Advanced Illness on Different Dates of Service": exists ( from "Outpatient Encounters with Advanced Illness" OutpatientEncounter1, "Outpatient Encounters with Advanced Illness" OutpatientEncounter2 where OutpatientEncounter2.period ends 1 day or more after day of end of OutpatientEncounter1.period return OutpatientEncounter1 ) define "Has Inpatient Encounter with Advanced Illness": exists ( ( Status."Finished Encounter" ( [Encounter: "Acute Inpatient"] ) ) InpatientEncounter where exists ( ( Global.EncounterDiagnosis ( InpatientEncounter ) ) Diagnosis where Diagnosis.code in "Advanced Illness" ) and QICoreCommon."ToInterval" ( InpatientEncounter.period ) starts during day of Interval[start of "Measurement Period" - 1 year, end of "Measurement Period"] ) define "Is Age 66 or Older Living Long Term in a Nursing Home": AgeInYearsAt(date from end of "Measurement Period" )>= 66 // CalculateAgeInYearsAt(Patient.birthDate, date from // end of "Measurement Period" // ) >= 66 // and ( ( Last((Status."Final Survey Observation"([Observation: "Housing status"])) HousingStatus // where QICoreCommon."ToInterval"(HousingStatus.effective) ends on or before // end of "Measurement Period" // sort by // end of QICoreCommon."ToInterval"(effective) asc // ) ) LastHousingStatus // where LastHousingStatus.value ~ "Lives in a nursing home (finding)" // ) is not null