BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jl1005
Obsidian | Level 7

Hi,

 

In my data, I have multiple rows for one person and I'd like to combine all the rows into one unique row while summing the values. For example:

 

combine.JPG

Where year_id and bat_id are equal, I would like to combine the rows and sum the column values. So for 2015, I would just want one row with values:

 82 for G, 232 for AB, and so one. What procedure is best able to do this, or could it be done in the data step?

 

Thank you,

 

Justin

1 ACCEPTED SOLUTION

Accepted Solutions
Jagadishkatam
Amethyst | Level 16

you could try the proc means

 

data have;
input year_id 	bat_id$	g	ab	r	h	_2b;
cards;
2015	almoa001	51	178	30	47	9
2015	almoa001	31	54	6	11	3
2014	almoa001	32	98	9	26	5
2014	almoa001	27	106	10	21	5
;


proc means data=have nway;
class year_id bat_id;
var g ab r h _2b;
output out=sums sum=/autoname;
run;

 

 

Thanks,
Jag

View solution in original post

3 REPLIES 3
Jagadishkatam
Amethyst | Level 16

you could try the proc means

 

data have;
input year_id 	bat_id$	g	ab	r	h	_2b;
cards;
2015	almoa001	51	178	30	47	9
2015	almoa001	31	54	6	11	3
2014	almoa001	32	98	9	26	5
2014	almoa001	27	106	10	21	5
;


proc means data=have nway;
class year_id bat_id;
var g ab r h _2b;
output out=sums sum=/autoname;
run;

 

 

Thanks,
Jag
Jagadishkatam
Amethyst | Level 16

alternatively proc sql

 

proc sql;
create table want as select year_id, bat_id,sum(g) as g, sum(ab) as ab, sum(r) as r, sum(h) as h, sum(_2b) as _2b
from have group by year_id,bat_id;
quit;
Thanks,
Jag
ballardw
Super User

If you use @Jagadishkatam's Proc Means solution with Sum= instead of Sum= / autoname then the variable names would stay unchanged if that would be the preferred behavior.

sas-innovate-2024.png

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.

 

Register now!

What is Bayesian Analysis?

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 3 replies
  • 760 views
  • 1 like
  • 3 in conversation