As a clinical trial statistician or programmer, answer three short questions in Your review: the task, the inputs and outputs, and the AI-assisted review. Agreeing with all three approves this benchmark; disagreeing with any needs a comment. Propose a fix with the Edit button on any source file, or add it to your comments.
We suggest spending no more than 5 minutes on a review. The other tabs beside Your review are background for assessment, not under review.
Summary
README.mdPublic entries: Python and R. Passing coverage requires an installed inventory.
Goal: build the SDTM subject visits (SV) domain from Operational Data Model (ODM) item data plus a demographics (DM) extract, naming for each visit the study events its subjects attended, when each visit started and ended, and the planned day, order, and epoch it belongs to.
Input: an ODM extract, one row per item value with the columns StudyOID, MetaDataVersionOID, SubjectKey, StudyEventOID, StudyEventRepeatKey, ItemGroupOID, ItemGroupRepeatKey, ItemOID and Value; plus a small DM extract with STUDYID, USUBJID and the reference start date RFSTDTC. Each study event carries its planned-visit attributes (visit name, visit number, planned day, planned order, epoch) as ODM items, and the VS, LB and EX forms carry their own collection dates.
Variables:
SVSEQnumbers each subject's visits in date order.VISITis the visit name carried on the study event, e.g. Week 2.VISITNUMis the planned visit number. Distinct numbers starting at 99.1 identify a subject's unscheduled visits.VISITDYis the planned study day of the visit; it stays empty for unscheduled visits, which carry no plan.SVSTDTCis the start date of the visit, the earliest form date among the visit's VS, LB and EX forms.SVENDTCis the end date of the visit, the latest form date; a visit whose forms were collected on different days spans them.SVSTDYis the study day of the visit start, counted from the subject's RFSTDTC.SVENDYis the study day of the visit end, counted from the subject's RFSTDTC.TAETORDis the planned order of the element within the arm; it stays empty for unscheduled visits.EPOCHis the epoch carried on the study event, e.g. TREATMENT.SVUPDESis the description the site entered for the unplanned visit, carried on the unscheduled study event as an ODM item; it stays empty for planned visits.
Note: visits are numbered by their start date, so an unscheduled visit takes its place between the planned visits around it. Repeated unscheduled visits retain separate identities.
Provenance: the fixtures are hand-built from a realistic EDC visit flow with plausible ODM naming, not records from a real study.
Inputs
Under review2 source filesinput/dm.csv
Edit2 rows| STUDYID | USUBJID | RFSTDTC |
|---|---|---|
| YAMAA-01 | 2026-02-17 | |
| YAMAA-01 | 2026-02-18 |
input/odm.csv
Edit54 rows| StudyOID | MetaDataVersionOID | SubjectKey | StudyEventOID | StudyEventRepeatKey | ItemGroupOID | ItemGroupRepeatKey | ItemOID | Value |
|---|---|---|---|---|---|---|---|---|
| YAMAA-01 | MDV.1 | 001 | SCRN | 1 | IG.TV | 1 | IT.TV.VISIT | Screening |
| YAMAA-01 | MDV.1 | 001 | SCRN | 1 | IG.TV | 1 | IT.TV.VISITNUM | -1 |
| YAMAA-01 | MDV.1 | 001 | SCRN | 1 | IG.TV | 1 | IT.TV.VISITDY | -7 |
| YAMAA-01 | MDV.1 | 001 | SCRN | 1 | IG.TV | 1 | IT.TV.TAETORD | 1 |
| YAMAA-01 | MDV.1 | 001 | SCRN | 1 | IG.TV | 1 | IT.TV.EPOCH | SCREENING |
| YAMAA-01 | MDV.1 | 001 | SCRN | 1 | IG.VS | 1 | IT.VS.VSDTC | 2026-02-10 |
| YAMAA-01 | MDV.1 | 001 | BL | 1 | IG.TV | 1 | IT.TV.VISIT | Baseline |
| YAMAA-01 | MDV.1 | 001 | BL | 1 | IG.TV | 1 | IT.TV.VISITNUM | 1 |
| YAMAA-01 | MDV.1 | 001 | BL | 1 | IG.TV | 1 | IT.TV.VISITDY | 1 |
| YAMAA-01 | MDV.1 | 001 | BL | 1 | IG.TV | 1 | IT.TV.TAETORD | 2 |
| YAMAA-01 | MDV.1 | 001 | BL | 1 | IG.TV | 1 | IT.TV.EPOCH | TREATMENT |
| YAMAA-01 | MDV.1 | 001 | BL | 1 | IG.VS | 1 | IT.VS.VSDTC | 2026-02-17 |
| YAMAA-01 | MDV.1 | 001 | BL | 1 | IG.LB | 1 | IT.LB.LBDTC | 2026-02-17 |
| YAMAA-01 | MDV.1 | 001 | BL | 1 | IG.EX | 1 | IT.EX.EXDTC | 2026-02-17 |
| YAMAA-01 | MDV.1 | 001 | UNSCH | 1 | IG.TV | 1 | IT.TV.VISIT | Unscheduled |
| YAMAA-01 | MDV.1 | 001 | UNSCH | 1 | IG.TV | 1 | IT.TV.VISITNUM | 99.1 |
| YAMAA-01 | MDV.1 | 001 | UNSCH | 1 | IG.TV | 1 | IT.TV.UPDES | Unscheduled safety visit |
| YAMAA-01 | MDV.1 | 001 | UNSCH | 1 | IG.TV | 1 | IT.TV.EPOCH | TREATMENT |
| YAMAA-01 | MDV.1 | 001 | UNSCH | 1 | IG.VS | 1 | IT.VS.VSDTC | 2026-03-02 |
| YAMAA-01 | MDV.1 | 001 | UNSCH | 2 | IG.TV | 1 | IT.TV.VISIT | Unscheduled |
| YAMAA-01 | MDV.1 | 001 | UNSCH | 2 | IG.TV | 1 | IT.TV.VISITNUM | 99.2 |
| YAMAA-01 | MDV.1 | 001 | UNSCH | 2 | IG.TV | 1 | IT.TV.UPDES | Unscheduled follow-up visit |
| YAMAA-01 | MDV.1 | 001 | UNSCH | 2 | IG.TV | 1 | IT.TV.EPOCH | TREATMENT |
| YAMAA-01 | MDV.1 | 001 | UNSCH | 2 | IG.VS | 1 | IT.VS.VSDTC | 2026-03-04 |
| YAMAA-01 | MDV.1 | 001 | W2 | 1 | IG.TV | 1 | IT.TV.VISIT | Week 2 |
| YAMAA-01 | MDV.1 | 001 | W2 | 1 | IG.TV | 1 | IT.TV.VISITNUM | 2 |
| YAMAA-01 | MDV.1 | 001 | W2 | 1 | IG.TV | 1 | IT.TV.VISITDY | 15 |
| YAMAA-01 | MDV.1 | 001 | W2 | 1 | IG.TV | 1 | IT.TV.TAETORD | 3 |
| YAMAA-01 | MDV.1 | 001 | W2 | 1 | IG.TV | 1 | IT.TV.EPOCH | TREATMENT |
| YAMAA-01 | MDV.1 | 001 | W2 | 1 | IG.VS | 1 | IT.VS.VSDTC | 2026-03-05 |
| YAMAA-01 | MDV.1 | 001 | W2 | 1 | IG.LB | 1 | IT.LB.LBDTC | 2026-03-06 |
| YAMAA-01 | MDV.1 | 001 | W2 | 1 | IG.EX | 1 | IT.EX.EXDTC | 2026-03-05 |
| YAMAA-01 | MDV.1 | 002 | SCRN | 1 | IG.TV | 1 | IT.TV.VISIT | Screening |
| YAMAA-01 | MDV.1 | 002 | SCRN | 1 | IG.TV | 1 | IT.TV.VISITNUM | -1 |
| YAMAA-01 | MDV.1 | 002 | SCRN | 1 | IG.TV | 1 | IT.TV.VISITDY | -7 |
| YAMAA-01 | MDV.1 | 002 | SCRN | 1 | IG.TV | 1 | IT.TV.TAETORD | 1 |
| YAMAA-01 | MDV.1 | 002 | SCRN | 1 | IG.TV | 1 | IT.TV.EPOCH | SCREENING |
| YAMAA-01 | MDV.1 | 002 | SCRN | 1 | IG.VS | 1 | IT.VS.VSDTC | 2026-02-11 |
| YAMAA-01 | MDV.1 | 002 | BL | 1 | IG.TV | 1 | IT.TV.VISIT | Baseline |
| YAMAA-01 | MDV.1 | 002 | BL | 1 | IG.TV | 1 | IT.TV.VISITNUM | 1 |
| YAMAA-01 | MDV.1 | 002 | BL | 1 | IG.TV | 1 | IT.TV.VISITDY | 1 |
| YAMAA-01 | MDV.1 | 002 | BL | 1 | IG.TV | 1 | IT.TV.TAETORD | 2 |
| YAMAA-01 | MDV.1 | 002 | BL | 1 | IG.TV | 1 | IT.TV.EPOCH | TREATMENT |
| YAMAA-01 | MDV.1 | 002 | BL | 1 | IG.VS | 1 | IT.VS.VSDTC | 2026-02-18 |
| YAMAA-01 | MDV.1 | 002 | BL | 1 | IG.LB | 1 | IT.LB.LBDTC | 2026-02-18 |
| YAMAA-01 | MDV.1 | 002 | BL | 1 | IG.EX | 1 | IT.EX.EXDTC | 2026-02-18 |
| YAMAA-01 | MDV.1 | 002 | W2 | 1 | IG.TV | 1 | IT.TV.VISIT | Week 2 |
| YAMAA-01 | MDV.1 | 002 | W2 | 1 | IG.TV | 1 | IT.TV.VISITNUM | 2 |
| YAMAA-01 | MDV.1 | 002 | W2 | 1 | IG.TV | 1 | IT.TV.VISITDY | 15 |
| YAMAA-01 | MDV.1 | 002 | W2 | 1 | IG.TV | 1 | IT.TV.TAETORD | 3 |
| YAMAA-01 | MDV.1 | 002 | W2 | 1 | IG.TV | 1 | IT.TV.EPOCH | TREATMENT |
| YAMAA-01 | MDV.1 | 002 | W2 | 1 | IG.VS | 1 | IT.VS.VSDTC | 2026-03-07 |
| YAMAA-01 | MDV.1 | 002 | W2 | 1 | IG.LB | 1 | IT.LB.LBDTC | 2026-03-07 |
| YAMAA-01 | MDV.1 | 002 | W2 | 1 | IG.EX | 1 | IT.EX.EXDTC | 2026-03-07 |
Expected output
Under review8 expected rowsexpected/sv.csv
Edit8 rows| DOMAIN | STUDYID | USUBJID | SVSEQ | VISIT | VISITNUM | VISITDY | SVSTDTC | SVENDTC | SVSTDY | SVENDY | TAETORD | EPOCH | SVUPDES |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| SV | YAMAA-01 | 1 | Screening | -1 | -7 | 2026-02-10 | 2026-02-10 | -7 | -7 | 1 | SCREENING | — | |
| SV | YAMAA-01 | 2 | Baseline | 1 | 1 | 2026-02-17 | 2026-02-17 | 1 | 1 | 2 | TREATMENT | — | |
| SV | YAMAA-01 | 3 | Unscheduled | 99.1 | — | 2026-03-02 | 2026-03-02 | 14 | 14 | — | TREATMENT | Unscheduled safety visit | |
| SV | YAMAA-01 | 4 | Unscheduled | 99.2 | — | 2026-03-04 | 2026-03-04 | 16 | 16 | — | TREATMENT | Unscheduled follow-up visit | |
| SV | YAMAA-01 | 5 | Week 2 | 2 | 15 | 2026-03-05 | 2026-03-06 | 17 | 18 | 3 | TREATMENT | — | |
| SV | YAMAA-01 | 1 | Screening | -1 | -7 | 2026-02-11 | 2026-02-11 | -7 | -7 | 1 | SCREENING | — | |
| SV | YAMAA-01 | 2 | Baseline | 1 | 1 | 2026-02-18 | 2026-02-18 | 1 | 1 | 2 | TREATMENT | — | |
| SV | YAMAA-01 | 3 | Week 2 | 2 | 15 | 2026-03-07 | 2026-03-07 | 18 | 18 | 3 | TREATMENT | — |
Human review and assessment
| Variable Name | Variable Label | Type | Length | Controlled Terms or Format | Origin | Core | Conversion Definition | Variable Type | Variable Order | Comments for Define |
|---|---|---|---|---|---|---|---|---|---|---|
| DOMAIN | Domain Abbreviation | Char | Assigned | Set constant value "SV". | SDTM | 1 | ||||
| STUDYID | Study Identifier | Char | Collected | Copy value from ODM.StudyOID. | SDTM | 2 | ||||
| USUBJID | Unique Subject Identifier | Char | Collected | Copy value from ODM.SubjectKey. | SDTM | 3 | ||||
| VISIT | Visit Name | Char | Derived | Set per constructed row; see the Row construction sheet. | SDTM | 4 | ||||
| VISITNUM | Visit Number | Num | Derived | Set per constructed row; see the Row construction sheet. | SDTM | 5 | ||||
| VISITDY | Planned Study Day of Visit | Num | Derived | Set per constructed row; see the Row construction sheet. | SDTM | 6 | ||||
| SVSTDTC | Start Date/Time of Visit | Char | ISO 8601 | Derived | Least of VSDTC, LBDTC and EXDTC. | SDTM | 7 | |||
| SVENDTC | End Date/Time of Visit | Char | ISO 8601 | Derived | Greatest of VSDTC, LBDTC and EXDTC. | SDTM | 8 | |||
| SVSTDY | Study Day of Start of Visit | Num | Derived | CDISC study day of SVSTDTC against RFSTDTC as day 1; there is no day zero. | SDTM | 9 | ||||
| SVENDY | Study Day of End of Visit | Num | Derived | CDISC study day of SVENDTC against RFSTDTC as day 1; there is no day zero. | SDTM | 10 | ||||
| TAETORD | Planned Order of Element within Arm | Num | Derived | Set per constructed row; see the Row construction sheet. | SDTM | 11 | ||||
| EPOCH | Epoch | Char | Derived | Set per constructed row; see the Row construction sheet. | SDTM | 12 | ||||
| SVUPDES | Description of Unplanned Visit | Char | Derived | Set per constructed row; see the Row construction sheet. | SDTM | 13 | ||||
| SVSEQ | Sequence Number | Num | Derived | Row number within each group of STUDYID, USUBJID, ordered by SVSTDTC. | SDTM | 14 |
| Template | Include when | VISIT | VISITNUM | VISITDY | VSDTC | LBDTC | EXDTC | TAETORD | EPOCH | SVUPDES |
|---|---|---|---|---|---|---|---|---|---|---|
| visit | Aggregate ONLY(ODM.Value) over records matching on the shared output keys, where ODM.ItemOID = 'IT.TV.VISIT' | Aggregate ONLY(ODM.Value) over records matching on the shared output keys, where ODM.ItemOID = 'IT.TV.VISITNUM' | Aggregate ONLY(ODM.Value) over records matching on the shared output keys, where ODM.ItemOID = 'IT.TV.VISITDY' | Aggregate ONLY(ODM.Value) over records matching on the shared output keys, where ODM.ItemOID = 'IT.VS.VSDTC' | Aggregate ONLY(ODM.Value) over records matching on the shared output keys, where ODM.ItemOID = 'IT.LB.LBDTC' | Aggregate ONLY(ODM.Value) over records matching on the shared output keys, where ODM.ItemOID = 'IT.EX.EXDTC' | Aggregate ONLY(ODM.Value) over records matching on the shared output keys, where ODM.ItemOID = 'IT.TV.TAETORD' | Aggregate ONLY(ODM.Value) over records matching on the shared output keys, where ODM.ItemOID = 'IT.TV.EPOCH' | Aggregate ONLY(ODM.Value) over records matching on the shared output keys, where ODM.ItemOID = 'IT.TV.UPDES' |
| Version | Date | Author | Description | Reviewer | Sign-off |
|---|---|---|---|---|---|
| 1.7 | 2026-10-20 | yamaa docs build | Read a specification in the 1.0 surface as its 0.5 counterpart; state that missing values stay missing where a mapping declares no missing handler, and leave out a declared missing answer that restates a handler's default | ||
| 1.6 | 2026-10-17 | yamaa docs build | Describe collected date parts written as ISO 8601 date text and times of day counted in seconds since midnight | ||
| 1.5 | 2026-10-17 | yamaa docs build | Name collected precision in words instead of letter codes; state imputation fallbacks, minimum source precision, bounds ignored outside the collected period, and invalid text with no handler as an error; describe date and datetime shifts, imputation flag codes, and partial text giving no date part | ||
| 1.4 | 2026-10-15 | yamaa docs build | State datetime precision codes through the hour and minute, and complete only the time parts a collected datetime lacks | ||
| 1.3 | 2026-09-29 | yamaa docs build | Show study codelist names, external dictionaries, and ISO 8601 formats in variable rows; list the document's codelist values and metadata | ||
| 1.2 | 2026-09-29 | yamaa docs build | Classify ADaM copies from a declared input as Predecessor and recodes as Derived, and SDTM recodes of locally derived columns as Derived | ||
| 1.1 | 2026-09-29 | yamaa docs build | Resolve lookups into the donor record they select, and give every derivation shape a plain-language rule instead of its raw YAML | ||
| 1.0 | yamaa docs build | Initial generation from spec.yaml |
prompts/sdtm-sv-subject-visits/full.md
Edit28 linesFollowing CDISC SDTM standards, use the provided DM and ODM datasets to create an SV dataset with one record per subject per visit.
The output dataset should contain the following columns in this order: DOMAIN, STUDYID, USUBJID, SVSEQ, VISIT, VISITNUM, VISITDY, SVSTDTC, SVENDTC, SVSTDY, SVENDY, TAETORD, EPOCH, SVUPDES
DOMAIN is "SV" on every record. STUDYID is the study OID and USUBJID the subject key, each as given in ODM. Each study event occurrence for a subject gives one record, and a repeated unscheduled event gives a record of its own. VISIT is the visit name carried on the study event. VISITNUM is the planned visit number carried on the study event, and distinct numbers starting at 99.1 identify a subject's unscheduled visits. VISITDY is the planned study day carried on the study event, and it has no value for an unscheduled visit, which carries no plan. SVSTDTC is the earliest of the form dates the visit's VS, LB and EX forms carry, and SVENDTC is the latest of them, so a visit whose forms were collected on different days spans them. SVSTDY is the study day of the visit start and SVENDY the study day of the visit end, each counted from the subject's RFSTDTC in DM. TAETORD is the planned order of the element within the arm carried on the study event, and it has no value for an unscheduled visit. EPOCH is the epoch carried on the study event. SVUPDES is the description the site entered for the unplanned visit, carried on the unscheduled study event, and it has no value for a planned visit. SVSEQ numbers each subject's visits from 1 in order of SVSTDTC, so an unscheduled visit takes its place between the planned visits around it.
Read the source datasets from /app/input and save the completed dataset as /app/output/sv.csv.
prompts/sdtm-sv-subject-visits/conventions.md
Edit13 linesFollowing CDISC SDTM standards, use the provided DM and ODM datasets to create an SV dataset with one record per subject per visit.
The output dataset should contain the following columns in this order: DOMAIN, STUDYID, USUBJID, SVSEQ, VISIT, VISITNUM, VISITDY, SVSTDTC, SVENDTC, SVSTDY, SVENDY, TAETORD, EPOCH, SVUPDES
DOMAIN is "SV" on every record. SVSTDY is the study day of the visit start and SVENDY the study day of the visit end, each counted from the subject's RFSTDTC in DM.
Read the source datasets from /app/input and save the completed dataset as /app/output/sv.csv.
prompts/sdtm-sv-subject-visits/brief.md
Edit9 linesFollowing CDISC SDTM standards, use the provided DM and ODM datasets to create an SV dataset with one record per subject per visit.
The output dataset should contain the following columns in this order: DOMAIN, STUDYID, USUBJID, SVSEQ, VISIT, VISITNUM, VISITDY, SVSTDTC, SVENDTC, SVSTDY, SVENDY, TAETORD, EPOCH, SVUPDES
Read the source datasets from /app/input and save the completed dataset as /app/output/sv.csv.
R
Edit3 lineslibrary(yamaa)yamaa_spec("spec.yaml")$run()$outputPython
Edit3 linesimport yamaayamaa.spec("spec.yaml").run().outputsolutions/sdtm-sv-subject-visits/result.R
Edit79 lines# Reference solution for the yamaa benchmark sdtm-sv-subject-visits (R track).## Written from the benchmark's full prompt and its inputs alone, as an agent# would write it. Harbor's oracle agent runs it to check that the prompt can# be solved and that the grader scores a correct answer 1.library(dplyr, warn.conflicts = FALSE)library(lubridate)library(readr)library(tidyr)study_day <- function(ref, coll) { if (is.na(ref) || is.na(coll)) { return(NA_integer_) } delta <- as.integer(coll - ref) if (delta >= 0L) delta + 1L else delta}dm <- read_csv( "/app/input/dm.csv", col_types = cols(.default = col_character()))odm <- read_csv( "/app/input/odm.csv", col_types = cols(.default = col_character()))ref_map <- setNames(suppressWarnings(ymd(dm$RFSTDTC)), dm$USUBJID)wide <- odm |> mutate(visit_key = paste(SubjectKey, StudyEventOID, StudyEventRepeatKey, sep = "|")) |> select(visit_key, SubjectKey, StudyOID, StudyEventOID, ItemOID, Value) |> pivot_wider(names_from = ItemOID, values_from = Value, values_fn = first)sv <- wide |> rowwise() |> mutate( dates = list(na.omit(c(IT.VS.VSDTC, IT.LB.LBDTC, IT.EX.EXDTC))), SVSTDTC = if (length(dates) == 0) NA_character_ else min(dates), SVENDTC = if (length(dates) == 0) NA_character_ else max(dates) ) |> ungroup() |> filter(!is.na(SVSTDTC)) |> transmute( DOMAIN = "SV", STUDYID = StudyOID, USUBJID = SubjectKey, VISIT = IT.TV.VISIT, VISITNUM = suppressWarnings(as.numeric(IT.TV.VISITNUM)), VISITDY = suppressWarnings(as.integer(IT.TV.VISITDY)), SVSTDTC, SVENDTC, SVSTDY = vapply( seq_len(n()), function(i) study_day(ref_map[[USUBJID[i]]], suppressWarnings(ymd(SVSTDTC[i]))), integer(1) ), SVENDY = vapply( seq_len(n()), function(i) study_day(ref_map[[USUBJID[i]]], suppressWarnings(ymd(SVENDTC[i]))), integer(1) ), TAETORD = suppressWarnings(as.integer(IT.TV.TAETORD)), EPOCH = IT.TV.EPOCH, SVUPDES = IT.TV.UPDES ) |> arrange(USUBJID, SVSTDTC) |> group_by(USUBJID) |> mutate(SVSEQ = row_number()) |> ungroup() |> arrange(USUBJID, SVSEQ) |> select( DOMAIN, STUDYID, USUBJID, SVSEQ, VISIT, VISITNUM, VISITDY, SVSTDTC, SVENDTC, SVSTDY, SVENDY, TAETORD, EPOCH, SVUPDES )dir.create("/app/output", showWarnings = FALSE)write_csv(sv, "/app/output/sv.csv", na = "")solutions/sdtm-sv-subject-visits/result.py
Edit119 lines# Reference solution for the yamaa benchmark sdtm-sv-subject-visits (Python track).## Written from the benchmark's full prompt and its inputs alone, as an agent# would write it. Harbor's oracle agent runs it to check that the prompt can# be solved and that the grader scores a correct answer 1."""Build the SDTM SV dataset from the DM and ODM source datasets.One record per subject per study event occurrence. Visit start and enddates are the earliest and latest of the form dates carried by thevisit's VS, LB and EX forms; study days are counted from the subject'sRFSTDTC in DM with no day zero."""import csvfrom datetime import dateINPUT_DIR = "/app/input"OUTPUT_DIR = "/app/output"COLUMNS = [ "DOMAIN", "STUDYID", "USUBJID", "SVSEQ", "VISIT", "VISITNUM", "VISITDY", "SVSTDTC", "SVENDTC", "SVSTDY", "SVENDY", "TAETORD", "EPOCH", "SVUPDES",]# Item groups whose dates bound the visit.FORM_GROUPS = {"IG.VS", "IG.LB", "IG.EX"}def parse_date(text): return date.fromisoformat(text)def study_day(day, reference): delta = (day - reference).days return delta + 1 if delta >= 0 else deltadef main(): # Reference start dates from DM, keyed by (STUDYID, USUBJID). rfstdtc = {} with open(f"{INPUT_DIR}/dm.csv", newline="") as handle: for row in csv.DictReader(handle): rfstdtc[(row["STUDYID"], row["USUBJID"])] = row["RFSTDTC"] # Group the ODM items by subject and study event occurrence. events = {} order = [] with open(f"{INPUT_DIR}/odm.csv", newline="") as handle: for row in csv.DictReader(handle): key = ( row["StudyOID"], row["SubjectKey"], row["StudyEventOID"], row["StudyEventRepeatKey"], ) if key not in events: events[key] = {"tv": {}, "dates": []} order.append(key) event = events[key] if row["ItemGroupOID"] == "IG.TV": event["tv"][row["ItemOID"]] = row["Value"] elif row["ItemGroupOID"] in FORM_GROUPS and row["Value"]: event["dates"].append(row["Value"]) records = [] for key in order: study_oid, subject_key, _, _ = key event = events[key] tv = event["tv"] dates = sorted(event["dates"]) svstdtc = dates[0] if dates else "" svendtc = dates[-1] if dates else "" reference = rfstdtc.get((study_oid, subject_key), "") svstdy = svendy = "" if reference: ref_date = parse_date(reference) if svstdtc: svstdy = study_day(parse_date(svstdtc), ref_date) if svendtc: svendy = study_day(parse_date(svendtc), ref_date) records.append({ "DOMAIN": "SV", "STUDYID": study_oid, "USUBJID": subject_key, "VISIT": tv.get("IT.TV.VISIT", ""), "VISITNUM": tv.get("IT.TV.VISITNUM", ""), "VISITDY": tv.get("IT.TV.VISITDY", ""), "SVSTDTC": svstdtc, "SVENDTC": svendtc, "SVSTDY": svstdy, "SVENDY": svendy, "TAETORD": tv.get("IT.TV.TAETORD", ""), "EPOCH": tv.get("IT.TV.EPOCH", ""), "SVUPDES": tv.get("IT.TV.UPDES", ""), "_sort": svstdtc, }) # SVSEQ numbers each subject's visits from 1 in SVSTDTC order. by_subject = {} for record in records: by_subject.setdefault(record["USUBJID"], []).append(record) sequenced = [] for subject in by_subject: visits = sorted(by_subject[subject], key=lambda r: r["_sort"]) for index, record in enumerate(visits, start=1): record["SVSEQ"] = index sequenced.append(record) with open(f"{OUTPUT_DIR}/sv.csv", "w", newline="") as handle: writer = csv.DictWriter(handle, fieldnames=COLUMNS) writer.writeheader() for record in sequenced: writer.writerow({name: record[name] for name in COLUMNS})if __name__ == "__main__": main()solutions/sdtm-sv-subject-visits/result.sas
Edit46 lines/* opensas reference solution for the yamaa benchmark sdtm-sv-subject-visits. Reads the benchmark inputs and derives the requested output. Execute with the existing opensas runtime: opensas /app/output/result.sas. */data dm; length STUDYID USUBJID RFSTDTC $1024; infile "/app/input/dm.csv" dsd dlm="," firstobs=2 truncover lrecl=32767; input STUDYID : $char1024. USUBJID : $char1024. RFSTDTC : $char1024.;run;data odm; length StudyOID MetaDataVersionOID SubjectKey StudyEventOID StudyEventRepeatKey ItemGroupOID ItemGroupRepeatKey ItemOID Value $1024; infile "/app/input/odm.csv" dsd dlm="," firstobs=2 truncover lrecl=32767; input StudyOID : $char1024. MetaDataVersionOID : $char1024. SubjectKey : $char1024. StudyEventOID : $char1024. StudyEventRepeatKey : $char1024. ItemGroupOID : $char1024. ItemGroupRepeatKey : $char1024. ItemOID : $char1024. Value : $char1024.;run;proc sort data=odm out=ordered; by StudyOID SubjectKey StudyEventOID StudyEventRepeatKey; run;data visits; length VISIT VNUM VDAY ORD EPOCH SVUPDES $1024; retain VISIT VNUM VDAY ORD EPOCH SVUPDES; set ordered; by StudyOID SubjectKey StudyEventOID StudyEventRepeatKey; if first.StudyEventRepeatKey then do; VISIT=''; VNUM=''; VDAY=''; ORD=''; EPOCH=''; SVUPDES=''; end; if ItemOID='IT.TV.VISIT' then VISIT=Value; if ItemOID='IT.TV.VISITNUM' then VNUM=Value; if ItemOID='IT.TV.VISITDY' then VDAY=Value; if ItemOID='IT.TV.TAETORD' then ORD=Value; if ItemOID='IT.TV.EPOCH' then EPOCH=Value; if ItemOID='IT.TV.UPDES' then SVUPDES=Value; if last.StudyEventRepeatKey then output;run;proc sql;create table dates as select StudyOID,SubjectKey,StudyEventOID,StudyEventRepeatKey,min(Value) as SVSTDTC,max(Value) as SVENDTC from odm where ItemOID in ('IT.VS.VSDTC','IT.LB.LBDTC','IT.EX.EXDTC') and not missing(Value) group by StudyOID,SubjectKey,StudyEventOID,StudyEventRepeatKey;create table joined as select a.*,b.SVSTDTC,b.SVENDTC,c.RFSTDTC from visits a inner join dates b on a.StudyOID=b.StudyOID and a.SubjectKey=b.SubjectKey and a.StudyEventOID=b.StudyEventOID and a.StudyEventRepeatKey=b.StudyEventRepeatKey left join dm c on a.StudyOID=c.STUDYID and a.SubjectKey=c.USUBJID; quit;data prepared; set joined; length DOMAIN $2 STUDYID USUBJID $1024; DOMAIN='SV'; STUDYID=StudyOID; USUBJID=SubjectKey; VISITNUM=input(VNUM,best32.); VISITDY=input(VDAY,best32.); TAETORD=input(ORD,best32.); SVSTDY=.; SVENDY=.; if not missing(RFSTDTC) then do; SVSTDY=input(SVSTDTC,yymmdd10.)-input(RFSTDTC,yymmdd10.); if SVSTDY>=0 then SVSTDY+1; SVENDY=input(SVENDTC,yymmdd10.)-input(RFSTDTC,yymmdd10.); if SVENDY>=0 then SVENDY+1; end; run;proc sort data=prepared; by STUDYID USUBJID SVSTDTC StudyEventOID StudyEventRepeatKey; run;data result; set prepared; by STUDYID USUBJID; retain SVSEQ UNSCH;if first.USUBJID then do; SVSEQ=0; UNSCH=0; end; SVSEQ+1;if StudyEventOID='UNSCH' then do; UNSCH+1; VISITNUM=99+UNSCH/10; VISITDY=.; TAETORD=.; end; else SVUPDES=''; run;proc sql; create table final as select DOMAIN, STUDYID, USUBJID, SVSEQ, VISIT, VISITNUM, VISITDY, SVSTDTC, SVENDTC, SVSTDY, SVENDY, TAETORD, EPOCH, SVUPDES from result;quit;proc export data=final outfile="/app/output/sv.csv" dbms=csv replace; run;
yamaa