Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
1346f70
Change order - aligns with sc methods
Jennit07 Sep 17, 2025
b882590
Move postcode code to own section
Jennit07 Sep 17, 2025
30eb049
Add latest record flag notes from sc team
Jennit07 Sep 17, 2025
9e2c124
Style code
Jennit07 Sep 17, 2025
7c3b103
Move arrange to make it more clear this is done
Jennit07 Sep 17, 2025
887befd
Use SC methodology to select latest record flag
Jennit07 Sep 18, 2025
84813a3
remove redundant code
Jennit07 Sep 18, 2025
8e416f8
Merge branch 'dec25-update' into sc_latest_record_flag
OluwatobiOni Sep 23, 2025
693ad34
sc: demog lookup by fy and align with sc
lizihao-anu Oct 31, 2025
47d9110
sc: replace scid with latest grouped by anon_chi
lizihao-anu Oct 31, 2025
0428f3c
sc: to adapt at with demog lookup
lizihao-anu Oct 31, 2025
c406c8b
sc: to adapt ch with demog lookup
lizihao-anu Oct 31, 2025
dd54988
sc: to adapt hc with demog lookup
lizihao-anu Oct 31, 2025
3c21a08
sc: to adapt sds with demog lookup
lizihao-anu Oct 31, 2025
cf2773c
sc: to adapt join_sc_client with demog lookup
lizihao-anu Oct 31, 2025
3a549d0
Style code
lizihao-anu Oct 31, 2025
a6d4166
Merge branch 'dec25-update' into ch_pc
lizihao-anu Oct 31, 2025
8ac1f8c
Update documentation
lizihao-anu Oct 31, 2025
885da83
bug fix for process_lookup_sc_demographics
lizihao-anu Oct 31, 2025
5ae0fb7
Merge branch 'dec25-update' into ch_pc
Jennit07 Nov 3, 2025
cf0a31a
update targets and join_sc_client()
lizihao-anu Nov 6, 2025
57be2ba
Merge branch 'dec25-update' into ch_pc
Jennit07 Nov 14, 2025
970694f
Merge branch 'dec25-update' into ch_pc
Jennit07 Nov 14, 2025
35f99d2
update process_lookup_sc_demographics.R
lizihao-anu Nov 14, 2025
2f1fa7c
Merge branch 'ch_pc' of github.com:Public-Health-Scotland/source-link…
lizihao-anu Nov 14, 2025
16e355b
Update documentation
lizihao-anu Nov 14, 2025
01d2f15
remove latest_record_flag
lizihao-anu Nov 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 12 additions & 32 deletions R/create_episode_file.R
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ create_episode_file <- function(
"anon_chi",
"person_id",
"social_care_id",
"linking_id",
"gender",
"dob",
"gpprac",
Expand Down Expand Up @@ -567,61 +568,40 @@ join_sc_client <- function(data,
}

if (file_type == "episode") {
# Match on client variables by chi
# Step 1. Link/join ep with sc_client by anon_chi,
# excluding episodes are not joined. We get `data_file_chi_join`
# Step 2. For the episodes are not joined in Step 1,
# join them with sc_client again by person_id,
# excluding those are not joined. We get `data_file_pi_join`
# Step 3. Episodes that are non-joined, is `data_file_unjoined`
# Step 4. bind rows together

# Match on client variables by linking_id
# Step 1
data_sc <- data %>%
dplyr::filter(.data$recid %in% c("AT", "HC", "CH", "SDS"))
data_non_sc <- data %>%
dplyr::filter(!(.data$recid %in% c("AT", "HC", "CH", "SDS")))

data_file_chi_join <- data_sc %>%
data_file_join <- data_sc %>%
dplyr::inner_join(
sc_client,
by = "anon_chi",
by = "linking_id",
relationship = "many-to-one",
suffix = c("", "_sc"),
# should never have NA in linking_id
na_matches = "never"
) %>%
dplyr::select(
-dplyr::ends_with("_sc")
)

# Step 2
data_file_pi_join <- data_sc %>%
data_file_unjoined <- data_sc %>%
dplyr::filter(!(
.data$ep_file_row_id %in% dplyr::pull(data_file_chi_join, .data$ep_file_row_id)
)) %>%
dplyr::inner_join(
sc_client,
by = "person_id",
relationship = "many-to-one",
suffix = c("", "_sc"),
na_matches = "never"
) %>%
dplyr::select(-dplyr::ends_with("_sc"))
.data$ep_file_row_id %in%
dplyr::pull(data_file_join, .data$ep_file_row_id)
))

# Step 3
data_file_unjoined <- data_sc %>%
dplyr::filter(!(.data$ep_file_row_id %in% c(
dplyr::pull(data_file_chi_join, .data$ep_file_row_id),
dplyr::pull(data_file_pi_join, .data$ep_file_row_id)
)))

# Step 4
data_file <- dplyr::bind_rows(
data_file_chi_join,
data_file_pi_join,
data_file_join,
data_file_unjoined,
data_non_sc
)
) %>%
select(-"linking_id")
} else {
data_file <- data %>%
dplyr::left_join(
Expand Down
43 changes: 43 additions & 0 deletions R/fy_helpers.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#' Add financial year from Social Care period
#' Financial year format, eg 2024
#' @param data social care data frame
#'
#' @returns social care data frame with financial year and financial quarter
add_fy_qtr_from_period <- function(data) {
data %>%
# create financial_year and financial_quarter variables for sorting
dplyr::mutate(
financial_year = as.numeric(stringr::str_sub(.data$period, 1, 4)),
financial_quarter = stringr::str_sub(.data$period, 6, 6)
) %>%
# set financial quarter to 5 when there is only an annual submission -
# for ordering periods with annual submission last
dplyr::mutate(
financial_quarter = dplyr::if_else(
is.na(.data$financial_quarter) |
.data$financial_quarter == "",
"5",
.data$financial_quarter
)
)
}


#' which_fy
#' Extract financial year from a date
#'
#' @param date a date variable
#' @param format calenddar year format 2024, or financial year format, 2425
#' @returns financial year
which_fy <- function(date, format = c("year", "fyear")) {
year <- as.numeric(format(date, "%Y"))
month <- as.numeric(format(date, "%m"))

start_year <- ifelse(month < 4, year - 1, year)
end_year <- start_year + 1
if (format == "year") {
return(start_year)
} else {
return(paste0(substr(start_year, 3, 4), substr(end_year, 3, 4)))
}
}
Loading
Loading