<?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: Sum/total for groups &amp;amp; creating new &amp;quot;total&amp;quot; variable same for ALL group members in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Sum-total-for-groups-amp-creating-new-quot-total-quot-variable/m-p/729503#M227017</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/375683"&gt;@morichard&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;How can i add count rows by groups through proc sql AND THEN (this is the key thing I can't find instructions on) create a new "total" variable that is the same for each member of the group -- NOT just the Last member of the group. In conceptual terms, I want each group member to know how many members of its group have x=1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have dataset&lt;/P&gt;
&lt;P&gt;x y n&lt;/P&gt;
&lt;P&gt;0 a 0&lt;/P&gt;
&lt;P&gt;1 a 1&lt;/P&gt;
&lt;P&gt;0 b 0&lt;/P&gt;
&lt;P&gt;1 b 1&lt;/P&gt;
&lt;P&gt;1 b 2&lt;/P&gt;
&lt;P&gt;0 c 0&lt;/P&gt;
&lt;P&gt;1 c 1&lt;/P&gt;
&lt;P&gt;1 c 2&lt;/P&gt;
&lt;P&gt;1 c 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;wanted output&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x y n n2&lt;/P&gt;
&lt;P&gt;0 a 0 1&lt;/P&gt;
&lt;P&gt;1 a 1 1&lt;/P&gt;
&lt;P&gt;0 b 0 2&lt;/P&gt;
&lt;P&gt;1 b 1 2&lt;/P&gt;
&lt;P&gt;1 b 2 2&lt;/P&gt;
&lt;P&gt;0 c 0 3&lt;/P&gt;
&lt;P&gt;1 c 1 3&lt;/P&gt;
&lt;P&gt;1 c 2 3&lt;/P&gt;
&lt;P&gt;1 c 3 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Grouping variable is y, the variable to be counted/totaled is x.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i know&amp;nbsp; how to do it with data step:&lt;/P&gt;
&lt;P&gt;Proc sort data = have ;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;by y;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by y;&lt;/P&gt;
&lt;P&gt;if first.y then n =0;&lt;/P&gt;
&lt;P&gt;n + x;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;For future reference please specify which variable(s) define a group. It really isn't nice to make us guess.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A possible solution if the values of X are only ever 0, 1 and possibly missing:&lt;/P&gt;
&lt;PRE&gt;data have;
   input x y $ n;
datalines;
0 a 0
1 a 1
0 b 0
1 b 1
1 b 2
0 c 0
1 c 1
1 c 2
1 c 3
;

proc sql;
   create table want as 
   select x,y,n, sum(x) as n2
   from have
   group by y
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;There will be a note in the log:&lt;/P&gt;
&lt;PRE&gt;NOTE: The query requires remerging summary statistics back with the original data.
&lt;/PRE&gt;
&lt;P&gt;which is because it is doing exactly what you request: merging a single summary statistic (the sum of X) to all of the rows of the group.&lt;/P&gt;</description>
    <pubDate>Fri, 26 Mar 2021 22:03:02 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2021-03-26T22:03:02Z</dc:date>
    <item>
      <title>Sum/total for groups &amp; creating new "total" variable same for ALL group members</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-total-for-groups-amp-creating-new-quot-total-quot-variable/m-p/729446#M226988</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;How can i add count rows by groups through proc sql AND THEN (this is the key thing I can't find instructions on) create a new "total" variable that is the same for each member of the group -- NOT just the Last member of the group. In conceptual terms, I want each group member to know how many members of its group have x=1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Have dataset&lt;/P&gt;&lt;P&gt;x y n&lt;/P&gt;&lt;P&gt;0 a 0&lt;/P&gt;&lt;P&gt;1 a 1&lt;/P&gt;&lt;P&gt;0 b 0&lt;/P&gt;&lt;P&gt;1 b 1&lt;/P&gt;&lt;P&gt;1 b 2&lt;/P&gt;&lt;P&gt;0 c 0&lt;/P&gt;&lt;P&gt;1 c 1&lt;/P&gt;&lt;P&gt;1 c 2&lt;/P&gt;&lt;P&gt;1 c 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;wanted output&amp;nbsp;&lt;/P&gt;&lt;P&gt;x y n n2&lt;/P&gt;&lt;P&gt;0 a 0 1&lt;/P&gt;&lt;P&gt;1 a 1 1&lt;/P&gt;&lt;P&gt;0 b 0 2&lt;/P&gt;&lt;P&gt;1 b 1 2&lt;/P&gt;&lt;P&gt;1 b 2 2&lt;/P&gt;&lt;P&gt;0 c 0 3&lt;/P&gt;&lt;P&gt;1 c 1 3&lt;/P&gt;&lt;P&gt;1 c 2 3&lt;/P&gt;&lt;P&gt;1 c 3 3&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Grouping variable is y, the variable to be counted/totaled is x.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i know&amp;nbsp; how to do it with data step:&lt;/P&gt;&lt;P&gt;Proc sort data = have ;&amp;nbsp;&lt;/P&gt;&lt;P&gt;by y;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want;&lt;/P&gt;&lt;P&gt;set have;&lt;/P&gt;&lt;P&gt;by y;&lt;/P&gt;&lt;P&gt;if first.y then n =0;&lt;/P&gt;&lt;P&gt;n + x;&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 17:08:16 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-total-for-groups-amp-creating-new-quot-total-quot-variable/m-p/729446#M226988</guid>
      <dc:creator>morichard</dc:creator>
      <dc:date>2021-03-26T17:08:16Z</dc:date>
    </item>
    <item>
      <title>Re: Sum/total for groups &amp; creating new "total" variable same for ALL group members</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-total-for-groups-amp-creating-new-quot-total-quot-variable/m-p/729457#M226993</link>
      <description>&lt;P&gt;Since you are bothering to sort the data set, here is a single data step solution to use that sorted data set&amp;nbsp; (changed "n+x" to "n2+x" below):&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  by y;
  if first.y then n2 =0;
  n2 + x;
  if last.y;
  do until (last.y);
    set have;
    by y;
    output;
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;There are two &lt;STRONG&gt;SET&lt;/STRONG&gt; plus &lt;STRONG&gt;BY&lt;/STRONG&gt;&amp;nbsp;pairs.&amp;nbsp; So the &lt;EM&gt;&lt;STRONG&gt;first.&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;and &lt;EM&gt;&lt;STRONG&gt;last.&lt;/STRONG&gt;&lt;/EM&gt;&amp;nbsp;dummy variables appear to be paired with the most-recently-used &lt;STRONG&gt;SET/BY&lt;/STRONG&gt; combination.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 18:34:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-total-for-groups-amp-creating-new-quot-total-quot-variable/m-p/729457#M226993</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2021-03-26T18:34:31Z</dc:date>
    </item>
    <item>
      <title>Re: Sum/total for groups &amp; creating new "total" variable same for ALL group members</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Sum-total-for-groups-amp-creating-new-quot-total-quot-variable/m-p/729503#M227017</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/375683"&gt;@morichard&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;How can i add count rows by groups through proc sql AND THEN (this is the key thing I can't find instructions on) create a new "total" variable that is the same for each member of the group -- NOT just the Last member of the group. In conceptual terms, I want each group member to know how many members of its group have x=1.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Have dataset&lt;/P&gt;
