<?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: Proc Report, summary by group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Proc-Report-summary-by-group/m-p/620162#M182191</link>
    <description>&lt;P&gt;May not be the cleanest solution but, it goes like this:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Create 'have2' which is two copies of 'have'
&lt;UL&gt;
&lt;LI&gt;the first will be the normal 'have' but with VIEW='DATA'&lt;/LI&gt;
&lt;LI&gt;the second of which has GROUPS='All' and VIEW='SMRY'&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;Run proc report with additional grouping variable VIEW but define as noprint so that it is not visible&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	set sashelp.class;
	TEST=catx(' ', 'Test', 1+mod(_n_-1, 2));
	GROUPS=catx(' ', 'Group', byte(mod(_n_-1, 3)+65));
	keep name sex test groups;
run;

data have2;
	set have(in=a) have(in=b);

	if b then
		do;
			VIEW='SMRY';
			GROUPS='All';
			output;
		end;
	else
		do;
			VIEW='DATA';
			output;
		end;
run;

PROC REPORT DATA=have2;
	COLUMNS view groups test sex, (n pct) all;
	DEFINE view / group noprint;
	DEFINE groups / group;
	DEFINE test / GROUP "test ";
	DEFINE sex/ ' ' across;
	DEFINE pct / "%" computed format=percent8.;
	DEFINE all / "Total" computed;

	/* Sum total number of responses  */
	compute before test;
		den=_c4_ + _c6_;
	endcomp;

	/*	 counter to shade rows*/
	compute before groups;
		I+1;
	ENDCOMP;
	compute groups;

		if mod(i, 2) eq 1 then
			call define(_row_, "style", "STYLE=[background=lightgrey]");
	endcomp;

	/* percentage */
	compute pct;
		_c5_=_c4_ / den;
		_c7_=_c6_ / den;
	endcomp;
	compute all;
		all=_c4_ + _c6_;
	endcomp;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot from 2020-01-27 07-22-25.png" style="width: 259px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35629i78C859B5F0FB162E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot from 2020-01-27 07-22-25.png" alt="Screenshot from 2020-01-27 07-22-25.png" /&gt;&lt;/span&gt;&lt;BR /&gt;(Not your data so values will be different)&lt;/P&gt;</description>
    <pubDate>Mon, 27 Jan 2020 14:23:19 GMT</pubDate>
    <dc:creator>unison</dc:creator>
    <dc:date>2020-01-27T14:23:19Z</dc:date>
    <item>
      <title>Proc Report, summary by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Report-summary-by-group/m-p/620145#M182184</link>
      <description>&lt;P&gt;I am trying to produce the following table:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 336pt;" border="0" summary="Page Layout" width="447px" frame="void" rules="none" cellspacing="0" cellpadding="0"&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="50px" height="20" class="xl65" style="height: 15.0pt; width: 48pt;"&gt;&lt;!--BEGINTABLE--&gt;&lt;/TD&gt;
&lt;TD width="51px" class="xl65" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="62px" class="xl65" style="width: 48pt;"&gt;Females&lt;/TD&gt;
&lt;TD width="51px" class="xl65" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl65" style="width: 48pt;"&gt;Males&lt;/TD&gt;
&lt;TD width="52px" class="xl65" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl65" style="width: 48pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="50px" height="20" class="xl65" style="height: 15.0pt;"&gt;Group&lt;/TD&gt;
&lt;TD width="51px" class="xl65"&gt;Test&lt;/TD&gt;
&lt;TD width="62px" class="xl65"&gt;n&lt;/TD&gt;
&lt;TD width="51px" class="xl65"&gt;%&lt;/TD&gt;
&lt;TD width="52px" class="xl65"&gt;n&lt;/TD&gt;
&lt;TD width="52px" class="xl65"&gt;%&lt;/TD&gt;
&lt;TD width="52px" class="xl65"&gt;Total&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="50px" height="20" class="xl66" style="height: 15.0pt;"&gt;Group a&lt;/TD&gt;
&lt;TD width="51px" class="xl66"&gt;Test 1&lt;/TD&gt;
&lt;TD width="62px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="50px" height="20" class="xl66" style="height: 15.0pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px" class="xl66"&gt;Test 2&lt;/TD&gt;
&lt;TD width="62px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="50px" height="20" class="xl65" style="height: 15.0pt;"&gt;Group b&lt;/TD&gt;
&lt;TD width="51px" class="xl65"&gt;Test 1&lt;/TD&gt;
&lt;TD width="62px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="50px" height="20" class="xl65" style="height: 15.0pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px" class="xl65"&gt;Test 2&lt;/TD&gt;
&lt;TD width="62px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="50px" height="20" class="xl66" style="height: 15.0pt;"&gt;Group c&lt;/TD&gt;
&lt;TD width="51px" class="xl66"&gt;Test 1&lt;/TD&gt;
&lt;TD width="62px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px" class="xl64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="50px" height="20" class="xl66" style="height: 15.0pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px" class="xl66"&gt;Test 2&lt;/TD&gt;
&lt;TD width="62px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px" class="xl64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl64"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl63"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="50px" height="20" class="xl67" style="height: 15.0pt;"&gt;Total&lt;/TD&gt;
&lt;TD width="51px" class="xl67"&gt;Test 1&lt;/TD&gt;
&lt;TD width="62px" class="xl68"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px" class="xl69"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl68"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl69"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl68"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="50px" height="20" class="xl67" style="height: 15.0pt;"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px" class="xl67"&gt;Test 2&lt;/TD&gt;
&lt;TD width="62px" class="xl68"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="51px" class="xl68"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl68"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl68"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;TD width="52px" class="xl68"&gt;&amp;nbsp;&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The closest I have come is only one row for the "total", but I want to show the overall frequency for each test. The code I have now is:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
PROC REPORT DATA=have ;
	COLUMNS groups test sex, (n pct) all;
	DEFINE groups / group ;
	DEFINE test / GROUP "test ";
	DEFINE sex/  ' ' across;
	DEFINE pct / "%" computed format=percent8.;
	DEFINE all / "Total" computed;

   /* Sum total number of responses  */
	compute before test ;
		den = _c3_ + _c5_;
	endcomp;

