Desktop productivity for business analysts and programmers

Cumulative frequency

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 14
Accepted Solution

Cumulative frequency

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,


Accepted Solutions
Solution
‎09-18-2017 06:29 PM
Super User
Posts: 22,857

Re: Cumulative frequency

Posted in reply to GuillermoPH

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


All Replies
Super User
Posts: 22,857

Re: Cumulative frequency

Posted in reply to GuillermoPH

Are you writing code or using the GUI?

Occasional Contributor
Posts: 14

Re: Cumulative frequency

I'm using the GUI but I could write it down in code.
Solution
‎09-18-2017 06:29 PM
Super User
Posts: 22,857

Re: Cumulative frequency

Posted in reply to GuillermoPH

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;
Occasional Contributor
Posts: 14

Re: Cumulative frequency

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 Smiley Happy 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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