Online training class for Clinical R programming batch starts on Monday, 02Feb2026.
Click here for details.
sas - code - here
#==============================================================================
#01_make_dm_xpt.R
# Create DM.xpt
#==============================================================================
library(tidyverse)
out_dir <- file.path("output", "xpt")
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)
dm <- tibble::tribble(
~studyid, ~domain, ~usubjid, ~subjid, ~sex, ~age,
"CSG001", "DM", "CSG001-0001", "0001", "M", 35,
"CSG001", "DM", "CSG001-0002", "0002", "F", 29
)
dm <- dm %>%
dplyr::mutate(
dplyr::across(where(is.character), ~ stringr::str_sub(.x, 1, 200))
)
haven::write_xpt(dm, file.path(out_dir, "DM.xpt"))
#==============================================================================
#02_make_ae_xpt.R
# Create AE.xpt
#==============================================================================
library(tidyverse)
out_dir <- file.path("output", "xpt")
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)
ae <- tibble::tribble(
~studyid, ~domain, ~usubjid, ~aeseq, ~aeterm, ~aeser,
"CSG001", "AE", "CSG001-0001", 1, "Headache", "N",
"CSG001", "AE", "CSG001-0002", 1, "Nausea", "N"
)
ae <- ae %>%
dplyr::mutate(
dplyr::across(where(is.character), ~ stringr::str_sub(.x, 1, 200))
)
haven::write_xpt(ae, file.path(out_dir, "AE.xpt"))
#==============================================================================
#03_make_lb_xpt.R
# Create LB.xpt
#==============================================================================
library(tidyverse)
out_dir <- file.path("output", "xpt")
dir.create(out_dir, recursive = TRUE, showWarnings = FALSE)
lb <- tibble::tribble(
~studyid, ~domain, ~usubjid, ~lbtestcd, ~lbtest, ~lbstresn, ~lbstresu,
"CSG001", "LB", "CSG001-0001", "HGB", "Hemoglobin", 13.2, "g/dL"
)
lb <- lb %>%
dplyr::mutate(
dplyr::across(where(is.character), ~ stringr::str_sub(.x, 1, 200))
)
haven::write_xpt(lb, file.path(out_dir, "LB.xpt"))
#==============================================================================
#run_all.R
# Batch runner (SAS-style batch submit)
#==============================================================================
setwd("C:/Users/curio/Downloads/r_batch_xpt_example")
library(tidyverse)
scripts <- c(
file.path("programs", "01_make_dm_xpt.R"),
file.path("programs", "02_make_ae_xpt.R"),
file.path("programs", "03_make_lb_xpt.R")
)
log_dir <- file.path("output", "logs")
dir.create(log_dir, recursive = TRUE, showWarnings = FALSE)
for (s in scripts) {
log_file <- file.path(
log_dir,
paste0(tools::file_path_sans_ext(basename(s)), ".log")
)
system2(
command = "Rscript",
args = s,
stdout = log_file,
stderr = log_file
)
}
cat("Batch run completed/n")