-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.R
1754 lines (1560 loc) · 56.1 KB
/
pipeline.R
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
### Pipeline to run PfD annual publication
# clear environment
rm(list = ls())
# source functions
# this is only a temporary step until all functions are built into packages
# select all .R files in functions sub-folder
function_files <- list.files(path = "functions", pattern = "\\.R$")
# loop over function_files to source all files in functions sub-folder
for (file in function_files) {
source(file.path("functions", file))
}
# 1. Setup ---------------------------------------------------------------------
# load GITHUB_KEY if available in environment or enter if not
if (Sys.getenv("GITHUB_PAT") == "") {
usethis::edit_r_environ()
stop(
"You need to set your GITHUB_PAT = YOUR PAT KEY in the .Renviron file which pops up. Please restart your R Studio after this and re-run the pipeline."
)
}
# load GITHUB_KEY if available in environment or enter if not
if (Sys.getenv("DB_DWCP_USERNAME") == "") {
usethis::edit_r_environ()
stop(
"You need to set your DB_DWCP_USERNAME = YOUR DWCP USERNAME and DB_DWCP_PASSWORD = YOUR DWCP PASSWORD in the .Renviron file which pops up. Please restart your R Studio after this and re-run the pipeline."
)
}
install.packages("devtools")
library(devtools)
# install nhsbsaUtils package first as need check_and_install_packages()
devtools::install_github("nhsbsa-data-analytics/nhsbsaUtils",
auth_token = Sys.getenv("GITHUB_PAT"),
force = TRUE)
library(nhsbsaUtils)
# install and library packages
req_pkgs <-
c(
"dplyr",
"stringr",
"data.table",
"yaml",
"openxlsx",
"rmarkdown",
"logr",
"highcharter",
"lubridate",
"dbplyr",
"tidyr",
"janitor",
"magrittr",
"tcltk",
"DT",
"htmltools",
"geojsonsf",
"readxl",
"kableExtra",
"nhsbsa-data-analytics/nhsbsaR",
"nhsbsa-data-analytics/nhsbsaExternalData",
"nhsbsa-data-analytics/accessibleTables",
"nhsbsa-data-analytics/nhsbsaDataExtract",
"nhsbsa-data-analytics/nhsbsaVis"
)
# library/install packages as required
nhsbsaUtils::check_and_install_packages(req_pkgs)
# set up logging
# lf <-
# logr::log_open(paste0(
# "Y:/Official Stats/PfD/log/pfd_log",
# format(Sys.time(), "%d%m%y%H%M%S"),
# ".log"
# ))
# load config
config <- yaml::yaml.load_file("config.yml")
# log_print("Config loaded", hide_notes = TRUE)
# log_print(config, hide_notes = TRUE)
# load options
nhsbsaUtils::publication_options()
# log_print("Options loaded", hide_notes = TRUE)
# 2. connect to DWH and pull max CY/FY ----------------------------------------
# build connection to warehouse
con <- nhsbsaR::con_nhsbsa(dsn = "FBS_8192k",
driver = "Oracle in OraClient19Home1",
"DWCP")
# 3. collect data from data warehouse ------------------------------------------
cost_per_icb_data <-
cost_per_icb_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
icb_name_lookup <- cost_per_icb_data |>
select(`Integrated Care Board Code`, `Integrated Care Board Name`) |>
distinct()
cost_per_icb_overall_data <-
tbl(con, dbplyr::in_schema("OST", "PFD_OVERALL_ICB_FACT_202407")) |>
collect() |>
arrange(FINANCIAL_YEAR,
ICB_CODE,
DRUG_TYPE,
desc(PATIENT_IDENTIFIED)) |>
select(
`Financial Year` = FINANCIAL_YEAR,
`Drug Type` = DRUG_TYPE,
`Integrated Care Board Code` = ICB_CODE,
`Identified Patient Flag` = PATIENT_IDENTIFIED,
`Total Identified Patients` = PATIENTS,
`Total Items` = ITEMS,
`Total Net Ingredient Cost (£)` = NIC
) |>
mutate(
`Total Identified Patients` = case_when(
`Identified Patient Flag` == "N" ~ 0,
TRUE ~ `Total Identified Patients`
)
) |>
left_join(icb_name_lookup) |>
relocate(`Integrated Care Board Name`, .after = `Integrated Care Board Code`) |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
cost_per_pat_data <-
cost_per_patient_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_national_data <-
national_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_paragraph_data <-
paragraph_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_u18_data <-
child_adult_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_imd_data <-
imd_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_imd_paragraph_data <-
imd_paragraph_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_ageband_data <-
ageband_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_ageband_paragraph_data <-
ageband_paragraph_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_gender_data <-
gender_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_gender_paragraph_data <-
gender_paragraph_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_age_gender_data <-
age_gender_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_age_gender_paragraph_data <-
age_gender_paragraph_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
pfd_national_presentation <- national_presentation(con = con,
schema = "OST",
table = "PFD_FACT_202407")
patient_identification_dt <-
capture_rate_extract_dt(con = con,
schema = "OST",
table = "PFD_FACT_202407") |>
select(1, 2, last_col(4):last_col())
patient_identification <-
capture_rate_extract(con = con,
schema = "OST",
table = "PFD_FACT_202407")
pfd_national_overall <-
tbl(con, dbplyr::in_schema("OST", "PFD_FACT_OVERALL_202406")) |>
collect() |>
select(
`Financial Year` = FINANCIAL_YEAR,
`Drug Type` = DRUG_TYPE,
`Identified Patient Flag` = PATIENT_IDENTIFIED,
`Total Identified Patients` = PATIENTS,
`Total Items` = ITEMS,
`Total Net Ingredient Cost (£)` = NIC
) |>
mutate(
`Total Identified Patients` = case_when(
`Identified Patient Flag` == "N" ~ 0,
TRUE ~ `Total Identified Patients`
)
) |>
apply_sdc(rounding = F,
suppress_column = "Total Identified Patients")
cost_per_patient_icb <- cost_per_icb_data |>
dplyr::ungroup() |>
dplyr::filter(`Identified Patient Flag` == "Y") |>
dplyr::mutate(`Total NIC per patient (£)` = `Total Net Ingredient Cost (£)` /
`Total Identified Patients`) |>
dplyr::select(
`Financial Year`,
`Integrated Care Board Name`,
`Integrated Care Board Code`,
`Total NIC per patient (£)`
)
# 4. Build costs/items excel tables --------------------------------------------
sheetNames <- c(
"Patient_Identification",
"National_Total",
"National_Paragraph",
"Cost_per_ICB",
"Cost_per_Patient",
"National_Presentation"
)
wb <- accessibleTables::create_wb(sheetNames)
# create metadata tab (will need to open file and auto row heights once ran)
meta_fields <- c(
"BNF Paragraph Code",
"BNF Paragraph Name",
"Financial Year",
"Financial Quarter",
"Identified Patient",
"Integrated Care Board Code",
"Integrated Care Board Name",
"Total Items",
"Total Net Ingredient Cost (£)",
"Total NIC per patient (£)",
"Total Patients",
"Drug Type"
)
meta_descs <-
c(
"The unique code used to refer to the British National Formulary (BNF) paragraph.",
"The name given to the British National Formulary (BNF) paragraph. This level of grouping of the BNF Therapeutical classification system sits below BNF section.",
"The financial year to which the data belongs.",
"The financial quarter to which the data belongs.",
"This shows where an item has been attributed to an NHS number that has been verified by the Personal Demographics Service (PDS).",
"The name given to the Integrated Care Board (ICB) that a prescribing organisation belongs to. This is based upon NHSBSA administrative records, not geographical boundaries and more closely reflect the operational organisation of practices than other geographical data sources.",
"The unique code used to refer to an Integrated Care Board (ICB)",
"The number of prescription items dispensed. 'Items' is the number of times a product appears on a prescription form. Prescription forms include both paper prescriptions and electronic messages.",
"Total Net Ingredient Cost is the amount that would be paid using the basic price of the prescribed drug or appliance and the quantity prescribed. Sometimes called the 'Net Ingredient Cost' (NIC). The basic price is given either in the Drug Tariff or is determined from prices published by manufacturers, wholesalers or suppliers. Basic price is set out in Parts 8 and 9 of the Drug Tariff. For any drugs or appliances not in Part 8, the price is usually taken from the manufacturer, wholesaler or supplier of the product. This is given in GBP (£).",
"Cost per patient has been calculated by dividing the total cost associated with identified patients by the number of patients that have been identified in that CCG per financial year.",
"Where patients are identified via the flag, the number of patients that the data corresponds to. This will always be 0 where 'Identified Patient' = N.",
"Based on the primary therapeutic use, this indicates whether an item is within the paragraphs associated with diabetes or whether the item is in another ('Non-diabetes') paragraph."
)
accessibleTables::create_metadata(wb,
meta_fields,
meta_descs)
#### Patient identification
# write data to sheet
write_sheet(
wb,
"Patient_Identification",
paste0(
"Prescribing for Diabetes - 2015/16 to ",
config$full_year,
" - Percentage of items for which an NHS number was recorded (%)"
),
c(
"The below percentages reflect the percentage of prescription items where a PDS verified NHS number was recorded."
),
patient_identification,
42
)
# left align columns A to C
format_data(wb,
"Patient_Identification",
c("A", "B", "C"),
"left",
"")
# right align columns and round to 2 DP - D (if using long data not pivoting wider) (!!NEED TO UPDATE AS DATA EXPANDS!!)
format_data(wb,
"Patient_Identification",
c("D"),
"right",
"0.00")
#### Total items data
# write data to sheet
# suggest note 3. could be condensed to something like "Total costs and items may not match those in our Prescription Cost Analysis (PCA) publication, as they are based on a prescribing view while PCA uses a dispensing view instead."
write_sheet(
wb,
"National_Total",
paste0(
"Table 1: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" total dispensed items and costs per financial year"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. The patient counts shown in these statistics should only be analysed at the level at which they are presented. Adding together any patient counts is likely to result in an overestimate of the number of patients.",
"3. Total costs and items may not be reconciled back to Prescribing Cost Analysis (PCA) publication figures as these figures are based around a 'prescribing view' of the data. This is where we use the drug or device that was prescribed to a patient, rather than the drug that was reimbursed to the dispenser to classify a prescription item. PCA uses a dispensing view where the inverse is true."
),
pfd_national_overall,
14
)
# left align columns A to C
format_data(wb,
"National_Total",
c("A", "B", "C"),
"left",
"")
# right align columns D and E and round to whole numbers with thousand separator
format_data(wb,
"National_Total",
c("D", "E"),
"right",
"#,##0")
# right align column F and round to 2dp with thousand separator
format_data(wb,
"National_Total",
c("F"),
"right",
"#,##0.00")
#### National Paragraph data
# write data to sheet
write_sheet(
wb,
"National_Paragraph",
paste0(
"Table 2: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" yearly totals split by BNF paragraph and identified patients"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. The patient counts shown in these statistics should only be analysed at the level at which they are presented. Adding together any patient counts is likely to result in an overestimate of the number of patients."
),
pfd_paragraph_data,
14
)
# left align columns A to D
format_data(wb,
"National_Paragraph",
c("A", "B", "C", "D"),
"left",
"")
# right align columns E and F and round to whole numbers with thousand separator
format_data(wb,
"National_Paragraph",
c("E", "F"),
"right",
"#,##0")
# right align column G and round to 2dp with thousand separator
format_data(wb,
"National_Paragraph",
c("G"),
"right",
"#,##0.00")
#### Cost per ICB data
# write data to sheet
write_sheet(
wb,
"Cost_per_ICB",
paste0(
"Table 3: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" costs and items per ICB per financial year"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. The patient counts shown in these statistics should only be analysed at the level at which they are presented. Adding together any patient counts is likely to result in an overestimate of the number of patients."
),
cost_per_icb_overall_data,
14
)
# left align columns A to D
format_data(wb,
"Cost_per_ICB",
c("A", "B", "C", "D", "E"),
"left",
"")
# right align columns E and F and round to whole numbers with thousand separator
format_data(wb,
"Cost_per_ICB",
c("F", "G"),
"right",
"#,##0")
# right align column G and round to 2dp with thousand separator
format_data(wb,
"Cost_per_ICB",
c("H"),
"right",
"#,##0.00")
#### Cost per patient data
# write data to sheet
write_sheet(
wb,
"Cost_per_Patient",
paste0(
"Table 4: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" average cost per patient per ICB per financial year"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. The patient counts shown in these statistics should only be analysed at the level at which they are presented. Adding together any patient counts is likely to result in an overestimate of the number of patients.",
"3. Integrated Care Boards (ICBs) succeeded sustainability and transformation plans (STPs) and replaced the functions of clinical commissioning groups (CCGs) in July 2022 with ICB sub locations replacing CCGs during the transition period of 2022/23. This table now displays data at ICB level to reflect the current intended structure.",
"4. Only costs where the patient was known have been included in the Total NIC per patient (£) calculation. "
),
cost_per_patient_icb |> filter(`Integrated Care Board Name` != "UNKNOWN ICB"),
14
)
# left align columns A to C
format_data(wb,
"Cost_per_Patient",
c("A", "B", "C"),
"left",
"")
# right align column D and round to 2dp with thousand separator
format_data(wb,
"Cost_per_Patient",
c("D"),
"right",
"#,##0.00")
#### national presentation
# write data to sheet
write_sheet(
wb,
"National_Presentation",
paste0(
"Table 5: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" total items and costs by BNF Presentation"
),
c("1. Field definitions can be found on the 'Metadata' tab."),
pfd_national_presentation,
14
)
# left align columns A to I
format_data(wb,
"National_Presentation",
c("A", "B", "C", "D", "E", "F", "G", "H", "I"),
"left",
"")
format_data(wb,
"National_Presentation",
c("J"),
"right",
"#,##0")
# right align column K and round to 2dp with thousand separator
format_data(wb,
"National_Presentation",
c("K"),
"right",
"#,##0.00")
# build cover sheet
accessibleTables::makeCoverSheet(
paste0("Prescribing for Diabetes - England 2015/16 - ", config$full_year),
"Costs and Items",
paste0("Publication date: ", config$publication_date),
wb,
sheetNames,
c(
"Metadata",
"Patient Identification Rates",
"Table 1: Total items",
"Table 2: Diabetes Items",
"Table 3: Costs Per ICB",
"Table 4: Costs Per Patient",
"Table 5: Costs and items by BNF presentation"
),
c("Metadata", sheetNames)
)
# save file into outputs folder
openxlsx::saveWorkbook(wb,
"outputs/PfD_2023_2024_costs_and_items_v001.xlsx",
overwrite = TRUE)
rm(wb)
# 5. Build patient demographics excel tables -----------------------------------
sheetNames <- c(
"Patient_Identification",
"National_Total",
"Gender",
"Dispensing_By_Gender",
"Age_Band",
"Dispensing_By_Age",
"Age_Band_and_Gender",
"Dispensing_By_Age_and_Gender",
"Adults_and_Children",
"Indices_of_Deprivation",
"IMD_By_Drug"
)
wb <- create_wb(sheetNames)
# create metadata tab (will need to open file and auto row heights once ran)
meta_fields <- c(
"BNF Paragraph Code",
"BNF Paragraph Name",
"Financial Year",
"Financial Quarter",
"Identified Patient",
"Integrated Care Board Code",
"Integrated Care Board Name",
"Total Items",
"Total Net Ingredient Cost (£)",
"Total NIC per patient (£)",
"Total Patients",
"Drug Type",
"Patient Gender",
"Age Band",
"IMD Decile"
)
meta_descs <-
c(
"The unique code used to refer to the British National Formulary (BNF) paragraph.",
"The name given to the British National Formulary (BNF) paragraph. This level of grouping of the BNF Therapeutical classification system sits below BNF section.",
"The financial year to which the data belongs.",
"The financial quarter to which the data belongs.",
"This shows where an item has been attributed to an NHS number that has been verified by the Personal Demographics Service (PDS).",
"The name given to the Integrated Care Board (ICB) that a prescribing organisation belongs to. This is based upon NHSBSA administrative records, not geographical boundaries and more closely reflect the operational organisation of practices than other geographical data sources.",
"The unique code used to refer to an Integrated Care Board (ICB)",
"The number of prescription items dispensed. 'Items' is the number of times a product appears on a prescription form. Prescription forms include both paper prescriptions and electronic messages.",
"Total Net Ingredient Cost is the amount that would be paid using the basic price of the prescribed drug or appliance and the quantity prescribed. Sometimes called the 'Net Ingredient Cost' (NIC). The basic price is given either in the Drug Tariff or is determined from prices published by manufacturers, wholesalers or suppliers. Basic price is set out in Parts 8 and 9 of the Drug Tariff. For any drugs or appliances not in Part 8, the price is usually taken from the manufacturer, wholesaler or supplier of the product. This is given in GBP (£).",
"Cost per patient has been calculated by dividing the total cost associated with identified patients by the number of patients that have been identified in that CCG per financial year.",
"Where patients are identified via the flag, the number of patients that the data corresponds to. This will always be 0 where 'Identified Patient' = N.",
"Based on the primary therapeutic use, this indicates whether an item is within the paragraphs associated with diabetes or whether the item is in another ('Non-diabetes') paragraph.",
"The gender of the patient as noted at the time the prescription was processed. This includes where the patient has been identified but the gender has not been recorded.",
"The age band of the patient as of the 30th September of the corresponding financial year the drug was prescribed.",
"The IMD decile of the patient, based on the location of their practice, where '1' is the 10% of areas with the highest deprivation score in the Index of Multiple Deprivation (IMD) from the English Indices of Deprivation 2019, and '10' is the 10% of areas with the lowest IMD deprivation score. Unknown values are where the items are attributed to an unidentified practice within a Primary Care Organisation (PCO), or where we have been unable to match the practice postcode to a postcode in the National Statistics Postcode Lookup (NSPL)."
)
create_metadata(wb,
meta_fields,
meta_descs)
#### Patient identification
# write data to sheet
write_sheet(
wb,
"Patient_Identification",
paste0(
"Prescribing for Diabetes - 2015/16 to ",
config$full_year,
" - Percentage of items for which an NHS number was recorded (%)"
),
c(
"The below percentages reflect the percentage of prescription items where a PDS verified NHS number was recorded."
),
patient_identification,
42
)
# left align columns A to C
format_data(wb,
"Patient_Identification",
c("A", "B", "C"),
"left",
"")
# right align columns and round to 2 DP - D (if using long data not pivoting wider) (!!NEED TO UPDATE AS DATA EXPANDS!!)
format_data(wb,
"Patient_Identification",
c("D"),
"right",
"0.00")
### Total items data
write_sheet(
wb,
"National_Total",
paste0(
"Table 1: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" total dispensed items and costs per financial year"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. The patient counts shown in these statistics should only be analysed at the level at which they are presented. Adding together any patient counts is likely to result in an overestimate of the number of patients.",
"3. Total costs and items may not be reconciled back to Prescribing Cost Analysis (PCA) publication figures as these figures are based around a 'prescribing view' of the data. This is where we use the drug or device that was prescribed to a patient, rather than the drug that was reimbursed to the dispenser to classify a prescription item. PCA uses a dispensing view where the inverse is true."
),
pfd_national_overall,
14
)
# left align columns A to C
format_data(wb,
"National_Total",
c("A", "B", "C"),
"left",
"")
# right align columns D and E and round to whole numbers with thousand separator
format_data(wb,
"National_Total",
c("D", "E"),
"right",
"#,##0")
# right align column F and round to 2dp with thousand separator
format_data(wb,
"National_Total",
c("F"),
"right",
"#,##0.00")
#### National Sex data
# write data to sheet
write_sheet(
wb,
"Gender",
paste0(
"Table 2: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" national prescribing by gender per financial year"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. It is possible for a patient to be marked as 'unknown' or 'indeterminate'. Due to the low number of patients that these two groups contain the NHSBSA has decided to group these classifications together.",
"3. The NHSBSA does not use the latest national data standard relating to patient gender, and use historic nomenclature in some cases. Please see the detailed Background Information and Methodology notice released with this publication for further information."
),
pfd_gender_data,
14
)
# left align columns A to D
format_data(wb,
"Gender",
c("A", "B", "C"),
"left",
"")
# right align columns D and E and round to whole numbers with thousand separator
format_data(wb,
"Gender",
c("D", "E"),
"right",
"#,##0")
# right align column F and round to 2dp with thousand separator
format_data(wb,
"Gender",
c("F"),
"right",
"#,##0.00")
#### Paragraph Sex data
# write data to sheet
write_sheet(
wb,
"Dispensing_By_Gender",
paste0(
"Table 3: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" yearly patients, items and costs by BNF paragraph split by patient Gender"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. It is possible for a patient to be marked as 'unknown' or 'indeterminate'. Due to the low number of patients that these two groups contain the NHSBSA has decided to group these classifications together.",
"3. The NHSBSA does not use the latest national data standard relating to patient gender, and use historic nomenclature in some cases. Please see the detailed Background Information and Methodology notice released with this publication for further information.",
"4. Statistical disclosure control is applied where prescribing relates to fewer than 5 patients. These cells will appear blank."
),
pfd_gender_paragraph_data,
14
)
# left align columns A to E
format_data(wb,
"Dispensing_By_Gender",
c("A", "B", "C", "D", "E"),
"left",
"")
# right align columns F and G and round to whole numbers with thousand separator
format_data(wb,
"Dispensing_By_Gender",
c("F", "G"),
"right",
"#,##0")
# right align column H and round to 2dp with thousand separator
format_data(wb,
"Dispensing_By_Gender",
c("H"),
"right",
"#,##0.00")
#### National age data
# write data to sheet
write_sheet(
wb,
"Age_Band",
paste0(
"Table 4: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" National prescribing by age per financial year"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. A patient's age is calculated at 30 September of the given year in order to be assigned to an age band. Some patients do not hold a date of birth and are assigned an 'unknown' age band."
),
pfd_ageband_data,
14
)
# left align columns A to D
format_data(wb,
"Age_Band",
c("A", "B", "C"),
"left",
"")
# right align columns D and E and round to whole numbers with thousand separator
format_data(wb,
"Age_Band",
c("D", "E"),
"right",
"#,##0")
# right align column F and round to 2dp with thousand separator
format_data(wb,
"Age_Band",
c("F"),
"right",
"#,##0.00")
#### Paragraph Age data
# write data to sheet
write_sheet(
wb,
"Dispensing_By_Age",
paste0(
"Table 5: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" yearly patients, items and costs by BNF paragraph split by patient gender"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. The patient counts shown in these statistics should only be analysed at the level at which they are presented. Adding together any patient counts is likely to result in an overestimate of the number of patients.",
"3. Statistical disclosure control is applied where prescribing relates to fewer than 5 patients. These cells will appear blank."
),
pfd_ageband_paragraph_data,
14
)
# left align columns A to E
format_data(wb,
"Dispensing_By_Age",
c("A", "B", "C", "D", "E"),
"left",
"")
# right align columns F and G and round to whole numbers with thousand separator
format_data(wb,
"Dispensing_By_Age",
c("F", "G"),
"right",
"#,##0")
# right align column H and round to 2dp with thousand separator
format_data(wb,
"Dispensing_By_Age",
c("H"),
"right",
"#,##0.00")
#### National age sex data
# write data to sheet
write_sheet(
wb,
"Age_Band_and_Gender",
paste0(
"Table 6: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" national prescribing by age and gender per financial year"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. These totals only include patients where gender is known."
),
pfd_age_gender_data,
14
)
# left align columns A to D
format_data(wb,
"Age_Band_and_Gender",
c("A", "B", "C", "D"),
"left",
"")
# right align columns E and F and round to whole numbers with thousand separator
format_data(wb,
"Age_Band_and_Gender",
c("E", "F"),
"right",
"#,##0")
# right align column G and round to 2dp with thousand separator
format_data(wb,
"Age_Band_and_Gender",
c("G"),
"right",
"#,##0.00")
#### Paragraph Age Gender data
# write data to sheet
write_sheet(
wb,
"Dispensing_By_Age_and_Gender",
paste0(
"Table 7: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" yearly patients, items and costs by BNF paragraph split by patient age and gender"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. The patient counts shown in these statistics should only be analysed at the level at which they are presented. Adding together any patient counts is likely to result in an overestimate of the number of patients.",
"3. These totals only include patients where gender is known.",
"4. Statistical disclosure control is applied where prescribing relates to fewer than 5 patients. These cells will appear blank."
),
pfd_age_gender_paragraph_data,
14
)
# left align columns A to F
format_data(wb,
"Dispensing_By_Age_and_Gender",
c("A", "B", "C", "D", "E", "F"),
"left",
"")
# right align columns G and H and round to whole numbers with thousand separator
format_data(wb,
"Dispensing_By_Age_and_Gender",
c("G", "H"),
"right",
"#,##0")
# right align column I and round to 2dp with thousand separator
format_data(wb,
"Dispensing_By_Age_and_Gender",
c("I"),
"right",
"#,##0.00")
#### National u18 data
# write data to sheet
write_sheet(
wb,
"Adults_and_Children",
paste0(
"Table 8: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" prescribing by adult/child split"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. A patient's age is calculated at 30 September of the given year in order to be assigned to an age band. Some patients do not hold a date of birth and are assigned an 'unknown' age band."
),
pfd_u18_data,
14
)
# left align columns A to C
format_data(wb,
"Adults_and_Children",
c("A", "B", "C"),
"left",
"")
# right align columns D and E and round to whole numbers with thousand separator
format_data(wb,
"Adults_and_Children",
c("D", "E"),
"right",
"#,##0")
format_data(wb,
"Adults_and_Children",
c("F"),
"right",
"#,##0.00")
#### National imd data
# write data to sheet
write_sheet(
wb,
"Indices_of_Deprivation",
paste0(
"Table 9: Prescribing for Diabetes - England 2015/16 to ",
config$full_year,
" yearly patients, items and costs by IMD Decile"
),
c(
"1. Field definitions can be found on the 'Metadata' tab.",
"2. IMD deciles used are taken from the English Indices of Deprivaton 2019 National Statistics publication.",
"3. Where a patient's postcode has not been able to to be matched to NSPL or the patient has not been identified the records are reported as 'unknown' IMD quintile.",
"4. The patient counts shown in these statistics should only be analysed at the level at which they are presented. Adding together any patient counts is likely to result in an overestimate of the number of patients."
),
pfd_imd_data,
14