aniusni
aniusni

Reputation: 59

Summing values by character in SAS

I created this fakedata as an example:

data fakedata;
length name $5;
infile datalines;
input name count percent;
return;
datalines;
Ania 1 17
Basia 1 3
Ola 1 10
Basia 1 52
Basia 1 2
Basia 1 16
;
run;

The result I want to have is:

table

---> summed counts and percents for Basia

I would like to have summed count and percent for Basia as she was only once in the table with count 4 and percent 83. I tried exchanging name into a number to do GROUP BY in proc sql but it changes into order by (I had such an error). Suppose that it isn't so difficult, but I can't find the solution. I also tried some arrays without any success. Any help appreciated!

Upvotes: 0

Views: 289

Answers (3)

Reeza
Reeza

Reputation: 21294

You can go back a step and use PROC FREQ most likely to generate this output in a single step. Based on counts the percents are not correct, but I'm not sure they're intended to be, right now they add up to over 100%. If you already have some summaries, then use the WEIGHT statement to account for the counts.

proc freq data=fakedata;
table name;
weight count;
run;

Upvotes: 0

aniusni
aniusni

Reputation: 59

Hm, actually I got an answer.

proc summary data=fakedata;
by name;
var count percent;
output out=wynik (drop = _FREQ_ _TYPE_) sum(count)=count sum(percent)=percent;
run;

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1270863

It sounds like proc sql does what you want:

proc sql;
    select name, count(*) as cnt, sum(percent) as sum_percent
    from fakedata
    group by name;

You can add a where clause to get the results just for one name.

Upvotes: 1

Related Questions