<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculate percent distribution from grouped data in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-percent-distribution-from-grouped-data/m-p/70884#M20422</link>
    <description>Thanks so much Art. It is one very clever way to do it using SQL.</description>
    <pubDate>Sun, 29 May 2011 21:54:41 GMT</pubDate>
    <dc:creator>Solph</dc:creator>
    <dc:date>2011-05-29T21:54:41Z</dc:date>
    <item>
      <title>Calculate percent distribution from grouped data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-percent-distribution-from-grouped-data/m-p/70882#M20420</link>
      <description>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.&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
data aa; &lt;BR /&gt;
input id  area$ school$ gender $ studentNum;&lt;BR /&gt;
datalines; &lt;BR /&gt;
1  area1 school1 M   30&lt;BR /&gt;
1  area1 school1 F    70&lt;BR /&gt;
2  area1 school2  M  60&lt;BR /&gt;
2  area1 school2  F   40&lt;BR /&gt;
3  area1 school3 M   80&lt;BR /&gt;
3  area1 school3 F    20&lt;BR /&gt;
4  area2 school4 M   40&lt;BR /&gt;
4  area2 school4 F    60&lt;BR /&gt;
5  area2 school5 M 100&lt;BR /&gt;
6  area2 school6 F  200&lt;BR /&gt;
;&lt;BR /&gt;
&lt;BR /&gt;
proc means data=aa noprint nway;	&lt;BR /&gt;
	class area school;&lt;BR /&gt;
	var studentNum; &lt;BR /&gt;
	output out=bb (drop= _freq_ _type_) sum=;&lt;BR /&gt;
	run;&lt;BR /&gt;
data  bb; set bb;&lt;BR /&gt;
	by area school;&lt;BR /&gt;
	if first.area then CUMtotal=0;&lt;BR /&gt;
	CUMtotal+studentNum;&lt;BR /&gt;
	format percent 8.1;&lt;BR /&gt;
	percent=studentNum*100/CUMtotal;&lt;BR /&gt;
	run;&lt;BR /&gt;
proc print data=aa noobs; run;&lt;BR /&gt;
proc print data=bb noobs; run;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
*** ACTUAL OUTPUT ****************************************;&lt;BR /&gt;
&lt;BR /&gt;
area     school    StudentNum  CUMtotal    Percent&lt;BR /&gt;
--------------------------------------------------------------------&lt;BR /&gt;
area1    school1       100       100          100.0&lt;BR /&gt;
area1    school2       100       200            50.0&lt;BR /&gt;
area1    school3       100       300            33.3&lt;BR /&gt;
&lt;BR /&gt;
area2    school4       100       100          100.0&lt;BR /&gt;
area2    school5       100       200            50.0&lt;BR /&gt;
area2    school6       200       400            50.0&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
***DESIRABLE OUTPUT ****************************;&lt;BR /&gt;
area     school    StudentNum  TOTAL    Percent&lt;BR /&gt;
--------------------------------------------------------------------&lt;BR /&gt;
area1    school1       100       300           33.3&lt;BR /&gt;
area1    school2       100       300           33.3&lt;BR /&gt;
area1    school3       100       300           33.3&lt;BR /&gt;
&lt;BR /&gt;
area2    school4       100       400           25.0&lt;BR /&gt;
area2    school5       100       400           25.0&lt;BR /&gt;
area2    school6       200       400           50.0

Message was edited by: Solph</description>
      <pubDate>Sun, 29 May 2011 08:36:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-percent-distribution-from-grouped-data/m-p/70882#M20420</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2011-05-29T08:36:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percent distribution from grouped data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-percent-distribution-from-grouped-data/m-p/70883#M20421</link>
      <description>As usual with SAS, you have multiple options one of which would be to do everything using proc sql.  E.g.:&lt;BR /&gt;
[pre]&lt;BR /&gt;
proc sql noprint;&lt;BR /&gt;
  create table want as&lt;BR /&gt;
    select distinct&lt;BR /&gt;
           area,&lt;BR /&gt;
           school,&lt;BR /&gt;
           studentnum,&lt;BR /&gt;
           sum(studentnum) as total,&lt;BR /&gt;
           100*studentnum/calculated total as percent&lt;BR /&gt;
      from (select distinct &lt;BR /&gt;
               area,&lt;BR /&gt;
               school, &lt;BR /&gt;
               sum(studentNum) as studentnum&lt;BR /&gt;
                  from aa&lt;BR /&gt;
                    group by area,school)&lt;BR /&gt;
        group by area&lt;BR /&gt;
;&lt;BR /&gt;
quit;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
HTH,&lt;BR /&gt;
Art</description>
      <pubDate>Sun, 29 May 2011 14:42:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-percent-distribution-from-grouped-data/m-p/70883#M20421</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2011-05-29T14:42:45Z</dc:date>
    </item>
    <item>
      <title>Re: Calculate percent distribution from grouped data</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/Calculate-percent-distribution-from-grouped-data/m-p/70884#M20422</link>
      <description>Thanks so much Art. It is one very clever way to do it using SQL.</description>
      <pubDate>Sun, 29 May 2011 21:54:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/Calculate-percent-distribution-from-grouped-data/m-p/70884#M20422</guid>
      <dc:creator>Solph</dc:creator>
      <dc:date>2011-05-29T21:54:41Z</dc:date>
    </item>
  </channel>
</rss>

