$npx -y skills add RConsortium/pharma-skills --skill admiral-adaeDerives an ADaM Adverse Events Analysis Dataset (ADAE) using the {admiral} R package and pharmaverse ecosystem. Use when a user needs to create ADAE from SDTM AE and supporting domains, derive standard adverse event analysis variables (severity, seriousness, treatment-emergent fl
| 1 | # admiral-adae |
| 2 | |
| 3 | Derives a CDISC-conformant ADAE dataset using {admiral}. Outputs executable, |
| 4 | QC-ready R code with derivation logic traceable to the ADaM specification. |
| 5 | |
| 6 | The primary design challenge in ADAE is the treatment-emergent adverse event |
| 7 | (TEAE) flag (TRTEMFL) and its supporting date infrastructure. All date and study |
| 8 | day derivations must flow from this before any analysis variables are added. |
| 9 | |
| 10 | --- |
| 11 | |
| 12 | ## Inputs |
| 13 | |
| 14 | Before generating code, confirm the following are available or explicitly noted |
| 15 | as absent: |
| 16 | |
| 17 | | Input | Required | Notes | |
| 18 | |---|---|---| |
| 19 | | AE | Yes | One record per AE per subject; subject spine for ADAE | |
| 20 | | ADSL | Yes | Provides TRTSDT, TRTEDT, TRT01P/A, population flags | |
| 21 | | MH | No | Medical history; needed for pre-existing condition flag (PREFL) | |
| 22 | | CM | No | Concomitant medications; sometimes linked to AE causality | |
| 23 | | ADaM ADAE spec | Yes | Variable list, derivation rules, TEAE definition, grading rules | |
| 24 | | Study context | Yes | TEAE window definition, SMQ/grouping flag scope, severity scale | |
| 25 | |
| 26 | If AE or ADSL are absent, stop and request them. If optional domains are |
| 27 | absent, omit the corresponding derivations and note this in code comments. |
| 28 | |
| 29 | **Note on pharmaversesdtm test data:** The `pharmaversesdtm::ae` dataset does |
| 30 | not contain `AETOXGR`. Users running this skill against pharmaverse test data |
| 31 | should skip the AETOXGR derivation in Step 7. The derivation is retained in |
| 32 | the skill for use with real study data where NCI CTCAE grading was collected. |
| 33 | |
| 34 | **Critical ADSL dependency:** ADAE must merge a defined set of ADSL variables |
| 35 | onto every AE record. Confirm with the statistician which ADSL variables are |
| 36 | required — at minimum: TRTSDT, TRTEDT, TRTSDTM, TRT01P, TRT01PN, TRT01A, |
| 37 | TRT01AN, and all population flags in scope (SAFFL, ITTFL). |
| 38 | |
| 39 | --- |
| 40 | |
| 41 | ## Workflow |
| 42 | |
| 43 | Follow these steps in order. Generate code section by section, not as a single |
| 44 | block. |
| 45 | |
| 46 | ### Step 1 — Setup and domain loading |
| 47 | |
| 48 | ```r |
| 49 | library(admiral) |
| 50 | library(dplyr) |
| 51 | library(lubridate) |
| 52 | library(pharmaversesdtm) |
| 53 | |
| 54 | # Load SDTM domains |
| 55 | ae <- pharmaversesdtm::ae |
| 56 | adsl <- adsl # assumed derived upstream; replace with path/load as needed |
| 57 | # mh <- pharmaversesdtm::mh # uncomment if pre-existing condition flag in scope |
| 58 | |
| 59 | # Remove DOMAIN from AE to avoid variable conflicts in merges |
| 60 | ae <- ae |> select(-DOMAIN) |
| 61 | |
| 62 | # Confirm AE has at least one record |
| 63 | stopifnot(nrow(ae) > 0) |
| 64 | ``` |
| 65 | |
| 66 | ### Step 2 — Subject spine from AE |
| 67 | |
| 68 | ADAE is a one-record-per-AE dataset; the subject spine is AE itself. Start |
| 69 | here and add ADSL variables in the next step. |
| 70 | |
| 71 | ```r |
| 72 | adae <- ae |
| 73 | ``` |
| 74 | |
| 75 | ### Step 3 — Merge ADSL variables |
| 76 | |
| 77 | Merge a controlled subset of ADSL variables onto every AE record. Do not merge |
| 78 | all of ADSL — select only variables referenced in the ADAE derivation logic |
| 79 | and required for the output dataset per the ADaM spec. |
| 80 | |
| 81 | ```r |
| 82 | # REVIEW: Confirm which ADSL variables are required per the ADAE spec. |
| 83 | # The list below covers the minimum set for TEAE flag derivation and treatment |
| 84 | # labelling. Extend with population flags and other ADSL variables as needed. |
| 85 | adsl_vars <- exprs( |
| 86 | STUDYID, USUBJID, |
| 87 | TRTSDT, TRTEDT, TRTSDTM, |
| 88 | TRT01P, TRT01PN, TRT01A, TRT01AN, |
| 89 | SAFFL, ITTFL |
| 90 | ) |
| 91 | |
| 92 | adae <- adae |> |
| 93 | derive_vars_merged( |
| 94 | dataset_add = adsl |> select(!!!adsl_vars), |
| 95 | by_vars = exprs(STUDYID, USUBJID) |
| 96 | ) |
| 97 | ``` |
| 98 | |
| 99 | ### Step 4 — AE date variables (ASTDT, ASTDTF, AENDT, AENDTF) |
| 100 | |
| 101 | Derive analysis start and end dates from AE.AESTDTC and AE.AEENDTC. Always |
| 102 | use `derive_vars_dt()` — never `as.Date()` directly on DTC variables. |
| 103 | |
| 104 | Use `date_imputation = "first"` for start dates and `"last"` for end dates per |
| 105 | CDISC convention. Always retain imputation flag variables (ASTDTF, AENDTF). |
| 106 | |
| 107 | ```r |
| 108 | adae <- adae |> |
| 109 | derive_vars_dt( |
| 110 | dtc = AESTDTC, |
| 111 | new_vars_prefix = "AST", |
| 112 | date_imputation = "first", |
| 113 | flag_imputation = "auto" |
| 114 | ) |> |
| 115 | derive_vars_dt( |
| 116 | dtc = AEENDTC, |
| 117 | new_vars_prefix = "AEN", |
| 118 | date_imputation = "last", |
| 119 | flag_imputation = "auto" |
| 120 | ) |
| 121 | ``` |
| 122 | |
| 123 | ### Step 5 — Study day variables (ASTDY, AENDY) |
| 124 | |
| 125 | Use `derive_vars_dy()` relative to TRTSDT from ADSL. Do not compute study days |
| 126 | manually with date subtraction — this bypasses the Day 1 = first dose date |
| 127 | offset logic required |