🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Obsidian | Level 7

## GROUP BY

I have the following code, which is returning the accumulated sum of all observations. But I want the accumulated sum by date.

In "proc sql" would do:

group by date

But in this code that I have, what do I do?

data resul1_perf;

set resul_perf;

if _n_ = 1 then bad_acc = bad;

if _n_ = 1 then good_acc = good;

else good_acc = good_acc + good;

dif_acc = abs(bad_acc - good_acc);

FORMAT good_acc COMMAX20.3;

FORMAT dif_acc COMMAX20.3;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
Diamond | Level 26

## Re: GROUP BY

``````data resul1_perf;
set resul_perf;
by date;
if first.date then do;
good_acc=0;
end;
good_acc + good;
dif_acc = abs(bad_acc - good_acc);
FORMAT bad_acc good_acc dif_acc COMMAX20.3;
run;``````
--
Paige Miller
2 REPLIES 2
Diamond | Level 26

## Re: GROUP BY

``````data resul1_perf;
set resul_perf;
by date;
if first.date then do;
good_acc=0;
end;
good_acc + good;
dif_acc = abs(bad_acc - good_acc);
FORMAT bad_acc good_acc dif_acc COMMAX20.3;
run;``````
--
Paige Miller
Super User

## Re: GROUP BY

Use BY in the data step and the first. and last. automatic variables:

``````proc sort data=resul_perf;
by date;
run;

data resul1_perf;
set resul_perf;
by date;
if first.date
then do;
good_acc = good;
end;
else do;
good_acc = good_acc + good;
end;
if last.date;
dif_acc = abs(bad_acc - good_acc);
format