Your explanations are very obscure indeed.
[pre]
data SAMPLE;
infile cards missover;
do until(CODE=.);
input CODE DEPTH @;
output;
end;
input;
cards;
20 1.00 45 9.00 21.1 -4.75 08 1.00 06 0.50 08 1.50
20 0.75 21.1 -2.2 08 1.50 20 0.50 21.2 -2.00 08 1.25
20 0.75 21.1 -2.25 08 1.50 21.2 -2.00 08 1.25 25 9.00
21.1 -2.75 8 1 40 1 6 .5 8 .75 40 6 8 2.75
run;
data SUMS;
set SAMPLE;
if SUM <0 then SUM+DEPTH; *add if negative;
else if CODE ne 40 then SUM+DEPTH; *else if positive add if not base;
if CODE=. then SUM=.; *reset between groups;
run;
.
yields for the last data line:
CODE DEPTH SUM
21.1 -2.75 -2.75
8.0 1.00 -1.75
40.0 1.00 -0.75
6.0 0.50 -0.25
8.0 0.75 0.50
40.0 6.00 0.50
8.0 2.75 3.25
unsure how you find 3.50