Review ask

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

Lifecycle: draftEditUnder review
What this benchmark means
README.md

Public 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:

  • SVSEQ numbers each subject's visits in date order.
  • VISIT is the visit name carried on the study event, e.g. Week 2.
  • VISITNUM is the planned visit number. Distinct numbers starting at 99.1 identify a subject's unscheduled visits.
  • VISITDY is the planned study day of the visit; it stays empty for unscheduled visits, which carry no plan.
  • SVSTDTC is the start date of the visit, the earliest form date among the visit's VS, LB and EX forms.
  • SVENDTC is the end date of the visit, the latest form date; a visit whose forms were collected on different days spans them.
  • SVSTDY is the study day of the visit start, counted from the subject's RFSTDTC.
  • SVENDY is the study day of the visit end, counted from the subject's RFSTDTC.
  • TAETORD is the planned order of the element within the arm; it stays empty for unscheduled visits.
  • EPOCH is the epoch carried on the study event, e.g. TREATMENT.
  • SVUPDES is 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 review
2 source files

input/dm.csv

Edit
2 rows
input/dm.csv
STUDYIDUSUBJIDRFSTDTC
YAMAA-012026-02-17
YAMAA-012026-02-18

input/odm.csv

Edit
54 rows
input/odm.csv
StudyOIDMetaDataVersionOIDSubjectKeyStudyEventOIDStudyEventRepeatKeyItemGroupOIDItemGroupRepeatKeyItemOIDValue
YAMAA-01MDV.1001SCRN1IG.TV1IT.TV.VISITScreening
YAMAA-01MDV.1001SCRN1IG.TV1IT.TV.VISITNUM-1
YAMAA-01MDV.1001SCRN1IG.TV1IT.TV.VISITDY-7
YAMAA-01MDV.1001SCRN1IG.TV1IT.TV.TAETORD1
YAMAA-01MDV.1001SCRN1IG.TV1IT.TV.EPOCHSCREENING
YAMAA-01MDV.1001SCRN1IG.VS1IT.VS.VSDTC2026-02-10
YAMAA-01MDV.1001BL1IG.TV1IT.TV.VISITBaseline
YAMAA-01MDV.1001BL1IG.TV1IT.TV.VISITNUM1
YAMAA-01MDV.1001BL1IG.TV1IT.TV.VISITDY1
YAMAA-01MDV.1001BL1IG.TV1IT.TV.TAETORD2
YAMAA-01MDV.1001BL1IG.TV1IT.TV.EPOCHTREATMENT
YAMAA-01MDV.1001BL1IG.VS1IT.VS.VSDTC2026-02-17
YAMAA-01MDV.1001BL1IG.LB1IT.LB.LBDTC2026-02-17
YAMAA-01MDV.1001BL1IG.EX1IT.EX.EXDTC2026-02-17
YAMAA-01MDV.1001UNSCH1IG.TV1IT.TV.VISITUnscheduled
YAMAA-01MDV.1001UNSCH1IG.TV1IT.TV.VISITNUM99.1
YAMAA-01MDV.1001UNSCH1IG.TV1IT.TV.UPDESUnscheduled safety visit
YAMAA-01MDV.1001UNSCH1IG.TV1IT.TV.EPOCHTREATMENT
YAMAA-01MDV.1001UNSCH1IG.VS1IT.VS.VSDTC2026-03-02
YAMAA-01MDV.1001UNSCH2IG.TV1IT.TV.VISITUnscheduled
YAMAA-01MDV.1001UNSCH2IG.TV1IT.TV.VISITNUM99.2
YAMAA-01MDV.1001UNSCH2IG.TV1IT.TV.UPDESUnscheduled follow-up visit
YAMAA-01MDV.1001UNSCH2IG.TV1IT.TV.EPOCHTREATMENT
YAMAA-01MDV.1001UNSCH2IG.VS1IT.VS.VSDTC2026-03-04
YAMAA-01MDV.1001W21IG.TV1IT.TV.VISITWeek 2
YAMAA-01MDV.1001W21IG.TV1IT.TV.VISITNUM2
YAMAA-01MDV.1001W21IG.TV1IT.TV.VISITDY15
YAMAA-01MDV.1001W21IG.TV1IT.TV.TAETORD3
YAMAA-01MDV.1001W21IG.TV1IT.TV.EPOCHTREATMENT
YAMAA-01MDV.1001W21IG.VS1IT.VS.VSDTC2026-03-05
YAMAA-01MDV.1001W21IG.LB1IT.LB.LBDTC2026-03-06
YAMAA-01MDV.1001W21IG.EX1IT.EX.EXDTC2026-03-05
YAMAA-01MDV.1002SCRN1IG.TV1IT.TV.VISITScreening
YAMAA-01MDV.1002SCRN1IG.TV1IT.TV.VISITNUM-1
YAMAA-01MDV.1002SCRN1IG.TV1IT.TV.VISITDY-7
YAMAA-01MDV.1002SCRN1IG.TV1IT.TV.TAETORD1
YAMAA-01MDV.1002SCRN1IG.TV1IT.TV.EPOCHSCREENING
YAMAA-01MDV.1002SCRN1IG.VS1IT.VS.VSDTC2026-02-11
YAMAA-01MDV.1002BL1IG.TV1IT.TV.VISITBaseline
YAMAA-01MDV.1002BL1IG.TV1IT.TV.VISITNUM1
YAMAA-01MDV.1002BL1IG.TV1IT.TV.VISITDY1
YAMAA-01MDV.1002BL1IG.TV1IT.TV.TAETORD2
YAMAA-01MDV.1002BL1IG.TV1IT.TV.EPOCHTREATMENT
YAMAA-01MDV.1002BL1IG.VS1IT.VS.VSDTC2026-02-18
YAMAA-01MDV.1002BL1IG.LB1IT.LB.LBDTC2026-02-18
YAMAA-01MDV.1002BL1IG.EX1IT.EX.EXDTC2026-02-18
YAMAA-01MDV.1002W21IG.TV1IT.TV.VISITWeek 2
YAMAA-01MDV.1002W21IG.TV1IT.TV.VISITNUM2
YAMAA-01MDV.1002W21IG.TV1IT.TV.VISITDY15
YAMAA-01MDV.1002W21IG.TV1IT.TV.TAETORD3
YAMAA-01MDV.1002W21IG.TV1IT.TV.EPOCHTREATMENT
YAMAA-01MDV.1002W21IG.VS1IT.VS.VSDTC2026-03-07
YAMAA-01MDV.1002W21IG.LB1IT.LB.LBDTC2026-03-07
YAMAA-01MDV.1002W21IG.EX1IT.EX.EXDTC2026-03-07

Expected output

Under review
8 expected rows

expected/sv.csv

Edit
8 rows
expected/sv.csv
DOMAINSTUDYIDUSUBJIDSVSEQVISITVISITNUMVISITDYSVSTDTCSVENDTCSVSTDYSVENDYTAETORDEPOCHSVUPDES
SVYAMAA-011Screening-1-72026-02-102026-02-10-7-71SCREENING—
SVYAMAA-012Baseline112026-02-172026-02-17112TREATMENT—
SVYAMAA-013Unscheduled99.1—2026-03-022026-03-021414—TREATMENTUnscheduled safety visit
SVYAMAA-014Unscheduled99.2—2026-03-042026-03-041616—TREATMENTUnscheduled follow-up visit
SVYAMAA-015Week 22152026-03-052026-03-0617183TREATMENT—
SVYAMAA-011Screening-1-72026-02-112026-02-11-7-71SCREENING—
SVYAMAA-012Baseline112026-02-182026-02-18112TREATMENT—
SVYAMAA-013Week 22152026-03-072026-03-0718183TREATMENT—
Shaded columns are derived— = missing value

Human review and assessment

Your review

3 questions and your comments · View benchmark discussions

Mapping spec

Generated from the benchmark YAML; do not edit by hand

Variable NameVariable LabelTypeLengthControlled Terms or FormatOriginCoreConversion DefinitionVariable TypeVariable OrderComments for Define
DOMAINDomain AbbreviationCharAssignedSet constant value "SV".SDTM1
STUDYIDStudy IdentifierCharCollectedCopy value from ODM.StudyOID.SDTM2
USUBJIDUnique Subject IdentifierCharCollectedCopy value from ODM.SubjectKey.SDTM3
VISITVisit NameCharDerivedSet per constructed row; see the Row construction sheet.SDTM4
VISITNUMVisit NumberNumDerivedSet per constructed row; see the Row construction sheet.SDTM5
VISITDYPlanned Study Day of VisitNumDerivedSet per constructed row; see the Row construction sheet.SDTM6
SVSTDTCStart Date/Time of VisitCharISO 8601DerivedLeast of VSDTC, LBDTC and EXDTC.SDTM7
SVENDTCEnd Date/Time of VisitCharISO 8601DerivedGreatest of VSDTC, LBDTC and EXDTC.SDTM8
SVSTDYStudy Day of Start of VisitNumDerivedCDISC study day of SVSTDTC against RFSTDTC as day 1; there is no day zero.SDTM9
SVENDYStudy Day of End of VisitNumDerivedCDISC study day of SVENDTC against RFSTDTC as day 1; there is no day zero.SDTM10
TAETORDPlanned Order of Element within ArmNumDerivedSet per constructed row; see the Row construction sheet.SDTM11
EPOCHEpochCharDerivedSet per constructed row; see the Row construction sheet.SDTM12
SVUPDESDescription of Unplanned VisitCharDerivedSet per constructed row; see the Row construction sheet.SDTM13
SVSEQSequence NumberNumDerivedRow number within each group of STUDYID, USUBJID, ordered by SVSTDTC.SDTM14
TemplateInclude whenVISITVISITNUMVISITDYVSDTCLBDTCEXDTCTAETORDEPOCHSVUPDES
visitAggregate 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'
VersionDateAuthorDescriptionReviewerSign-off
1.72026-10-20yamaa docs buildRead 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.62026-10-17yamaa docs buildDescribe collected date parts written as ISO 8601 date text and times of day counted in seconds since midnight
1.52026-10-17yamaa docs buildName 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.42026-10-15yamaa docs buildState datetime precision codes through the hour and minute, and complete only the time parts a collected datetime lacks
1.32026-09-29yamaa docs buildShow study codelist names, external dictionaries, and ISO 8601 formats in variable rows; list the document's codelist values and metadata
1.22026-09-29yamaa docs buildClassify ADaM copies from a declared input as Predecessor and recodes as Derived, and SDTM recodes of locally derived columns as Derived
1.12026-09-29yamaa docs buildResolve lookups into the donor record they select, and give every derivation shape a plain-language rule instead of its raw YAML
1.0yamaa docs buildInitial generation from spec.yaml

Agent prompt

The request an agent receives with the input files; each tier states no more than the one above

prompts/sdtm-sv-subject-visits/full.md

Edit
28 lines

Following 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

Edit
13 lines

Following 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

Edit
9 lines

Following 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.

yamaa code

Public calls generated from benchmark.yaml; results must match the expected output

R

Edit
3 lines
library(yamaa)yamaa_spec("spec.yaml")$run()$output

Python

Edit
3 lines
import yamaayamaa.spec("spec.yaml").run().output

Solution without yamaa

Written from the full prompt and the inputs alone; CI checks it reproduces the expected output

solutions/sdtm-sv-subject-visits/result.R

Edit
79 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

Edit
119 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

Edit
46 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;