I currently have a situation where I have:
ID Cost
1 10
1 5
2 10
3 10
3 10
What code can I use to convert the above into:
ID Cost
1 15
2 10
3 20
Thanks!
And just to show explicitly @Kurt_Bremser's answer:
data have;
input id cost;
datalines;
1 10
1 5
2 10
3 10
3 10
;
run;
proc sql;
create table want as
select id, sum(cost) as cost
from have
group by id;
quit;
In SQL, SELECT id and SUM(cost), GROUP BY id.
And just to show explicitly @Kurt_Bremser's answer:
data have;
input id cost;
datalines;
1 10
1 5
2 10
3 10
3 10
;
run;
proc sql;
create table want as
select id, sum(cost) as cost
from have
group by id;
quit;
Fab, thanks 🙂
What @mklangley said in (I guess) his last post is also known as Maxim 4.
(the "guess" is because of the pronomen)
And that's where PROC MEANS will shine over SQL. In SQL you'll have to list every variable and statistic separately. If you want a different stat, say the mean or the standard deviation for 10 variables that will get tedious quickly.
proc means data=have noprint NWAY SUM STACKODS;
by ID;
var cost;
run;
Now, you add 4 cost variables and want both the sum and average? Add MEAN to the PROC statement and change the VAR statement slightly to include all 4 variables.
proc means data=have noprint NWAY SUM MEAN STACKODS;
by ID;
var cost1-cost4;
run;
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.