<?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: Subtotal and Grand Total in dataset in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527238#M143697</link>
    <description>&lt;P&gt;Are you allowed to use PROC SQL?&lt;/P&gt;</description>
    <pubDate>Tue, 15 Jan 2019 08:51:44 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2019-01-15T08:51:44Z</dc:date>
    <item>
      <title>Subtotal and Grand Total in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527236#M143696</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;I have this dataset where I want to display the subtotals and grand total without using a proc tabulate.&lt;/P&gt;
&lt;P&gt;From my "HAVE" dataset, I'd like the totals (num1 and num2) be calculated for each Group, and the grand total be calculated, placed in the first cell of the "WANT" dataset. I'd like to have the output in SAS dataset form, not Proc Report nor Tabulate. Thank you!&lt;/P&gt;
&lt;P&gt;data have;&lt;BR /&gt;input Group :$20. Member :$20. num1 num2;&lt;BR /&gt;datalines;&lt;BR /&gt;Group_a Member_a_1 6 0&lt;BR /&gt;Group_a Member_a_2 4 1&lt;BR /&gt;Group_b Member_b_1 6 3&lt;BR /&gt;Group_b Member_b_2 3 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;input final_text :$20. num1 num2;&lt;BR /&gt;datalines;&lt;BR /&gt;Grand_Total 19 5&lt;BR /&gt;Group_a 10 1&lt;BR /&gt;Member_a_1 6 0&lt;BR /&gt;Member_a_2 4 1&lt;BR /&gt;Group_b 9 4&lt;BR /&gt;Member_b_1 6 3&lt;BR /&gt;Member_b_2 3 1&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jan 2019 08:41:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527236#M143696</guid>
      <dc:creator>angeliquec</dc:creator>
      <dc:date>2019-01-15T08:41:39Z</dc:date>
    </item>
    <item>
      <title>Re: Subtotal and Grand Total in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527238#M143697</link>
      <description>&lt;P&gt;Are you allowed to use PROC SQL?&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jan 2019 08:51:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527238#M143697</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2019-01-15T08:51:44Z</dc:date>
    </item>
    <item>
      <title>Re: Subtotal and Grand Total in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527239#M143698</link>
      <description>&lt;P&gt;Yes.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Jan 2019 08:57:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527239#M143698</guid>
      <dc:creator>angeliquec</dc:creator>
      <dc:date>2019-01-15T08:57:04Z</dc:date>
    </item>
    <item>
      <title>Re: Subtotal and Grand Total in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527243#M143700</link>
      <description>&lt;P&gt;Data step language is good for this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Group :$20. Member :$20. num1 num2;
datalines;
Group_a Member_a_1 6 0
Group_a Member_a_2 4 1
Group_b Member_b_1 6 3
Group_b Member_b_2 3 1
;
run;

data want;
set have end=eof;
by group;
retain
  sum_num1
  sum_num2
  grand_num1 0
  grand_num2 0
;
if first.group
then do;
  sum_num1 = 0;
  sum_num2 = 0;
end;
grand_num1 + num1;
grand_num2 + num2;
sum_num1 + num1;
sum_num2 + num2;
output;
if last.group
then do;
  member = 'Group Sum';
  num1 = sum_num1;
  num2 = sum_num2;
  output;
end;
if eof
then do;
  member = 'Grand Total';
  num1 = grand_num1;
  num2 = grand_num2;
  output;
end;
drop sum: grand:;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Jan 2019 09:13:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527243#M143700</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-01-15T09:13:56Z</dc:date>
    </item>
    <item>
      <title>Re: Subtotal and Grand Total in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527250#M143705</link>
      <description>&lt;P&gt;Open want dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input Group :$20. Member :$20. num1 num2;
datalines;
Group_a Member_a_1 6 0
Group_a Member_a_2 4 1
Group_b Member_b_1 6 3
Group_b Member_b_2 3 1
;
run;

ods select none;
proc report data=have out=want nowd;
column  Group  Member  num1 num2;
define group/group;
compute before;
 Member='Grand_Total';
endcomp;
compute before group;
 Member=group;
endcomp;
break before group/summarize;
rbreak before/summarize;
run;
ods select all;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 15 Jan 2019 09:51:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527250#M143705</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2019-01-15T09:51:38Z</dc:date>
    </item>
    <item>
      <title>Re: Subtotal and Grand Total in dataset</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527594#M143856</link>
      <description>&lt;P&gt;thank you! It's also a good solution&lt;/P&gt;</description>
      <pubDate>Wed, 16 Jan 2019 01:45:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Subtotal-and-Grand-Total-in-dataset/m-p/527594#M143856</guid>
      <dc:creator>angeliquec</dc:creator>
      <dc:date>2019-01-16T01:45:44Z</dc:date>
    </item>
  </channel>
</rss>

