-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRESPOND.sas
1516 lines (1302 loc) · 48.6 KB
/
RESPOND.sas
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*==============================*/
/* Project: RESPOND */
/* Author: Ryan O'Dea */
/* Created: 4/27/2023 */
/* Updated: 6/24/2024 */
/*==============================*/
/*
Overall, the logic behind the known capture is fairly simple:
search through individual databases and flag if an ICD9, ICD10,
CPT, NDC, or other specialized code matches our lookup table.
If a record has one of these codes, it is 'flagged' for OUD.
The utilized databases are then joined onto the SPINE demographics
dataset and if the sum of flags is greater than zero, then the
record is flagged with OUD.
At current iteration, data being pulled through this method is
stratified by Year (or Year and Month), Race, Sex, and Age
(where age groups are defined in the table below).
*/
/*==============================*/
/* GLOBAL VARIABLES */
/*==============================*/
%LET year = (2015:2022);
%LET MOUD_leniency = 7;
%LET today = %sysfunc(today(), date9.);
%LET formatted_date = %sysfunc(translate(&today, %str(_), %str(/)));
/*===========AGE================*/
PROC FORMAT;
VALUE age_grps_five
low-5 = '1' 6-10 = '2'
11-15 = '3' 16-20 = '4'
21-25 = '5' 26-30 = '6'
31-35 = '7' 36-40 = '8'
41-45 = '9' 46-50 = '10'
51-55 = '11' 56-60 = '12'
61-65 = '13' 66-70 = '14'
71-75 = '15' 76-80 = '16'
81-85 = '17' 86-90 = '18'
91-95 = '19' 96-998 = '20'
999 = '999';
PROC FORMAT;
VALUE age_grps_twenty
low-20 = '1' 21-40 = '2'
41-60 = '3' 61-80 = '4'
81-998 = '5' 999 = '999';
/*========ICD CODES=============*/
%LET ICD = ('30400','30401','30402','30403',
'30470','30471','30472','30473',
'30550','30551','30552','30553', /* ICD9 */
'F1110','F1111','F11120','F11121',
'F11122','F11129','F1113','F1114',
'F11150','F11151','F11159','F11181',
'F11182','F11188','F1119','F1120',
'F1121','F11220','F11221','F11222',
'F11229','F1123','F1124','F11250',
'F11251','F11259','F11281','F11282',
'F11288','F1129','F1193','F1199', /* ICD10 */
'9701','96500','96501','96502',
'96509','E8500','E8501','E8502',
'T400X1A','T400X2A','T400X3A','T400X4A',
'T400X1D','T400X2D','T400X3D','T400X4D',
'T401X1A','T401X2A','T401X3A','T401X4A',
'T401X1D','T401X2D','T401X3D','T401X4D',
'T402X1A','T402X2A','T402X3A','T402X4A',
'T402X1D','T402X2D','T402X3D','T402X4D',
'T403X1A','T403X2A','T403X3A','T403X4A',
'T403X1D','T403X2D','T403X3D','T403X4D',
'T404X1A','T404X2A','T404X3A','T404X4A',
'T404X1D','T404X2D','T404X3D','T404X4D',
'T40601A','T40601D','T40602A','T40602D',
'T40603A','T40603D','T40604A','T40604D',
'T40691A','T40692A','T40693A','T40694A',
'T40691D','T40692D','T40693D','T40694D', /* Overdose Codes */
'G2067','G2068','G2069','G2070','G2071',
'G2072','G2074','G2075','G2076','G2077',
'G2078','G2079','G2080','H0020','HZ81ZZZ',
'HZ91ZZZ','HZ94ZZZ','J0570','J0571','J0572',
'J0573','J0574','J0575','J2315','Q9991',
'Q9992','S0109', /* MOUD */
'F1193','F1199'/* Additional RESPOND */);
%LET PROC = ('G2067','G2068','G2069','G2070',
'G2071','G2072','G2073','G2074',
'G2075', /* MAT Opioid */
'G2076','G2077','G2078','G2079',
'G2080','G2081', /*Opioid Trt */
'J0570','J0571','J0572','J0573',
'J0574','J0575','J0592','S0109',
'G2215','G2216','G1028', /* Naloxone*/
'Q9991','Q9992','H0020','HZ81ZZZ','HZ91ZZZ');
%LET bsas_drugs = (5,6,7,21,22,23,24,26);
/*
Take NDC codes where buprenorphine has been identified,
insert them into BUP_NDC as a macro variable
*/
PROC SQL;
CREATE TABLE ndc AS
SELECT DISTINCT NDC
FROM PHDPMP.PMP
WHERE BUP_CAT_PMP = 1;
QUIT;
PROC SQL noprint;
SELECT quote(trim(ndc), "'") into :BUP_NDC separated by ','
FROM ndc;
QUIT;
/*===============================*/
/* DATA PULL */
/*===============================*/
/*======DEMOGRAPHIC DATA=========*/
/*
Using data from DEMO, take the cartesian coordinate of years
(as defined above) and months 1:12 to construct a shell table
*/
DATA demographics;
SET PHDSPINE.DEMO (KEEP= ID FINAL_RE FINAL_SEX YOB);
IF FINAL_RE = 9 THEN DELETE;
IF FINAL_RE = 99 THEN DELETE;
IF FINAL_SEX = 9 THEN DELETE;
IF FINAL_SEX = 99 THEN DELETE;
RUN;
%let start_year=%scan(%substr(&year,2,%length(&year)-2),1,':');
%let end_year=%scan(%substr(&year,2,%length(&year)-2),2,':');
DATA months; DO month = 1 to 12; OUTPUT; END; RUN;
DATA years; DO year = &start_year to &end_year; OUTPUT; END; RUN;
PROC SQL;
CREATE TABLE demographics_monthly AS
SELECT * FROM demographics, months, years;
QUIT;
PROC SQL;
CREATE TABLE demographics_yearly AS
SELECT * FROM demographics, years;
QUIT;
/*=========APCD DATA=============*/
/*
The APCD consists of the Medical and Pharmacy Claims datasets and,
along with Casemix, are the datasets where we primarily search along
our ICD code list. We construct a variable named `OUD_APCD` within our
APCD Medical dataset using `MED_ICD1-25`, `MED_PROC1-7`, `MED_ECODE`, `MED_ADM_DIAGNOSIS`
and `MED_DIS_DIAGNOSIS`. We preform a rowwise search and add one to a
temporary `count` variable if they appear within our ICD code list.
At the end, if the `count` variable is strictly greater than one
then our `OUD_APCD` flag is set to 1.
The APCD medical dataset does not hold variables for searching
for NDC Codes, so we add in the APCD pharmacy dataset with
`PHARM_NDC` to search for applicable NDC codes.
If `PHARM_NDC` or `PHARM_ICD` is within our OUD Codes lists above,
then our `OUD_PHARM` flag is set to 1.
*/
DATA apcd (KEEP= ID oud_apcd year_apcd month_apcd);
SET PHDAPCD.MEDICAL (KEEP= ID MED_ECODE MED_ADM_DIAGNOSIS
MED_ICD_PROC1-MED_ICD_PROC7
MED_ICD1-MED_ICD25
MED_FROM_DATE_YEAR MED_FROM_DATE_MONTH
MED_DIS_DIAGNOSIS
MED_PROC_CODE
WHERE= (MED_FROM_DATE_YEAR IN &year));
cnt_oud_apcd = 0;
oud_apcd = 0;
ARRAY vars1 {*} ID MED_ECODE MED_ADM_DIAGNOSIS
MED_ICD_PROC1-MED_ICD_PROC7
MED_ICD1-MED_ICD25
MED_DIS_DIAGNOSIS
MED_PROC_CODE;
DO i = 1 TO dim(vars1);
IF vars1[i] in &ICD THEN cnt_oud_apcd = cnt_oud_apcd+1;
END;
DROP= i;
IF cnt_oud_apcd > 0 THEN oud_apcd = 1;
IF oud_apcd = 0 THEN DELETE;
year_apcd = MED_FROM_DATE_YEAR;
month_apcd = MED_FROM_DATE_MONTH;
RUN;
DATA pharm (KEEP= year_pharm month_pharm oud_pharm ID);
SET PHDAPCD.PHARMACY(KEEP= PHARM_NDC PHARM_FILL_DATE_MONTH
PHARM_FILL_DATE_YEAR PHARM_ICD ID);
month_pharm = PHARM_FILL_DATE_MONTH;
year_pharm = PHARM_FILL_DATE_YEAR;
IF PHARM_ICD IN &ICD OR
PHARM_NDC IN (&BUP_NDC) THEN oud_pharm = 1;
ELSE oud_pharm = 0;
IF oud_pharm = 0 THEN DELETE;
RUN;
/*======CASEMIX DATA==========*/
/*
### Emergency Department
Casemix.ED (Emergency Department) has three smaller internally
linked tables: ED, ED_DIAG, and ED_PROC; all linked together by
their internal `ED_ID`, which is only found in the ED tables
and should not be linked back to the PHD ID.
1. ED: Within the ED Dataset, we are interested in if `ED_DIAG1`
or `ED_PRINCIPLE_ECODE` are within our OUD Code list.
A temporary variable `OUD_ED_RAW` is created as a flag.
2. ED_DIAG: Within the ED_DIAG Dataset, we construct our flag,
`OUD_ED_DIAG` from the variable `ED_DIAG`
3. ED_PROC: Within the ED_PROC Dataset, we construct our flag,
`OUD_ED_PROC` from the variable `ED_PROC`
4. Datasets ED, ED_DIAG, and ED_PROC and joined along
their internal `ED_ID`. If the sum of created flags is
strictly greater than zero, then the overall `OUD_CM_ED`
flag is set to 1.
### Hospital Inpatient Discharge
Casemix.HD (Hospital Inpatient Discharge) follows the same pattern
as ED and has three smaller internally linked tables: HD, HD_DIAG,
and HD_PROC; all linked together by their internal `HD_ID`,
which is only found in the HD tables and should not be linked
back to the PHD ID.
1. HD: Within the HD Dataset, we are intersted in if `HD_PROC1` or
`HD_DIAG1` are within our OUD Code list. A temporary variable
`OUD_HD_RAW` is created as a flag.
2. HD_DIAG: Within the HD_DIAG Dataset, we construct our flag,
`OUD_HD_DIAG` from the variable `HD_DIAG`
3. HD_PROC: Within the HD_PROC Dataset, we construct our flag,
`OUD_HD_PROC` from the variable `HD_PROC`
4. Datasets HD, HD_DIAG, and HD_PROC and joined along their
internal `HD_ID`. If the sum of created flags is strictly
greater than zero, then the overall `OUD_CM_HD` flag is set to 1.
### Outpatient Observations
Casemix.OO (Outpatient Observations) breaks from the previous
pattern of HD and ED by only have one attributing table.
Within this table, we construct our flag `OUD_CM_OO` by searching
through `OO_DIAG1-16`, `OO_PROC1-4`, `OO_CPT1-10`, and
`OO_PRINCIPALEXTERNAL_CAUSECODE`. We preform a rowwise search and
add one to a temporary `count` variable if they appear within our
code lists. At the end, if the `count` variable is strictly greater
than one then our `OUD_CM_OO` flag is set to 1.
*/
/* ED */
DATA casemix_ed (KEEP= ID oud_cm_ed ED_ID year_cm month_cm);
SET PHDCM.ED (KEEP= ID ED_DIAG1 ED_PRINCIPLE_ECODE ED_ADMIT_YEAR ED_AGE ED_ID ED_ADMIT_MONTH
WHERE= (ED_ADMIT_YEAR IN &year));
IF ED_DIAG1 in &ICD OR
ED_PRINCIPLE_ECODE IN &ICD THEN oud_cm_ed = 1;
ELSE oud_cm_ed = 0;
IF oud_cm_ed > 0 THEN do;
year_cm = ED_ADMIT_YEAR;
month_cm = ED_ADMIT_MONTH;
end;
RUN;
/* ED_DIAG */
DATA casemix_ed_diag (KEEP= oud_cm_ed_diag ED_ID);
SET PHDCM.ED_DIAG (KEEP= ED_ID ED_DIAG);
IF ED_DIAG in &ICD THEN oud_cm_ed_diag = 1;
ELSE oud_cm_ed_diag = 0;
RUN;
/* ED_PROC */
DATA casemix_ed_proc (KEEP= oud_cm_ed_proc ED_ID);
SET PHDCM.ED_PROC (KEEP= ED_ID ED_PROC);
IF ED_PROC in &PROC THEN oud_cm_ed_proc = 1;
ELSE oud_cm_ed_proc = 0;
RUN;
/* CASEMIX ED MERGE */
PROC SQL;
CREATE TABLE pharm AS
SELECT DISTINCT *
FROM pharm;
CREATE TABLE casemix_ed_proc AS
SELECT DISTINCT *
FROM casemix_ed_proc;
CREATE TABLE apcd AS
SELECT DISTINCT *
FROM apcd;
CREATE TABLE casemix_ed AS
SELECT DISTINCT *
FROM casemix_ed;
CREATE TABLE casemix_ed_diag AS
SELECT DISTINCT *
FROM casemix_ed_diag;
CREATE TABLE casemix AS
SELECT *
FROM casemix_ed
LEFT JOIN casemix_ed_diag ON casemix_ed.ED_ID = casemix_ed_diag.ED_ID
LEFT JOIN casemix_ed_proc ON casemix_ed_diag.ED_ID = casemix_ed_proc.ED_ID;
QUIT;
DATA casemix (KEEP= ID oud_ed year_cm age_ed month_cm);
SET casemix;
IF SUM(oud_cm_ed_proc, oud_cm_ed_diag, oud_cm_ed) > 0 THEN oud_ed = 1;
ELSE oud_ed = 0;
IF oud_ed = 0 THEN DELETE;
RUN;
/* HD DATA */
DATA hd (KEEP= HD_ID ID oud_hd_raw year_hd month_hd);
SET PHDCM.HD (KEEP= ID HD_DIAG1 HD_PROC1 HD_ADMIT_YEAR HD_AGE HD_ID HD_ADMIT_MONTH HD_ECODE
WHERE= (HD_ADMIT_YEAR IN &year));
IF HD_DIAG1 in &ICD OR
HD_PROC1 in &PROC OR
HD_ECODE IN &ICD THEN oud_hd_raw = 1;
ELSE oud_hd_raw = 0;
IF oud_hd_raw > 0 THEN do;
year_hd = HD_ADMIT_YEAR;
month_hd = HD_ADMIT_MONTH;
end;
RUN;
/* HD DIAG DATA */
DATA hd_diag (KEEP= HD_ID oud_hd_diag);
SET PHDCM.HD_DIAG (KEEP= HD_ID HD_DIAG);
IF HD_DIAG in &ICD THEN oud_hd_diag = 1;
ELSE oud_hd_diag = 0;
RUN;
/* HD PROC DATA */
DATA hd_proc(KEEP= HD_ID oud_hd_proc);
SET PHDCM.HD_PROC(KEEP = HD_ID HD_PROC);
IF HD_PROC IN &PROC THEN oud_hd_proc = 1;
ELSE oud_hd_proc = 0;
RUN;
/* HD MERGE */
PROC SQL;
CREATE TABLE pharm AS
SELECT DISTINCT *
FROM pharm;
CREATE TABLE hd_diag AS
SELECT DISTINCT *
FROM hd_diag;
CREATE TABLE casemix AS
SELECT DISTINCT *
FROM casemix;
CREATE TABLE hd AS
SELECT DISTINCT *
FROM hd;
CREATE TABLE hd_proc AS
SELECT DISTINCT *
FROM hd_proc;
CREATE TABLE hd AS
SELECT *
FROM hd
LEFT JOIN hd_diag ON hd.HD_ID = hd_diag.HD_ID
LEFT JOIN hd_proc ON hd.HD_ID = hd_proc.HD_ID;
QUIT;
DATA hd (KEEP= ID oud_hd year_hd month_hd);
SET hd;
IF SUM(oud_hd_diag, oud_hd_raw, oud_hd_proc) > 0 THEN oud_hd = 1;
ELSE oud_hd = 0;
IF oud_hd = 0 THEN DELETE;
RUN;
/* OO */
DATA oo (KEEP= ID oud_oo year_oo month_oo);
SET PHDCM.OO (KEEP= ID OO_DIAG1-OO_DIAG16 OO_PROC1-OO_PROC4
OO_ADMIT_YEAR OO_ADMIT_MONTH
OO_CPT1-OO_CPT10
OO_PRINCIPALEXTERNAL_CAUSECODE
WHERE= (OO_ADMIT_YEAR IN &year));
cnt_oud_oo = 0;
ARRAY vars2 {*} OO_DIAG1-OO_DIAG16
OO_PROC1-OO_PROC4
OO_CPT1-OO_CPT10
OO_PRINCIPALEXTERNAL_CAUSECODE;
DO k = 1 TO dim(vars2);
IF SUBSTR(VNAME(vars2[k]), 1) = 'OO_PROC' THEN
IF vars2[k] IN &PROC THEN
cnt_oud_oo = cnt_oud_oo + 1;
ELSE IF vars2[k] IN &ICD THEN
cnt_oud_oo = cnt_oud_oo + 1;
END;
DROP k;
IF cnt_oud_oo > 0 THEN oud_oo = 1;
ELSE oud_oo = 0;
IF oud_oo = 0 THEN DELETE;
year_oo = OO_ADMIT_YEAR;
month_oo = OO_ADMIT_MONTH;
RUN;
/* MERGE ALL CM */
/* Perform full join for all casemix tables */
PROC SQL;
CREATE TABLE casemix AS
SELECT *
FROM casemix
FULL JOIN hd ON casemix.ID = hd.ID
AND casemix.year_cm = hd.year_hd
AND casemix.month_cm = hd.month_hd
FULL JOIN oo ON hd.ID = oo.ID
AND casemix.year_cm = oo.year_oo
AND casemix.month_cm = oo.month_oo;
QUIT;
PROC STDIZE DATA = casemix OUT = casemix reponly missing = 9999; RUN;
DATA casemix (KEEP = ID oud_cm year_cm month_cm);
SET casemix;
IF oud_ed = 9999 THEN oud_ed = 0;
IF oud_hd = 9999 THEN oud_hd = 0;
IF oud_oo = 9999 THEN oud_oo = 0;
IF sum(oud_ed, oud_hd, oud_oo) > 0 THEN oud_cm = 1;
ELSE oud_cm = 0;
IF oud_cm = 0 THEN DELETE;
year_cm = min(year_oo, year_hd, year_cm);
month_cm = min(month_oo, month_hd, month_cm);
RUN;
/* BSAS */
/*
Like Matris, the BSAS dataset involves some PHD level encoding.
We tag a record with our flag, `OUD_BSAS`, if
`CLT_ENR_PRIMARY_DRUG`, `CLT_ENR_SECONDARY_DRUG`,
`CLT_ENR_TERTIARY_DRUG` are in the encoded list: (5,6,7,21,22,23,24,26)
or if `PHD_PRV_SERV_CAT = 7` (Opioid Treatment).
Descriptions of the BSAS drugs respective to
PHD level documentation
1. 5: Heroin
2. 6: Non-Rx Methadone
3. 7: Other Opiates
4. 21: Oxycodone
5. 22: Non-Rx Suboxone
6. 23: Rx Opiates
7. 24: Non-Rx Opiates
8. 26: Fentanyl
*/
DATA bsas (KEEP= ID oud_bsas year_bsas month_bsas);
SET PHDBSAS.BSAS (KEEP= ID CLT_ENR_OVERDOSES_LIFE
CLT_ENR_PRIMARY_DRUG
CLT_ENR_SECONDARY_DRUG
CLT_ENR_TERTIARY_DRUG
PDM_PRV_SERV_CAT
ENR_YEAR_BSAS
ENR_MONTH_BSAS
WHERE= (ENR_YEAR_BSAS IN &year));
IF (CLT_ENR_OVERDOSES_LIFE > 0 AND CLT_ENR_OVERDOSES_LIFE ^= 999)
OR CLT_ENR_PRIMARY_DRUG in &bsas_drugs
OR CLT_ENR_SECONDARY_DRUG in &bsas_drugs
OR CLT_ENR_TERTIARY_DRUG in &bsas_drugs
OR PDM_PRV_SERV_CAT = 7 THEN oud_bsas = 1;
ELSE oud_bsas = 0;
IF oud_bsas = 0 THEN DELETE;
year_bsas = ENR_YEAR_BSAS;
month_bsas = ENR_MONTH_BSAS;
RUN;
/* MATRIS */
/*
The MATRIS Dataset depends on PHD level encoding of variables
`OPIOID_ORI_MATRIS` and `OPIOID_ORISUBCAT_MATRIS` to
construct our flag variable, `OUD_MATRIS`.
*/
DATA matris (KEEP= ID oud_matris year_matris month_matris);
SET PHDEMS.MATRIS (KEEP= ID OPIOID_ORI_MATRIS
OPIOID_ORISUBCAT_MATRIS
inc_year_matris
inc_month_matris
AGE_MATRIS
AGE_UNITS_MATRIS
WHERE= (inc_year_matris IN &year));
IF OPIOID_ORI_MATRIS = 1
OR OPIOID_ORISUBCAT_MATRIS in (1:5) THEN oud_matris = 1;
ELSE oud_matris = 0;
IF oud_matris = 0 THEN DELETE;
year_matris = inc_year_matris;
month_matris = inc_month_matris;
RUN;
/* DEATH */
/*
The Death dataset holds the official cause and manner of
death assigned by physicians and medical examiners. For our
purposes, we are only interested in the variable `OPIOID_DEATH`
which is based on 'ICD10 codes or literal search' from other
PHD sources.
*/
DATA death (KEEP= ID oud_death year_death month_death);
SET PHDDEATH.DEATH (KEEP= ID OPIOID_DEATH YEAR_DEATH AGE_DEATH
WHERE= (YEAR_DEATH IN &year));
IF OPIOID_DEATH = 1 THEN oud_death = 1;
ELSE oud_death = 0;
IF oud_death = 0 THEN DELETE;
year_death = YEAR_DEATH;
month_death = MONTH_DEATH;
RUN;
/* PMP */
/*
Within the PMP dataset, we only use the `BUPRENORPHINE_PMP`
to define the flag `OUD_PMP` - conditioned on BUP_CAT_PMP = 1.
*/
DATA pmp (KEEP= ID oud_pmp year_pmp month_pmp);
SET PHDPMP.PMP (KEEP= ID BUPRENORPHINE_PMP date_filled_year date_filled_month BUP_CAT_PMP
WHERE= (date_filled_year IN &year));
IF BUPRENORPHINE_PMP = 1 AND
BUP_CAT_PMP = 1 THEN oud_pmp = 1;
ELSE oud_pmp = 0;
IF oud_pmp = 0 THEN DELETE;
year_pmp = date_filled_year;
month_pmp = date_filled_month;
RUN;
/*===========================*/
/* MAIN MERGE */
/*===========================*/
/*
As a final series of steps:
1. APCD-Pharm, APCD-Medical, Casemix, Death, PMP, Matris,
BSAS are joined together on the cartesian coordinate of Months
(1:12), Year (2015:2021), and SPINE (Race, Sex, ID)
2. The sum of the fabricated flags is taken. If the sum is strictly
greater than zero, then the master flag is set to 1.
Zeros are deleted
4. We select distinct ID, Age Bins, Race, Year, and Month and
output the count of those detected with OUD
5. Any count that is between 1 and 10 are suppressed and set to -1,
any zeros are true zeros
*/
PROC SQL;
CREATE TABLE oo AS
SELECT DISTINCT *
FROM oo;
CREATE TABLE bsas AS
SELECT DISTINCT *
FROM bsas;
CREATE TABLE matris AS
SELECT DISTINCT *
FROM matris;
CREATE TABLE death AS
SELECT DISTINCT *
FROM death;
CREATE TABLE pmp AS
SELECT DISTINCT *
FROM pmp;
CREATE TABLE oud_monthly AS
SELECT * FROM demographics_monthly
LEFT JOIN apcd ON apcd.ID = demographics_monthly.ID
AND apcd.year_apcd = demographics_monthly.year
AND apcd.month_apcd = demographics_monthly.month
LEFT JOIN casemix ON casemix.ID = demographics_monthly.ID
AND casemix.year_cm = demographics_monthly.year
AND casemix.month_cm = demographics_monthly.month
LEFT JOIN bsas ON bsas.ID = demographics_monthly.ID
AND bsas.year_bsas = demographics_monthly.year
AND bsas.month_bsas = demographics_monthly.month
LEFT JOIN matris ON matris.ID = demographics_monthly.ID
AND matris.year_matris = demographics_monthly.year
AND matris.month_matris = demographics_monthly.month
LEFT JOIN death ON death.ID = demographics_monthly.ID
AND death.year_death = demographics_monthly.year
AND death.month_death = demographics_monthly.month
LEFT JOIN pmp ON pmp.ID = demographics_monthly.ID
AND pmp.year_pmp = demographics_monthly.year
AND pmp.month_pmp = demographics_monthly.month
LEFT JOIN pharm ON pharm.ID = demographics_monthly.ID
AND pharm.year_pharm = demographics_monthly.year
AND pharm.month_pharm = demographics_monthly.month;
CREATE TABLE oud_yearly AS
SELECT * FROM demographics_yearly
LEFT JOIN apcd ON apcd.ID = demographics_yearly.ID
AND apcd.year_apcd = demographics_yearly.year
LEFT JOIN casemix ON casemix.ID = demographics_yearly.ID
AND casemix.year_cm = demographics_yearly.year
LEFT JOIN bsas ON bsas.ID = demographics_yearly.ID
AND bsas.year_bsas = demographics_yearly.year
LEFT JOIN matris ON matris.ID = demographics_yearly.ID
AND matris.year_matris = demographics_yearly.year
LEFT JOIN death ON death.ID = demographics_yearly.ID
AND death.year_death = demographics_yearly.year
LEFT JOIN pmp ON pmp.ID = demographics_yearly.ID
AND pmp.year_pmp = demographics_yearly.year
LEFT JOIN pharm ON pharm.ID = demographics_yearly.ID
AND pharm.year_pharm = demographics_yearly.year;
CREATE TABLE oud_yearly AS
SELECT DISTINCT *
FROM oud_yearly;
QUIT;
PROC STDIZE DATA = oud_monthly OUT = oud_monthly reponly missing = 9999; RUN;
PROC STDIZE DATA = oud_yearly OUT = oud_yearly reponly missing = 9999; RUN;
DATA oud_monthly;
SET oud_monthly;
ARRAY oud_flags {*} oud_apcd oud_cm
oud_death oud_matris
oud_pmp oud_bsas
oud_pharm;
DO i = 1 TO dim(oud_flags);
IF oud_flags[i] = 9999 THEN oud_flags[i] = 0;
END;
oud_cnt = sum(oud_apcd, oud_cm, oud_death, oud_matris, oud_pmp, oud_bsas, oud_pharm);
IF oud_cnt > 0 THEN oud_master = 1;
ELSE oud_master = 0;
IF oud_master = 0 THEN DELETE;
age = year - YOB;
age_grp_five = put(age, age_grps_five.);
age_grp_twenty = put(age, age_grps_twenty.);
RUN;
DATA oud_yearly;
SET oud_yearly;
ARRAY oud_flags {*} oud_apcd oud_cm
oud_death oud_matris
oud_pmp oud_bsas
oud_pharm;
DO i = 1 TO dim(oud_flags);
IF oud_flags[i] = 9999 THEN oud_flags[i] = 0;
END;
oud_cnt = sum(oud_apcd, oud_cm, oud_death, oud_matris, oud_pmp, oud_bsas, oud_pharm);
IF oud_cnt > 0 THEN oud_master = 1;
ELSE oud_master = 0;
IF oud_master = 0 THEN DELETE;
age = year - YOB;
age_grp_five = put(age, age_grps_five.);
age_grp_twenty = put(age, age_grps_twenty.);
RUN;
PROC SQL;
CREATE TABLE oud_out_yearly AS
SELECT DISTINCT year,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_yearly
GROUP BY year;
CREATE TABLE oud_out_monthly AS
SELECT DISTINCT year, month,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_monthly
GROUP BY year, month;
CREATE TABLE oud_five_yearly AS
SELECT DISTINCT age_grp_five, year,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_yearly
GROUP BY age_grp_five, year, FINAL_SEX, FINAL_RE;
CREATE TABLE oud_twenty_yearly AS
SELECT DISTINCT age_grp_twenty, year,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_yearly
GROUP BY age_grp_twenty, year;
CREATE TABLE oud_five_monthly AS
SELECT DISTINCT age_grp_five, year, month,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_monthly
GROUP BY age_grp_five, year, month;
CREATE TABLE oud_twenty_monthly AS
SELECT DISTINCT age_grp_twenty, year, month,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_monthly
GROUP BY age_grp_twenty, year, month;
CREATE TABLE oud_sex_yearly AS
SELECT DISTINCT year, FINAL_SEX,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_yearly
GROUP BY year, FINAL_SEX;
CREATE TABLE oud_sex_monthly AS
SELECT DISTINCT year, month, FINAL_SEX,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_monthly
GROUP BY year, month, FINAL_SEX;
CREATE TABLE oud_race_yearly AS
SELECT DISTINCT year, FINAL_RE,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_yearly
GROUP BY year, FINAL_RE;
CREATE TABLE oud_race_monthly AS
SELECT DISTINCT year, month, FINAL_RE,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_monthly
GROUP BY year, month, FINAL_RE;
QUIT;
PROC EXPORT
DATA= oud_out_yearly
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDCount_Yearly_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_out_monthly
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDCount_Monthly_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_five_yearly
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDCount_Five_Yearly_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_twenty_yearly
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDCount_Twenty_Yearly_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_five_monthly
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDCount_Five_Monthly_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_twenty_monthly
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDCount_Twenty_Monthly_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_sex_monthly
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDCount_Sex_Monthly_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_sex_yearly
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDCount_Sex_Yearly_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_race_monthly
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDCount_Race_Monthly_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_race_yearly
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDCount_Race_Yearly_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
/* Data Origin Location */
/*
Data used by the Capture Re-Capture Method (CRC) is pulled from
the Public Health Data Warehouse (PHDW) and is non-stratified.
This data details how many people are within the combination of
databases we pull from. For example, a row detailing '1' in the
APCD and Casemix column would indicate that 'x' people in the
N_ID column were 'captured' in both APCD and Casemix in the time
of interest (a given year.) This extends to all six of the
databases we currently pull from.
*/
PROC SQL;
CREATE TABLE oud_origin_five AS
SELECT DISTINCT oud_cm AS Casemix,
IFN(sum(oud_apcd, oud_pharm)>0, 1, 0) AS APCD,
oud_bsas AS BSAS,
oud_pmp AS PMP,
oud_matris AS Matris,
oud_death AS Death,
year,
age_grp_five,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_yearly
GROUP BY Casemix, APCD, BSAS, PMP, Matris, Death, year, age_grp_five;
CREATE TABLE oud_origin_twenty AS
SELECT DISTINCT oud_cm AS Casemix,
IFN(sum(oud_apcd, oud_pharm)>0, 1, 0) AS APCD,
oud_bsas AS BSAS,
oud_pmp AS PMP,
oud_matris AS Matris,
oud_death AS Death,
year,
age_grp_twenty,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_yearly
GROUP BY Casemix, APCD, BSAS, PMP, Matris, Death, year, age_grp_twenty;
CREATE TABLE oud_origin_race AS
SELECT DISTINCT oud_cm AS Casemix,
IFN(sum(oud_apcd, oud_pharm)>0, 1, 0) AS APCD,
oud_bsas AS BSAS,
oud_pmp AS PMP,
oud_matris AS Matris,
oud_death AS Death,
FINAL_RE, year,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_yearly
GROUP BY Casemix, APCD, BSAS, PMP, Matris, Death, year, FINAL_RE;
CREATE TABLE oud_origin_sex AS
SELECT DISTINCT oud_cm AS Casemix,
IFN(sum(oud_apcd, oud_pharm)>0, 1, 0) AS APCD,
oud_bsas AS BSAS,
oud_pmp AS PMP,
oud_matris AS Matris,
oud_death AS Death,
FINAL_SEX, year,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_yearly
GROUP BY Casemix, APCD, BSAS, PMP, Matris, Death, year, FINAL_SEX;
CREATE TABLE oud_origin AS
SELECT DISTINCT oud_cm AS Casemix,
IFN(sum(oud_apcd, oud_pharm)>0, 1, 0) AS APCD,
oud_bsas AS BSAS,
oud_pmp AS PMP,
oud_matris AS Matris,
oud_death AS Death, year,
IFN(COUNT(DISTINCT ID) IN (1:10), -1, COUNT(DISTINCT ID)) AS N_ID
FROM oud_yearly
GROUP BY Casemix, APCD, BSAS, PMP, Matris, Death, year;
QUIT;
PROC EXPORT
DATA= oud_origin
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDOrigin_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_origin_five
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDOrigin_Five_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_origin_twenty
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDOrigin_Twenty_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_origin_race
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDOrigin_Race_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
PROC EXPORT
DATA= oud_origin_sex
OUTFILE= "/sas/data/DPH/OPH/PHD/FOLDERS/SUBSTANCE_USE_CODE/RESPOND/RESPOND UPDATE/OUDOrigin_Sex_&formatted_date..csv"
DBMS= csv REPLACE;
RUN;
/*==============================*/
/* MOUD Counts */
/*==============================*/
/*
The goal of this portion of the script is to extract MOUD counts and
starts while treating it as a formal subset of the code defined above
(OUDCounts.) The table most used in this portion is the relatively-new
SPINE.MOUD table.
MOUD Starts are immediately given through SPINE.MOUD's DATE_START_*_MOUD
MOUD Counts, on the other hand, require a type of 'expansion', where we
create a new dataset filling out the months inbetween DATE_START_*_MOUD and
DATE_END_*_MOUD.
Restrictions:
1. If the lapse between a record's end date and the next record's
start date is < 7, we merge the two records together.
2. After this merge, if there are any more records which are <7 they
are removed from counts/starts tabulation
3. If medication A is found to be completely encompassed by another
medication B, then we remove the record of medication A.
*/
DATA moud;
SET PHDSPINE.MOUD;
RUN;
PROC SORT data=moud;
by ID DATE_START_MOUD;
RUN;
PROC SQL;
CREATE TABLE moud_demo AS
SELECT *, DEMO.FINAL_RE, DEMO.FINAL_SEX, DEMO.YOB
FROM moud
LEFT JOIN PHDSPINE.DEMO ON moud.ID = DEMO.ID;
QUIT;
PROC SORT DATA=moud_demo;
by ID TYPE_MOUD DATE_START_MOUD;
RUN;
/*
Create `episode_id`, which forms the basis for merging when
two episode IDs are the same
*/
DATA moud_demo;
SET moud_demo;
by ID TYPE_MOUD;
retain episode_num;
lag_date = lag(DATE_END_MOUD);
IF FIRST.TYPE_MOUD THEN lag_date = .;
IF FIRST.TYPE_MOUD THEN episode_num = 1;
diff = DATE_START_MOUD - lag_date;
/* If the difference is greater than MOUD leniency, assume
it is another treatment episode */
IF diff >= &MOUD_leniency THEN flag = 1; ELSE flag = 0;
IF flag = 1 THEN episode_num = episode_num + 1;
episode_id = catx("_", ID, episode_num);
RUN;
PROC SORT data=moud_demo;
BY episode_id;
RUN;
/* Filter cohort to OUD cohort above*/
PROC SQL;
CREATE TABLE moud_demo AS
SELECT *
FROM moud_demo
WHERE ID IN (SELECT DISTINCT ID FROM oud_yearly);
QUIT;
/*
Merge where episode ID is the same, taking the
start_month/year of the first record, and the
end_month/year of the final record
*/
DATA moud_demo;
SET moud_demo;
by episode_id;
retain DATE_START_MOUD;
IF FIRST.episode_id THEN DO;
start_month = DATE_START_MONTH_MOUD;
start_year = DATE_START_YEAR_MOUD;
start_date = DATE_START_MOUD;
END;
IF LAST.episode_id THEN DO;
end_month = DATE_END_MONTH_MOUD;
end_year = DATE_END_YEAR_MOUD;
end_date = DATE_END_MOUD;
END;
IF end_date - start_date < &MOUD_leniency THEN DELETE;
RUN;
PROC SORT data=moud_demo (KEEP= start_date start_month start_year
end_date end_month end_year
ID FINAL_RE FINAL_SEX TYPE_MOUD YOB);
BY ID;
RUN;