Hello everyone,
I have a problem which I have already solved more or less but was wondering if there might be a more elegant/efficient/faster solution to it. Hope somebody can help me.
DATA have;
format month date9.;
input month date9. id amount;
datalines;
31DEC2020 1 10
31JAN2021 1 0
28FEB2021 1 5
31MAR2021 1 0
31DEC2020 2 0
31JAN2021 2 0
28FEB2021 2 5
31MAR2021 2 0
31DEC2020 3 20
31JAN2021 3 10
28FEB2021 3 0
31MAR2021 3 0
;
This is the table I have and I want to have a table which gives me for each month the overall sum of the amounts.
I tried it with this:
DATA want;
SET have;
BY id;
RETAIN sum_all;
sum_all = sum(amount, sum_all);
RUN;
But even though I group by the id the cum calculation does not restart at every new id.
I solved it by sorting the have table and the doing the calculation by introducing two IF conditions:
proc sort
data=have
out=have_sorted
;
by
id
month
;
run;
DATA want2;
SET have_sorted;
BY id;
RETAIN sum_all;
IF first.id AND amount GT 0 THEN sum_all = 0;
IF first.id AND amount EQ 0 THEN sum_all = amount;
sum_all = sum(amount, sum_all);
RUN;
So the table want2 is how I want the end result to look like, for each id and month sum of amount so far.
Thank you for your help.
This is the simplest method:
data want;
set have;
by id;
if first.id
then sum_all = amount;
else sum_all + amount;
run;
The SUM statement (sum_all + amount) implies two things:
- missing values are considered as zero
- the variable to the left is automatically retained
You say you want the following but your results are not by month, but by ID. Which is the ultimate requirement? Assuming by month and ID:
@aguilar_john wrote:
This is the table I have and I want to have a table which gives me for each month the overall sum of the amounts.
Logically your code is almost as succinct as it can be (with the exception of using PROC EXPAND instead).
This is giving you a running total, did you want a running total or just a total per month?
If per month, PROC MEANS will be more efficient and can be used unsorted.
proc means data=have N SUM NWAY STACKODS;
class month ID;
format month yymmn6.;
var amount;
output out=want1 sum=total_amount n=Number_Obs;
ods output summary = want2;
run;
Your current code can be simplified as follows:
Retain is implicit with the SUM statement. Can your data have missing values? If so, you may want the sum function instead, so keeping the RETAIN + SUM() function is a better overall solution.
DATA want2;
SET have_sorted;
BY id;
IF first.id THEN sum_all = 0;
sum_all + amount;
RUN;
@aguilar_john wrote:
Hello everyone,
I have a problem which I have already solved more or less but was wondering if there might be a more elegant/efficient/faster solution to it. Hope somebody can help me.
DATA have; format month date9.; input month date9. id amount; datalines; 31DEC2020 1 10 31JAN2021 1 0 28FEB2021 1 5 31MAR2021 1 0 31DEC2020 2 0 31JAN2021 2 0 28FEB2021 2 5 31MAR2021 2 0 31DEC2020 3 20 31JAN2021 3 10 28FEB2021 3 0 31MAR2021 3 0 ;
This is the table I have and I want to have a table which gives me for each month the overall sum of the amounts.
I tried it with this:
DATA want; SET have; BY id; RETAIN sum_all; sum_all = sum(amount, sum_all); RUN;
But even though I group by the id the cum calculation does not restart at every new id.
I solved it by sorting the have table and the doing the calculation by introducing two IF conditions:
proc sort data=have out=have_sorted ; by id month ; run; RUN; DATA want2; SET have_sorted; BY id; RETAIN sum_all; IF first.id AND amount GT 0 THEN sum_all = 0; IF first.id AND amount EQ 0 THEN sum_all = amount; sum_all = sum(amount, sum_all); RUN;
So the table want2 is how I want the end result to look like, for each id and month sum of amount so far.
Thank you for your help.
This is the simplest method:
data want;
set have;
by id;
if first.id
then sum_all = amount;
else sum_all + amount;
run;
The SUM statement (sum_all + amount) implies two things:
- missing values are considered as zero
- the variable to the left is automatically retained
Thank you @Kurt_Bremser and @Reeza for your help!
The solution @Kurt_Bremser presented suits me the most.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: