<?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 - Calculation of Mean and SUM based on row in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423286#M68103</link>
    <description>&lt;P&gt;Thank you very much to both, I will try both approaches.&amp;nbsp; I am a bit hesitant to use PROC Tabulate as my real code groups the rows and adds aggregates (totals) after certain groups.&amp;nbsp; I would like to keep this.&amp;nbsp; Can Proc Tabulate replicate this as well.&amp;nbsp; Not very versed in this Procedure.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC REPORT DATA=WORK.RANK_ALL;
COLUMNS Rank Grouping REGROUPING Amounts, Summary_date ('Overall' AMOUNTS = TotalAmount) _dummy ;
WHERE SEGMENT_Lvl_1 EQ 'Bespoke' AND YEAR(SUMMARY_DATE)ne 2017;
DEFINE Rank / Group '' NOPRINT;
DEFINE Grouping / GROUP ORDER=INTERNAL '' LEFT;
DEFINE REGROUPING / GROUP '' ;
DEFINE SUMMARY_DATE / ACROSS ORDER=INTERNAL '' FORMAT=DDMMYYd10.; /*mmyyd.*/
DEFINE AMOUNTS / ANALYSIS SUM '' FORMAT=COMMA20.2;
DEFINE _dummy / COMPUTED '' NOPRINT;
DEFINE TotalAmount / SUM 'Total' NOPRINT;
BREAK AFTER Rank / SUMMARIZE STYLE={foreground=black fontweight=bold fontstyle=italic};
COMPUTE _dummy / CHAR LENGTH = 21;
	_dummy = catx(":", Rank, _break_);
		IF lowcase(_break_) = "rank" AND Rank NOT IN ('a','b','e','f','i','j','k','l','m') THEN DO;
		REGROUPING = "Total";
		CALL MISSING(Rank);
		END;
		IF lowcase(_break_) = "rank" AND Rank IN ('a','b','e','f','i','j','k','l','m') THEN DO;
		CALL MISSING(Rank, REGROUPING);
		CALL MISSING(_c3_,_c4_,_c5_,_c6_,_c7_,_c8_,_c9_,_c10_,_c11_,_c12_,_c13_,_c14_,_c15_,_c16_,_c17_,_c18_,_c19_,_c20_,_c21_,_c22_,_c23_,_c24_,_c25_,_c26_);
		END;

ENDCOMP;
COMPUTE AFTER Rank;
	LINE '';
ENDCOMP;


RUN;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Which gives me this output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Report.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/17509i4ED77B6E11ACB8E6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Report.JPG" alt="Report.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see I am adding totals to certain groups and not others.&amp;nbsp; I will have a look at PROC Tabulate and see if I can create this output.&amp;nbsp; Perhaps I should just work on my source data and get that aligned first to make it easier.&amp;nbsp;My goal was to group the dates as Years and Quarters which I have now achieved thanks to a community member.&amp;nbsp; The only thing left is to sum or mean based on the grouping.&amp;nbsp; I will give PROC Tabulate a try.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks all again,&lt;/P&gt;&lt;P&gt;Aksel&lt;/P&gt;</description>
    <pubDate>Fri, 22 Dec 2017 09:16:05 GMT</pubDate>
    <dc:creator>asdf12_12</dc:creator>
    <dc:date>2017-12-22T09:16:05Z</dc:date>
    <item>
      <title>PROC REPORT - Calculation of Mean and SUM based on row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423124#M68095</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have this sample data giving me this sample report. &amp;nbsp;I have row A and row B. &amp;nbsp;I would like the calculations to be either Mean or Sum based on the Row values. &amp;nbsp;ie if in the first column row is A then Sum if it is B then mean.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
  do groupValue = "A", "B";
    do someDate = intnx("year", today(), -2, "B") to mdy(8,15, year(today()));
      value = ceil(ranuni(0) * 1000);
      output;
    end;
  end;
  format
    someDate date9.
    value commax14.
  ;
run;

proc format;
  value year_current
    low - "31dec2016"d = [year4.]
    "01jan2017"d - "31jul2017"d = "2017H1"
    "01aug2017"d - "31dec2017"d = "2017H2"
  ;
run;

proc report data=have;
  column groupValue someDate, value;
  define groupValue / group;
  define someDate / across format=year_current.;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Which gives me:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.JPG" style="width: 315px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/17496i55AC82FB328C50B2/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.JPG" alt="Capture.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I would like the values in row A to be means and have the row B as sums (as it already is).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Your help is much appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards,&lt;/P&gt;</description>
      <pubDate>Thu, 21 Dec 2017 15:30:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423124#M68095</guid>
      <dc:creator>asdf12_12</dc:creator>
      <dc:date>2017-12-21T15:30:08Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT - Calculation of Mean and SUM based on row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423133#M68098</link>
      <description>&lt;P&gt;This would be very simple to do if you use data steps plus PROC MEANS/PROC SUMMARY to obtain the&amp;nbsp;desired results before you get to PROC REPORT. Then you could use PROC REPORT to simply produce the report, and you are not using PROC REPORT to do the calculations.&lt;/P&gt;</description>
      <pubDate>Thu, 21 Dec 2017 16:00:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423133#M68098</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-12-21T16:00:21Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT - Calculation of Mean and SUM based on row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423242#M68101</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Or, as an alternate approach, another idea is to switch to PROC TABULATE. If you run this example, which uses SASHELP.SHOES, you'll see that a separate statistic was calculated for every row:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc tabulate data=sashelp.shoes f=comma18.;
  var sales inventory returns;
  class region;
  table sales*sum inventory*mean returns*max,
        region all;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You might have to restructure your data a bit to get this, but it is very do-able. So, for example, if your existing structure of data were in a file called have.csv (attached), then you could product this output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="use_tabulate.png" style="width: 375px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/17505iF4DA89A830423DDD/image-size/large?v=v2&amp;amp;px=999" role="button" title="use_tabulate.png" alt="use_tabulate.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;With this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have ;
  infile 'c:\temp\have.csv' dlm=',' dsd;
  input groupvalue $ somedate : date9. value;
  format someDate date9. value commax14. ;
