Hello SAS Community,
I'm doing my first steps in SAS at the moment and i've gote a problem which i can't solve...
I have a table like the following example:
Column1 - Column2
A - 500
B - 580
A - 911
B - 419
B - 165
So all i want to do is to create a new SAS Table with aggregated rows for all "A"s and "B"s.
Thanks for your help!
Welcome to the SAS Community 🙂
Is this what you are after?
data have;
input Column1 $ Column2;
datalines;
A 500
B 580
A 911
B 419
B 165
;
proc sql;
create table want as
select Column1, sum(Column2) as sum
from have
group by Column1;
quit;
Welcome to the SAS Community 🙂
Is this what you are after?
data have;
input Column1 $ Column2;
datalines;
A 500
B 580
A 911
B 419
B 165
;
proc sql;
create table want as
select Column1, sum(Column2) as sum
from have
group by Column1;
quit;
Alternatively via data step
data have;
input Column1$ Column2;
cards;
A 500
B 580
A 911
B 419
B 165
;
proc sort data=have;
by column1;
run;
data want;
set have;
by column1;
retain sum;
if first.column1 then sum=column2;
else sum+column2;
if last.column1;
run;
FWIW
data have;
input Column1 $ Column2;
datalines;
A 500
B 580
A 911
B 419
B 165
;
data _null_;
if _n_=1 then do;
dcl hash H () ;
h.definekey ("Column1") ;
h.definedata ("Column1","sum") ;
h.definedone () ;
end;
do until(z);
set have end=z;
if h.find()= 0 then sum=sum(sum,column2);
else sum=column2;
h.replace();
end;
h.output(dataset:'want');
stop;
run;
proc summary data=have nway ;
class column1;
var column2;
output out=want(drop=_:) sum=sum;
run;
Save $250 on SAS Innovate and get a free advance copy of the new SAS For Dummies book! Use the code "SASforDummies" to register. Don't miss out, May 6-9, in Orlando, Florida.
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.