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:
In the end, i'd like to have this new column:
Any help will be much appreciated!
Regards,
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;
Are you writing code or using the GUI?
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;
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 🙂
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.