<?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: grouping some of the members of a variable and adding a grand total in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489142#M127628</link>
    <description>&lt;P&gt;It is possible replace the insert with union as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select mem length=100,sum(mem in ('F' 'C')) as freq, sum(percent) as percents from have where mem in ('F' 'C') group by mem
union
select 'Grand Total' as mem length=100, count(*) as freq, sum(percent) as percents from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 23 Aug 2018 07:02:13 GMT</pubDate>
    <dc:creator>Jagadishkatam</dc:creator>
    <dc:date>2018-08-23T07:02:13Z</dc:date>
    <item>
      <title>grouping some of the members of a variable and adding a grand total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489127#M127616</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a data set and I need to group the type of member "mem" and get a grand total at the bottom.&amp;nbsp; When I am trying to limit the number of members to group, i am still getting the grand total for all the members.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have;&lt;BR /&gt;input mem $ frequency percent;&lt;BR /&gt;datalines;&lt;BR /&gt;M 240 24.0&lt;BR /&gt;M 320 32.0&lt;BR /&gt;F 120 12.0&lt;BR /&gt;M 80 8.0&lt;BR /&gt;C 68 6.8&lt;BR /&gt;M 42 4.2&lt;BR /&gt;C 130 13.0&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Proc Sql;&lt;BR /&gt;Create table Game as&lt;BR /&gt;select mem as Member , count(frequency) as Freq , sum(percent) as Percent&lt;BR /&gt;from have&lt;BR /&gt;group by mem&lt;BR /&gt;having mem in ("C", "F")&lt;BR /&gt;union&lt;BR /&gt;Select "Grand tot" as mem&lt;BR /&gt;,count(frequency) as Freq&lt;BR /&gt;,sum(percent) as Percent&lt;BR /&gt;from have;&lt;BR /&gt;Quit;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The output that I am getting is&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Obs Member&amp;nbsp; Freq Percent&lt;/P&gt;&lt;P&gt;1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&amp;nbsp; &amp;nbsp; &amp;nbsp; 19.8&lt;/P&gt;&lt;P&gt;2&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; F&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;12.0&lt;/P&gt;&lt;P&gt;3&amp;nbsp; &amp;nbsp; &amp;nbsp;Grand tot&amp;nbsp; 7&amp;nbsp; &amp;nbsp; &amp;nbsp;100.0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The Grand tot is not giving the value for the selected number of "mem".&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried by putting the having clause in the second part of the union too, however I did not achive results.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Please suggest a viable option.&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanking you!&lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 05:28:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489127#M127616</guid>
      <dc:creator>75063</dc:creator>
      <dc:date>2018-08-23T05:28:42Z</dc:date>
    </item>
    <item>
      <title>Re: grouping some of the members of a variable and adding a grand total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489129#M127617</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select mem length=100,sum(mem in ('F' 'C')) as freq, sum(percent) as percents from have where mem in ('F' 'C') group by mem;
insert into want (mem, freq, percents) 
select 'Grand Total' as mem length=100, count(*) as freq, sum(percent) as percents from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Aug 2018 05:43:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489129#M127617</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-08-23T05:43:30Z</dc:date>
    </item>
    <item>
      <title>Re: grouping some of the members of a variable and adding a grand total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489138#M127624</link>
      <description>&lt;P&gt;many thanks for the guidance, It worked for me. However, just wanted to know if it is possible to do with a "UNION" too (The way&amp;nbsp; i was trying).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you!&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 06:37:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489138#M127624</guid>
      <dc:creator>75063</dc:creator>
      <dc:date>2018-08-23T06:37:53Z</dc:date>
    </item>
    <item>
      <title>Re: grouping some of the members of a variable and adding a grand total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489142#M127628</link>
      <description>&lt;P&gt;It is possible replace the insert with union as below&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as select mem length=100,sum(mem in ('F' 'C')) as freq, sum(percent) as percents from have where mem in ('F' 'C') group by mem
union
select 'Grand Total' as mem length=100, count(*) as freq, sum(percent) as percents from have;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 23 Aug 2018 07:02:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489142#M127628</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2018-08-23T07:02:13Z</dc:date>
    </item>
    <item>
      <title>Re: grouping some of the members of a variable and adding a grand total</title>
      <link>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489143#M127629</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/148556"&gt;@75063&lt;/a&gt;:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;How do you expect your grand total to be limited to mem in ("C","F") if you're not qualifying the input by these values in the bottom part of the union? Just add the corresponding WHERE clause to it:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql ;                           
  create table Game as               
  select mem               as Member 
       , count (frequency) as Freq   
       , sum (percent)     as Percent
  from   have                        
  where  mem in ("C", "F")           
  group  mem                         
  union                              
  select "Grand tot"       as Member 
       , count (frequency) as Freq   
       , sum (percent)     as Percent
  from have                          
  where  mem in ("C", "F")           
  ;                                  
quit ;                               
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Alternatively, it can be done in a DATA step using the hash object. Admittedly, it's more involved programmatically, but it gets there in a single pass over the subset input file (while SQL needs two):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_ ;                                                       
  dcl hash h (ordered:"A") ;                                        
  h.defineKey  ("Member") ;                                         
  h.defineData ("Member", "Freq", "Percent") ;                      
  h.defineDone () ;                                                 
  do TF = 1 by 1 until (z) ;                                        
    set have (rename=(Percent=P) where=(mem in ("C","F"))) end = z ;
    Member = put (mem, $9.) ;                                       
    if h.find() then call missing (Freq, Percent) ; /* Note: It means "if NOT found" */            
    Freq    + 1 ;                                                   
    Percent + P ;                                                   
    h.replace() ;                                                   
    TP + P ;                                                        
  end ;                                                             
  h.add (key:"Grand tot", data:"Grand tot", data:TF, data:TP) ;     
  h.output (dataset:"Game") ;                                       
run ;                                                               
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;HTH&lt;/P&gt;
&lt;P&gt;Paul D.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Aug 2018 07:10:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/grouping-some-of-the-members-of-a-variable-and-adding-a-grand/m-p/489143#M127629</guid>
      <dc:creator>hashman</dc:creator>
      <dc:date>2018-08-23T07:10:13Z</dc:date>
    </item>
  </channel>
</rss>

