SAS Enterprise Guide

Desktop productivity for business analysts and programmers
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
GuillermoPH
Obsidian | Level 7

Hi!

 

I'm trying to aggregate the following by EstadoCDR, buy i haven't been able to produce the desired result. Here's an example of how my DB looks like:

 

DB_Original.jpg

 

In the end, i'd like to have this new column:

 

DB_Resultante.jpg

 

Any help will be much appreciated!

 

Regards,

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Here's an example via code. I used one of the datasets to illustrate the concepts, but it's basically using BY processing and the RETAIN statement. 

 

*generate sample data;
data baseball;
set sashelp.baseball;
keep  team name salary;
run;

*sort on Grouping variable, for BY statement;
proc sort data=baseball;
by team name;
run;

*add totals;
data want;
set baseball;
by team name; *same as from PROC SORT;
retain total_salary; *keeps values across rows;

if first.team then total_salary = sum(0, salary); *reset for first record;
else total_salary = sum(total_salary, salary); *accumulate;
run;

View solution in original post

4 REPLIES 4
Reeza
Super User

Are you writing code or using the GUI?

GuillermoPH
Obsidian | Level 7
I'm using the GUI but I could write it down in code.
Reeza
Super User

Here's an example via code. I used one of the datasets to illustrate the concepts, but it's basically using BY processing and the RETAIN statement. 

 

*generate sample data;
data baseball;
set sashelp.baseball;
keep  team name salary;
run;

*sort on Grouping variable, for BY statement;
proc sort data=baseball;
by team name;
run;

*add totals;
data want;
set baseball;
by team name; *same as from PROC SORT;
retain total_salary; *keeps values across rows;

if first.team then total_salary = sum(0, salary); *reset for first record;
else total_salary = sum(total_salary, salary); *accumulate;
run;
GuillermoPH
Obsidian | Level 7

Yes, exactly what I was looking for! Just had to do a minor change in the coding (I already had the info sorted the way I wanted):

 

 

data acumula;
set ms_cdr;
by EstadoCDR;
retain Acumulado_MS;
if first.EstadoCDR then Acumulado_MS = sum(0, ms_cdr);
else Acumulado_MS = sum(Acumulado_MS, ms_cdr);
run;

Thanks a lot! You saved me 🙂 

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

Register now!

Creating Custom Steps in SAS Studio

Check out this tutorial series to learn how to build your own steps in SAS Studio.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 4 replies
  • 6908 views
  • 1 like
  • 2 in conversation