&lt;P&gt;x y n&lt;/P&gt;
&lt;P&gt;0 a 0&lt;/P&gt;
&lt;P&gt;1 a 1&lt;/P&gt;
&lt;P&gt;0 b 0&lt;/P&gt;
&lt;P&gt;1 b 1&lt;/P&gt;
&lt;P&gt;1 b 2&lt;/P&gt;
&lt;P&gt;0 c 0&lt;/P&gt;
&lt;P&gt;1 c 1&lt;/P&gt;
&lt;P&gt;1 c 2&lt;/P&gt;
&lt;P&gt;1 c 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;wanted output&amp;nbsp;&lt;/P&gt;
&lt;P&gt;x y n n2&lt;/P&gt;
&lt;P&gt;0 a 0 1&lt;/P&gt;
&lt;P&gt;1 a 1 1&lt;/P&gt;
&lt;P&gt;0 b 0 2&lt;/P&gt;
&lt;P&gt;1 b 1 2&lt;/P&gt;
&lt;P&gt;1 b 2 2&lt;/P&gt;
&lt;P&gt;0 c 0 3&lt;/P&gt;
&lt;P&gt;1 c 1 3&lt;/P&gt;
&lt;P&gt;1 c 2 3&lt;/P&gt;
&lt;P&gt;1 c 3 3&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Grouping variable is y, the variable to be counted/totaled is x.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;i know&amp;nbsp; how to do it with data step:&lt;/P&gt;
&lt;P&gt;Proc sort data = have ;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;by y;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;by y;&lt;/P&gt;
&lt;P&gt;if first.y then n =0;&lt;/P&gt;
&lt;P&gt;n + x;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;For future reference please specify which variable(s) define a group. It really isn't nice to make us guess.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;A possible solution if the values of X are only ever 0, 1 and possibly missing:&lt;/P&gt;
&lt;PRE&gt;data have;
   input x y $ n;
datalines;
0 a 0
1 a 1
0 b 0
1 b 1
1 b 2
0 c 0
1 c 1
1 c 2
1 c 3
;

proc sql;
   create table want as 
   select x,y,n, sum(x) as n2
   from have
   group by y
   ;
quit;&lt;/PRE&gt;
&lt;P&gt;There will be a note in the log:&lt;/P&gt;
&lt;PRE&gt;NOTE: The query requires remerging summary statistics back with the original data.
&lt;/PRE&gt;
&lt;P&gt;which is because it is doing exactly what you request: merging a single summary statistic (the sum of X) to all of the rows of the group.&lt;/P&gt;</description>
      <pubDate>Fri, 26 Mar 2021 22:03:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Sum-total-for-groups-amp-creating-new-quot-total-quot-variable/m-p/729503#M227017</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-03-26T22:03:02Z</dc:date>
    </item>
  </channel>
</rss>

