-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstratifiedsql.sql
More file actions
27 lines (23 loc) · 998 Bytes
/
Copy pathstratifiedsql.sql
File metadata and controls
27 lines (23 loc) · 998 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
-- 1. Create a clean, indexed base view or table combining records
DROP TABLE IF EXISTS #RenalBaseCohort;
SELECT
p.patient_id,
p.cohort,
-- Ensure variables are standardized categorical strings
ISNULL(p.baseline_mental_health, 'None/Minimal') AS baseline_mental_health,
ISNULL(p.baseline_bp, 'Normal') AS baseline_bp,
ISNULL(p.baseline_dm, 'No') AS baseline_dm,
ISNULL(p.imd_quintile, '5') AS imd_quintile,
-- Longitudinal tracking variables
l.observation_time,
l.eGFR,
-- Survival/Competing Risk variables
p.survival_time,
p.event_status -- 0=Censored, 1=ESKD, 2=Pre-ESKD Death
INTO #RenalBaseCohort
FROM PatientsBaselineTable p
JOIN eGFRLabRecords l ON p.patient_id = l.patient_id
WHERE l.eGFR BETWEEN 5 AND 200 -- Remove extreme data errors
AND p.survival_time > 0;
-- Index the temporary table to maximize performance for the next steps
CREATE CLUSTERED INDEX IX_Patient_Time ON #RenalBaseCohort(patient_id, observation_time);