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;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: