<?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: How to calculate groupwise cumulative percentages in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713346#M220063</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361528"&gt;@Shradha1&lt;/a&gt;&amp;nbsp;Sorry, there you go&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data popn;
Input country$ state$ city$ population;
Cards;
US California LA 100
US California SD 50
US Texas Houston 10
US Texas Austin 5
INDIA MH Mumbai 10
INDIA MH Pune 5
INDIA MH Nasik 7
INDIA WB Kol 6
INDIA UP Bs 4
;

data want(drop = p);
   p = 0;
   do until (last.state);
      set popn;
      by country state notsorted;
      p + population;
   end;

   do until (last.state);
      set popn;
      by country state notsorted;
      percent = sum(percent, divide(population, p));
      output;
   end;

   format percent percent8.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;country  state     city population  percent 
US       Californ  LA       100     67% 
US       Californ  SD       50      100% 
US       Texas     Houston  10      67% 
US       Texas     Austin   5       100% 
INDIA    MH        Mumbai   10      45% 
INDIA    MH        Pune     5       68% 
INDIA    MH        Nasik    7       100% 
INDIA    WB        Kol      6       100% 
INDIA    UP        Bs       4       100% &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 22 Jan 2021 13:55:54 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2021-01-22T13:55:54Z</dc:date>
    <item>
      <title>How to calculate groupwise cumulative percentages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713339#M220057</link>
      <description>I have a dataset containing country, state, city and total population within each:&lt;BR /&gt;Data popn;&lt;BR /&gt;Input country$ state$ city$ population;&lt;BR /&gt;Cards;&lt;BR /&gt;US California LA 100&lt;BR /&gt;US California SD 50&lt;BR /&gt;US Texas Houston 10&lt;BR /&gt;US Texas Austin 5&lt;BR /&gt;INDIA MH Mumbai 10&lt;BR /&gt;INDIA MH Pune 5&lt;BR /&gt;INDIA MH Nasik 7&lt;BR /&gt;INDIA WB Kol 6&lt;BR /&gt;INDIA UP Bs 4&lt;BR /&gt;&lt;BR /&gt;What I require is state wise cumulative percentage of population, that is by the second column as groups:&lt;BR /&gt;US California LA 100 67%&lt;BR /&gt;US California SD 50. 100%&lt;BR /&gt;US Texas Houston 10 67%&lt;BR /&gt;US Texas Austin 5. 100%&lt;BR /&gt;INDIA MH Mumbai 10. 45%&lt;BR /&gt;INDIA MH Pune 5. 68%&lt;BR /&gt;INDIA MH Nasik 7. 100%&lt;BR /&gt;INDIA WB Kol 6. 100%&lt;BR /&gt;INDIA UP Bs 4. 100%&lt;BR /&gt;&lt;BR /&gt;How do I get this ?&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Fri, 22 Jan 2021 13:24:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713339#M220057</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-01-22T13:24:16Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate groupwise cumulative percentages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713340#M220058</link>
      <description>&lt;P&gt;Do you want a report or a SAS data set?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Data set:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop = p);
   p = 0;
   do until (last.state);
      set popn;
      by country state notsorted;
      p + population;
   end;

   do until (last.state);
      set popn;
      by country state notsorted;
      percent = divide(population, p);
      output;
   end;

   format percent percent8.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;country  state     city     population  percent 
US       Californ  LA       100         67% 
US       Californ  SD       50          33% 
US       Texas     Houston  10          67% 
US       Texas     Austin   5           33% 
INDIA    MH        Mumbai   10          45% 
INDIA    MH        Pune     5           23% 
INDIA    MH        Nasik    7           32% 
INDIA    WB        Kol      6           100% 
INDIA    UP        Bs       4           100% 
&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Jan 2021 13:39:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713340#M220058</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-22T13:39:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate groupwise cumulative percentages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713341#M220059</link>
      <description>&lt;P&gt;here's my code ... maybe someone has a shorter version&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data popn;
Input country $ state $ city $ population;
order=_n_;
Cards;
US California LA 100
US California SD 50
US Texas Houston 10
US Texas Austin 5
INDIA MH Mumbai 10
INDIA MH Pune 5
INDIA MH Nasik 7
INDIA WB Kol 6
INDIA UP Bs 4
;
proc summary data=popn nway;
    class country state;
    var population;
    output out=stats sum=pop_sum;
run;
proc sort data=popn;
    by country state;
run;
data want;
    merge popn stats;
    by country state;
run;
proc sort data=want;
    by order;
run;
data want2;
    set want;
    by country notsorted state notsorted;
    if first.state then cum_population=0;
    cum_population+population; /* Cumulative population */
    percent=cum_population / pop_sum;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 13:34:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713341#M220059</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-01-22T13:34:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate groupwise cumulative percentages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713345#M220062</link>
      <description>I want sas dataset but cumulative percentages within each state and not percentages</description>
      <pubDate>Fri, 22 Jan 2021 13:48:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713345#M220062</guid>
      <dc:creator>Shradha1</dc:creator>
      <dc:date>2021-01-22T13:48:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to calculate groupwise cumulative percentages</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713346#M220063</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/361528"&gt;@Shradha1&lt;/a&gt;&amp;nbsp;Sorry, there you go&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data popn;
Input country$ state$ city$ population;
Cards;
US California LA 100
US California SD 50
US Texas Houston 10
US Texas Austin 5
INDIA MH Mumbai 10
INDIA MH Pune 5
INDIA MH Nasik 7
INDIA WB Kol 6
INDIA UP Bs 4
;

data want(drop = p);
   p = 0;
   do until (last.state);
      set popn;
      by country state notsorted;
      p + population;
   end;

   do until (last.state);
      set popn;
      by country state notsorted;
      percent = sum(percent, divide(population, p));
      output;
   end;

   format percent percent8.;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;country  state     city population  percent 
US       Californ  LA       100     67% 
US       Californ  SD       50      100% 
US       Texas     Houston  10      67% 
US       Texas     Austin   5       100% 
INDIA    MH        Mumbai   10      45% 
INDIA    MH        Pune     5       68% 
INDIA    MH        Nasik    7       100% 
INDIA    WB        Kol      6       100% 
INDIA    UP        Bs       4       100% &lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 Jan 2021 13:55:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-calculate-groupwise-cumulative-percentages/m-p/713346#M220063</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2021-01-22T13:55:54Z</dc:date>
    </item>
  </channel>
</rss>

