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

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;

retain bad_acc good_acc;

if _n_ = 1 then bad_acc = bad;

else bad_acc = bad_acc + bad;

if _n_ = 1 then good_acc = good;

else good_acc = good_acc + good;

dif_acc = abs(bad_acc - good_acc);

FORMAT bad_acc COMMAX20.3;

FORMAT good_acc COMMAX20.3;

FORMAT dif_acc COMMAX20.3;

run;

 

 

1 ACCEPTED SOLUTION

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

View solution in original post

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

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;
retain bad_acc good_acc;
if first.date
then do;
  bad_acc = bad;
  good_acc = good;
end;
else do;
  bad_acc = bad_acc + bad;
  good_acc = good_acc + good;
end;
if last.date;
dif_acc = abs(bad_acc - good_acc);
format
  bad_acc
  good_acc
  dif_acc commax20.3
;
run;

SAS INNOVATE 2024

Innovate_SAS_Blue.png

Registration is open! SAS is returning to Vegas for an AI and analytics experience like no other! Whether you're an executive, manager, end user or SAS partner, SAS Innovate is designed for everyone on your team. Register for just $495 by 12/31/2023.

If you are interested in speaking, there is still time to submit a session idea. More details are posted on the website. 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Get the $99 certification deal.jpg

 

 

Back in the Classroom!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 280 views
  • 0 likes
  • 3 in conversation