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.

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

Sources checked 20 September 2026. Implementation commentary reflects a practical review perspective; source material may change.

Have a related programming or implementation challenge?

Discuss it with Sai ↗