## Is it possible to calculate the percentage of some category for each group, using proc sql?

Solved
Occasional Contributor
Posts: 9

# Is it possible to calculate the percentage of some category for each group, using proc sql?

I have a dataset as follows. And I want to get the percentage of students with A, B, or C for each class, is it possible to do by Proc SQL? Thank you so much.

--------------------

ID   Credit  Class

--------------------

1     A         1

2     B         1

3     B         1

4     C         1

5     A         1

6     A         2

7     C         2

8     C         2

9     C         2

10   B         2

-------------------

And the result I want to get is

-----------------------

Class  A    B    C

-----------------------

1        0.4  0.4  0.2

2        0.2  0.2  0.6

----------------------

Accepted Solutions
Solution
‎03-07-2017 12:59 PM
PROC Star
Posts: 311

## Re: Is it possible to calculate the percentage of some category for each group, using proc sql?

You can calculate the average of a boolean reponse:

``````data have;
input credit \$ class;
datalines;
A         1
B         1
B         1
C         1
A         1
A         2
C         2
C         2
C         2
B         2
;

proc sql;
SELECT class,
mean(credit = 'A') 'A',
mean(credit = 'B') 'B',
mean(credit = 'C') 'C'
FROM have
GROUP BY class;
quit;

``````

All Replies
Solution
‎03-07-2017 12:59 PM
PROC Star
Posts: 311

## Re: Is it possible to calculate the percentage of some category for each group, using proc sql?

You can calculate the average of a boolean reponse:

``````data have;
input credit \$ class;
datalines;
A         1
B         1
B         1
C         1
A         1
A         2
C         2
C         2
C         2
B         2
;

proc sql;
SELECT class,
mean(credit = 'A') 'A',
mean(credit = 'B') 'B',
mean(credit = 'C') 'C'
FROM have
GROUP BY class;
quit;

``````
Occasional Contributor
Posts: 9