BookmarkSubscribeRSS Feed
Solph
Pyrite | Level 9
I wanted to calculate percent distribution of students by school for each geographic area. I first used proc means to sum up the student counts by area and school, then got the cumulative total. But how do I assign the total student numbers by area so as to calculate the percentage (out of total cases, not out of cumulative total)? I need to merge this percent distribution (by area, school) into other dataset for further data manipulation. Thanks in advance.


data aa;
input id area$ school$ gender $ studentNum;
datalines;
1 area1 school1 M 30
1 area1 school1 F 70
2 area1 school2 M 60
2 area1 school2 F 40
3 area1 school3 M 80
3 area1 school3 F 20
4 area2 school4 M 40
4 area2 school4 F 60
5 area2 school5 M 100
6 area2 school6 F 200
;

proc means data=aa noprint nway;
class area school;
var studentNum;
output out=bb (drop= _freq_ _type_) sum=;
run;
data bb; set bb;
by area school;
if first.area then CUMtotal=0;
CUMtotal+studentNum;
format percent 8.1;
percent=studentNum*100/CUMtotal;
run;
proc print data=aa noobs; run;
proc print data=bb noobs; run;


*** ACTUAL OUTPUT ****************************************;

area school StudentNum CUMtotal Percent
--------------------------------------------------------------------
area1 school1 100 100 100.0
area1 school2 100 200 50.0
area1 school3 100 300 33.3

area2 school4 100 100 100.0
area2 school5 100 200 50.0
area2 school6 200 400 50.0


***DESIRABLE OUTPUT ****************************;
area school StudentNum TOTAL Percent
--------------------------------------------------------------------
area1 school1 100 300 33.3
area1 school2 100 300 33.3
area1 school3 100 300 33.3

area2 school4 100 400 25.0
area2 school5 100 400 25.0
area2 school6 200 400 50.0 Message was edited by: Solph
2 REPLIES 2
art297
Opal | Level 21
As usual with SAS, you have multiple options one of which would be to do everything using proc sql. E.g.:
[pre]
proc sql noprint;
create table want as
select distinct
area,
school,
studentnum,
sum(studentnum) as total,
100*studentnum/calculated total as percent
from (select distinct
area,
school,
sum(studentNum) as studentnum
from aa
group by area,school)
group by area
;
quit;
[/pre]
HTH,
Art
Solph
Pyrite | Level 9
Thanks so much Art. It is one very clever way to do it using SQL.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 2 replies
  • 6225 views
  • 0 likes
  • 2 in conversation