run;

proc sort data=have; by somedate groupvalue;
run;

proc transpose data=have out=hav_out(drop=_name_);
  by somedate;
  id groupvalue;
  var value;
run;

proc tabulate data=hav_out f=comma18.;
  var a b;
  class somedate;
  table a*sum b*mean,
        somedate all;
  format somedate year_current.;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 Dec 2017 01:07:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423242#M68101</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2017-12-22T01:07:32Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT - Calculation of Mean and SUM based on row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423286#M68103</link>
      <description>&lt;P&gt;Thank you very much to both, I will try both approaches.&amp;nbsp; I am a bit hesitant to use PROC Tabulate as my real code groups the rows and adds aggregates (totals) after certain groups.&amp;nbsp; I would like to keep this.&amp;nbsp; Can Proc Tabulate replicate this as well.&amp;nbsp; Not very versed in this Procedure.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC REPORT DATA=WORK.RANK_ALL;
COLUMNS Rank Grouping REGROUPING Amounts, Summary_date ('Overall' AMOUNTS = TotalAmount) _dummy ;
WHERE SEGMENT_Lvl_1 EQ 'Bespoke' AND YEAR(SUMMARY_DATE)ne 2017;
DEFINE Rank / Group '' NOPRINT;
DEFINE Grouping / GROUP ORDER=INTERNAL '' LEFT;
DEFINE REGROUPING / GROUP '' ;
DEFINE SUMMARY_DATE / ACROSS ORDER=INTERNAL '' FORMAT=DDMMYYd10.; /*mmyyd.*/
DEFINE AMOUNTS / ANALYSIS SUM '' FORMAT=COMMA20.2;
DEFINE _dummy / COMPUTED '' NOPRINT;
DEFINE TotalAmount / SUM 'Total' NOPRINT;
BREAK AFTER Rank / SUMMARIZE STYLE={foreground=black fontweight=bold fontstyle=italic};
COMPUTE _dummy / CHAR LENGTH = 21;
	_dummy = catx(":", Rank, _break_);
		IF lowcase(_break_) = "rank" AND Rank NOT IN ('a','b','e','f','i','j','k','l','m') THEN DO;
		REGROUPING = "Total";
		CALL MISSING(Rank);
		END;
		IF lowcase(_break_) = "rank" AND Rank IN ('a','b','e','f','i','j','k','l','m') THEN DO;
		CALL MISSING(Rank, REGROUPING);
		CALL MISSING(_c3_,_c4_,_c5_,_c6_,_c7_,_c8_,_c9_,_c10_,_c11_,_c12_,_c13_,_c14_,_c15_,_c16_,_c17_,_c18_,_c19_,_c20_,_c21_,_c22_,_c23_,_c24_,_c25_,_c26_);
		END;

ENDCOMP;
COMPUTE AFTER Rank;
	LINE '';
ENDCOMP;


RUN;&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Which gives me this output:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Report.JPG" style="width: 600px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/17509i4ED77B6E11ACB8E6/image-size/large?v=v2&amp;amp;px=999" role="button" title="Report.JPG" alt="Report.JPG" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;As you can see I am adding totals to certain groups and not others.&amp;nbsp; I will have a look at PROC Tabulate and see if I can create this output.&amp;nbsp; Perhaps I should just work on my source data and get that aligned first to make it easier.&amp;nbsp;My goal was to group the dates as Years and Quarters which I have now achieved thanks to a community member.&amp;nbsp; The only thing left is to sum or mean based on the grouping.&amp;nbsp; I will give PROC Tabulate a try.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks all again,&lt;/P&gt;&lt;P&gt;Aksel&lt;/P&gt;</description>
      <pubDate>Fri, 22 Dec 2017 09:16:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423286#M68103</guid>
      <dc:creator>asdf12_12</dc:creator>
      <dc:date>2017-12-22T09:16:05Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT - Calculation of Mean and SUM based on row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423388#M68109</link>
      <description>Hi:&lt;BR /&gt;  TABULATE can't do what you're doing with PROC REPORT.&lt;BR /&gt;cynthia</description>
      <pubDate>Fri, 22 Dec 2017 21:28:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423388#M68109</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2017-12-22T21:28:20Z</dc:date>
    </item>
    <item>
      <title>Re: PROC REPORT - Calculation of Mean and SUM based on row</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423563#M68133</link>
      <description>&lt;P&gt;Thank you very much as always.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Dec 2017 11:17:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-REPORT-Calculation-of-Mean-and-SUM-based-on-row/m-p/423563#M68133</guid>
      <dc:creator>asdf12_12</dc:creator>
      <dc:date>2017-12-26T11:17:52Z</dc:date>
    </item>
  </channel>
</rss>

