Hello,
I am trying to add up the AMOUNT AND INCREASE with multiple rows have the same ID.
data calc;
input ID AMOUNT INCREASE
cards;
8888 50 3.55
8889 1.10 0
8889 2.99 2.22
8890 3.48 15
8891 34.08 1
8891 144.87 4
8891 2.24 0.8
8892 0 0
;
I like to add the AMOUNT AND INCREASE FOR EACH ID.
The result:
8888 50 3.55
8889 4.09 2.22
8890 3.48 15
8891 181.19 5.8
8892 0 0
Thank you.
Summarizing data is straightforward with grouping variables.
Here's an example of two ways to do this, with different output styles. 2 tables are created, want and want2 with different structures, I think you're looking for WANT1 perhaps?
*Create summary statistics for a dataset by a 'grouping' variable and store it in a dataset;
*Generate sample fake data;
data have;
input ID feature1 feature2 feature3;
cards;
1 7.72 5.43 4.35
1 5.54 2.25 8.22
1 4.43 6.75 2.22
1 3.22 3.21 7.31
2 6.72 2.86 6.11
2 5.89 4.25 5.25
2 3.43 7.30 8.21
2 1.22 3.55 6.55
;
run;
*Create summary data;
proc means data=have noprint;
by id;
var feature1-feature3;
output out=want median= var= mean= /autoname;
run;
*Show for display;
proc print data=want;
run;
*First done here:https://communities.sas.com/t5/General-SAS-Programming/Getting-creating-new-summary-variables-longitudinal-data/m-p/347940/highlight/false#M44842;
*Another way to present data is as follows;
proc means data=have stackods nway n min max mean median std p5 p95;
by id;
var feature1-feature3;
ods output summary=want2;
run;
*Show for display;
proc print data=want2;
Summarizing data is straightforward with grouping variables.
Here's an example of two ways to do this, with different output styles. 2 tables are created, want and want2 with different structures, I think you're looking for WANT1 perhaps?
*Create summary statistics for a dataset by a 'grouping' variable and store it in a dataset;
*Generate sample fake data;
data have;
input ID feature1 feature2 feature3;
cards;
1 7.72 5.43 4.35
1 5.54 2.25 8.22
1 4.43 6.75 2.22
1 3.22 3.21 7.31
2 6.72 2.86 6.11
2 5.89 4.25 5.25
2 3.43 7.30 8.21
2 1.22 3.55 6.55
;
run;
*Create summary data;
proc means data=have noprint;
by id;
var feature1-feature3;
output out=want median= var= mean= /autoname;
run;
*Show for display;
proc print data=want;
run;
*First done here:https://communities.sas.com/t5/General-SAS-Programming/Getting-creating-new-summary-variables-longitudinal-data/m-p/347940/highlight/false#M44842;
*Another way to present data is as follows;
proc means data=have stackods nway n min max mean median std p5 p95;
by id;
var feature1-feature3;
ods output summary=want2;
run;
*Show for display;
proc print data=want2;
Thank you so much.
data calc;
input ID AMOUNT INCREASE;
cards;
8888 50 3.55
8889 1.10 0
8889 2.99 2.22
8890 3.48 15
8891 34.08 1
8891 144.87 4
8891 2.24 0.8
8892 0 0
;
proc sql;
create table want as
select id,sum(AMOUNT) as sum_amt, sum(increase) as sum_increase
from calc
group by id;
quit;
thank you so much.
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 25. Read more here about why you should contribute and what is in it for you!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.