Check the order, then the rule
Sort or otherwise provide the ordering required by the BY statement. Specify tie-breakers when selecting the first record has analytical meaning. If two rows share the same date, the retained row should follow a documented rule rather than an accidental source order.
Technical background: [1] [2]. Practical recommendations below are VSPJ Consulting commentary.
Reset retained state
When accumulating within a subject, initialise the accumulator at FIRST.usubjid. Check subjects with one row, missing values and tied times. Compare the output with a small hand-calculated example before running the full dataset. The sum statement treats missing inputs differently from ordinary addition, so choose deliberately.
Illustrative example
proc sort data=have out=ordered;
by usubjid visitnum;
run;
data totals;
set ordered;
by usubjid;
if first.usubjid then total=0;
total + value;
if last.usubjid then output;
run;
/* Illustrative: missing VALUE contributes zero. */
Review and test this example against your own inputs and specification before use.
Sources & further reading
References reviewed 21 September 2026. Sources support the technical background; workflow recommendations are independent implementation commentary. Release-watch details are dated snapshots. Check current documentation before adoption.
Have a related programming or implementation challenge?
Discuss it with Sai Prasad ↗