/*	 counter to shade rows*/
   	compute before groups ;
   		I+1;
	ENDCOMP;

	compute groups ;
		if mod(i,2) eq 1
		then call define(_row_, "style", "STYLE=[background=lightgrey]");
	endcomp;

   /* percentage */
	compute pct;
		_c4_ = _c3_ / den;
		_c6_ = _c5_ / den;
	endcomp;


	compute all;
		all = _c3_ + _c5_;
	endcomp;

	rbreak after  / summarize;

	compute after /style={just=r};
		groups = "Total";
		_c4_ = _c3_ / all;
		_c6_ = _c5_ / all;
	endcomp;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Jan 2020 12:40:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Report-summary-by-group/m-p/620145#M182184</guid>
      <dc:creator>MB_Analyst</dc:creator>
      <dc:date>2020-01-27T12:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: Proc Report, summary by group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Proc-Report-summary-by-group/m-p/620162#M182191</link>
      <description>&lt;P&gt;May not be the cleanest solution but, it goes like this:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Create 'have2' which is two copies of 'have'
&lt;UL&gt;
&lt;LI&gt;the first will be the normal 'have' but with VIEW='DATA'&lt;/LI&gt;
&lt;LI&gt;the second of which has GROUPS='All' and VIEW='SMRY'&lt;/LI&gt;
&lt;/UL&gt;
&lt;/LI&gt;
&lt;LI&gt;Run proc report with additional grouping variable VIEW but define as noprint so that it is not visible&lt;/LI&gt;
&lt;/UL&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
	set sashelp.class;
	TEST=catx(' ', 'Test', 1+mod(_n_-1, 2));
	GROUPS=catx(' ', 'Group', byte(mod(_n_-1, 3)+65));
	keep name sex test groups;
run;

data have2;
	set have(in=a) have(in=b);

	if b then
		do;
			VIEW='SMRY';
			GROUPS='All';
			output;
		end;
	else
		do;
			VIEW='DATA';
			output;
		end;
run;

PROC REPORT DATA=have2;
	COLUMNS view groups test sex, (n pct) all;
	DEFINE view / group noprint;
	DEFINE groups / group;
	DEFINE test / GROUP "test ";
	DEFINE sex/ ' ' across;
	DEFINE pct / "%" computed format=percent8.;
	DEFINE all / "Total" computed;

	/* Sum total number of responses  */
	compute before test;
		den=_c4_ + _c6_;
	endcomp;

	/*	 counter to shade rows*/
	compute before groups;
		I+1;
	ENDCOMP;
	compute groups;

		if mod(i, 2) eq 1 then
			call define(_row_, "style", "STYLE=[background=lightgrey]");
	endcomp;

	/* percentage */
	compute pct;
		_c5_=_c4_ / den;
		_c7_=_c6_ / den;
	endcomp;
	compute all;
		all=_c4_ + _c6_;
	endcomp;
RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Screenshot from 2020-01-27 07-22-25.png" style="width: 259px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/35629i78C859B5F0FB162E/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Screenshot from 2020-01-27 07-22-25.png" alt="Screenshot from 2020-01-27 07-22-25.png" /&gt;&lt;/span&gt;&lt;BR /&gt;(Not your data so values will be different)&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jan 2020 14:23:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Proc-Report-summary-by-group/m-p/620162#M182191</guid>
      <dc:creator>unison</dc:creator>
      <dc:date>2020-01-27T14:23:19Z</dc:date>
    </item>
  </channel>
</rss>

