<?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 TABULATE Sum statistic in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/PROC-TABULATE-Sum-statistic/m-p/67293#M19248</link>
    <description>SamuelG&lt;BR /&gt;
&lt;BR /&gt;
If I understand what you are attempting, I would be very tempted to summarize the data using proc summary with the class variables to get the sums for each group and then run the resulting data set through a data step to calculate the rates. Then the _type_ variable can be selected and/or formatted to show the various groups you need. The country/year/product combinations that don't actually have any data to sum could be handled in Tabulate with a preloadfmt option to display blank rows or columns as needed.&lt;BR /&gt;
&lt;BR /&gt;
proc summary data=sashelp.prdsal3;&lt;BR /&gt;
   class country year product;&lt;BR /&gt;
   var predict actual;&lt;BR /&gt;
   output out=prodsum sum=;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data prodrate;&lt;BR /&gt;
   set prodsum;&lt;BR /&gt;
   rate=predict/actual;&lt;BR /&gt;
   if _type_=6 and product='' then product='All'; /* add a useful description to display below*/&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* this is using tabulate just arrange precalculated values*/&lt;BR /&gt;
/* shows year and individual products or all products */&lt;BR /&gt;
proc tabulate data=prodrate;&lt;BR /&gt;
   where _type_ in (6,7);&lt;BR /&gt;
   class country year product/order=unformatted missing;&lt;BR /&gt;
   var predict actual rate;&lt;BR /&gt;
   table year=' '*product=' ',&lt;BR /&gt;
         country*max=' '*(predict actual rate)&lt;BR /&gt;
         /printmiss misstext='0';&lt;BR /&gt;
run;</description>
    <pubDate>Fri, 28 Aug 2009 21:54:30 GMT</pubDate>
    <dc:creator>ballardw</dc:creator>
    <dc:date>2009-08-28T21:54:30Z</dc:date>
    <item>
      <title>PROC TABULATE Sum statistic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-TABULATE-Sum-statistic/m-p/67290#M19245</link>
      <description>Hello,&lt;BR /&gt;
&lt;BR /&gt;
I have just noticed that I was doing something wrong with my PROC TABULATE. &lt;BR /&gt;
&lt;BR /&gt;
Basically, I would like the newly created variable "Rate" to calculate predict /actual column by column (INCLUDING the TOTAL column). It does it fine until the TOTAL column, where SAS sums up all the Rates instead of dividing predict/actual. If you run the code, you'll find out that the rate for Canada 1997 = 5.31 but I woul like it to read 1.06 (1,889,464 / 1,781,102).&lt;BR /&gt;
&lt;BR /&gt;
Is there another statistic that could help me with this? See my code below :&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
PROC TABULATE&lt;BR /&gt;
DATA=SASHELP.PRDSAL3&lt;BR /&gt;
	OUT=WORK.SALE1;&lt;BR /&gt;
&lt;BR /&gt;
	TITLE;&lt;BR /&gt;
	TITLE1 "Sale #1";&lt;BR /&gt;
&lt;BR /&gt;
	VAR predict actual;&lt;BR /&gt;
	CLASS country 		/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	CLASS year 			/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	CLASS product 		/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	TABLE &lt;BR /&gt;
&lt;BR /&gt;
/* Row Dimension */&lt;BR /&gt;
year={LABEL=''}*(product={LABEL=''} ALL={LABEL='TOTAL'}),&lt;BR /&gt;
&lt;BR /&gt;
/* Column Dimension */&lt;BR /&gt;
country*(predict*sum={label=''} actual*Sum={LABEL=''})&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/ BOX={LABEL=_PAGE_} PRINTMISS MISSTEXT='0' INDENT=0 		;&lt;BR /&gt;
	;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
DATA WORK.SALE2;&lt;BR /&gt;
	SET WORK.SALE1;&lt;BR /&gt;
&lt;BR /&gt;
Rate=predict_sum/actual_sum;&lt;BR /&gt;
&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
PROC TABULATE&lt;BR /&gt;
DATA=WORK.SALE2;&lt;BR /&gt;
&lt;BR /&gt;
	TITLE1 "Sale #3";&lt;BR /&gt;
&lt;BR /&gt;
	VAR predict_sum actual_sum rate;&lt;BR /&gt;
	CLASS country 		/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	CLASS year 			/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	CLASS product 		/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	TABLE &lt;BR /&gt;
&lt;BR /&gt;
/* Row Dimension */&lt;BR /&gt;
year={LABEL=''}*(product={LABEL=''} ALL={LABEL='TOTAL'}),&lt;BR /&gt;
&lt;BR /&gt;
/* Column Dimension */&lt;BR /&gt;
country*(predict_sum*sum={label=''} actual_sum*Sum={LABEL=''} rate*sum={LABEL=''})&lt;BR /&gt;
&lt;BR /&gt;
&lt;BR /&gt;
/ BOX={LABEL=_PAGE_} PRINTMISS MISSTEXT='0' INDENT=0 		;&lt;BR /&gt;
	;&lt;BR /&gt;
RUN;&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Edit : I'm sorry code looks funny...I took the time to format it all in SAS, but apparently the [ pre ] option does not like it...hope it is still readable.

Message was edited by: SamuelG.</description>
      <pubDate>Wed, 26 Aug 2009 20:05:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-TABULATE-Sum-statistic/m-p/67290#M19245</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-08-26T20:05:18Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TABULATE Sum statistic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-TABULATE-Sum-statistic/m-p/67291#M19246</link>
      <description>Hi:&lt;BR /&gt;
  Even though you calculate RATE in a DATA step, by the time PROC TABULATE gets RATE, there is no longer any visibility or awareness of RATE's underlying division when TABULATE builds your report #3. You ask for RATE*SUM in your report #3 and that's what it sounds like TABULATE is giving you. &lt;BR /&gt;
&lt;BR /&gt;
  I don't believe that TABULATE is doing anything wrong -- it's giving you the statistics you asked for.&lt;BR /&gt;
 &lt;BR /&gt;
  Although you can use PCTSUM with a custom denominator in TABULATE, I doubt that, in this circumstance,  it will give you the number you want -- on just the one row. You've already calculate the RATE for the other rows and there's not a good way to get only the Total row to be the result of the division. You're using 2 TABULATES and a DATA step -- essentially reading and processing the same data 3 times. You could generate your report in 1 PROC REPORT step with COUNTRY as an ACROSS variable and using a COMPUTED report item for a calculate RATE item.&lt;BR /&gt;
 &lt;BR /&gt;
  The program below shows two different versions of a PROC REPORT -- one which shows the year total at the bottom of the products (1) and another which shows the year total at the top of the products (2).&lt;BR /&gt;
 &lt;BR /&gt;
cynthia&lt;BR /&gt;
 &lt;BR /&gt;
[pre]&lt;BR /&gt;
ods html file='c:\temp\tworep.html' style=egdefault;&lt;BR /&gt;
                       &lt;BR /&gt;
proc report data=sashelp.prdsal3 nowd nocenter;&lt;BR /&gt;
   column year product country,(predict actual rate);&lt;BR /&gt;
   TITLE1 "1) Sale and Rate with PROC REPORT and Compute AFTER";&lt;BR /&gt;
    define year / group noprint;&lt;BR /&gt;
    define product / group;&lt;BR /&gt;
    define country / across;&lt;BR /&gt;
    define predict / sum f=comma15.2;&lt;BR /&gt;
    define actual / sum f=comma15.2;&lt;BR /&gt;
    define rate / computed f=7.2;&lt;BR /&gt;
    compute rate;&lt;BR /&gt;
      _c5_ = _c3_ / _c4_;&lt;BR /&gt;
      _c8_ = _c6_ / _c7_;&lt;BR /&gt;
      _c11_ = _c9_ / _c10_;&lt;BR /&gt;
    endcomp;&lt;BR /&gt;
    break after year / summarize;&lt;BR /&gt;
    compute after year;&lt;BR /&gt;
      product = put(year,4.)||' '||'Total';&lt;BR /&gt;
      line ' ';&lt;BR /&gt;
    endcomp;&lt;BR /&gt;
run;&lt;BR /&gt;
                         &lt;BR /&gt;
proc report data=sashelp.prdsal3 nowd nocenter;&lt;BR /&gt;
   column year product country,(predict actual rate);&lt;BR /&gt;
   TITLE1 "2) Sale and Rate with PROC REPORT and Compute BEFORE";&lt;BR /&gt;
    define year / group noprint;&lt;BR /&gt;
    define product / group;&lt;BR /&gt;
    define country / across;&lt;BR /&gt;
    define predict / sum f=comma15.2;&lt;BR /&gt;
    define actual / sum f=comma15.2;&lt;BR /&gt;
    define rate / computed f=7.2;&lt;BR /&gt;
    compute rate;&lt;BR /&gt;
      _c5_ = _c3_ / _c4_;&lt;BR /&gt;
      _c8_ = _c6_ / _c7_;&lt;BR /&gt;
      _c11_ = _c9_ / _c10_;&lt;BR /&gt;
    endcomp;&lt;BR /&gt;
    break before year / summarize;&lt;BR /&gt;
    compute before year;&lt;BR /&gt;
      product = put(year,4.)||' '||'Total';&lt;BR /&gt;
    endcomp;&lt;BR /&gt;
    compute after year;&lt;BR /&gt;
      line ' ' ;&lt;BR /&gt;
    endcomp;&lt;BR /&gt;
run;&lt;BR /&gt;
ods html close;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]</description>
      <pubDate>Thu, 27 Aug 2009 03:27:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-TABULATE-Sum-statistic/m-p/67291#M19246</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2009-08-27T03:27:18Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TABULATE Sum statistic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-TABULATE-Sum-statistic/m-p/67292#M19247</link>
      <description>Cynthia, &lt;BR /&gt;
&lt;BR /&gt;
Sadly, it' even more complicated than that...Maybe there's a better way to achieve what I want to do, but I went with things I was comfortable with.The reason I'm using DATA statements is that I want to create conditional variables.  &lt;BR /&gt;
&lt;BR /&gt;
Let me give you a more complete example of what I want...It's the actual code I'm using, but adapted for PRDSAL3 dataset. As you'll see, I'm creating another variable (FINAL_INDEX), which is dependant on the country. &lt;BR /&gt;
&lt;BR /&gt;
Here's the code:&lt;BR /&gt;
&lt;BR /&gt;
[pre]&lt;BR /&gt;
&lt;BR /&gt;
ODS _ALL_ close;&lt;BR /&gt;
PROC TABULATE&lt;BR /&gt;
DATA=SASHELP.PRDSAL3&lt;BR /&gt;
	OUT=WORK.Summarytable1;&lt;BR /&gt;
&lt;BR /&gt;
	TITLE;&lt;BR /&gt;
	TITLE1 "Summary table 1";&lt;BR /&gt;
&lt;BR /&gt;
	VAR predict actual;&lt;BR /&gt;
	CLASS country 		/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	CLASS year 			/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	CLASS product 		/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	TABLE &lt;BR /&gt;
&lt;BR /&gt;
/* Row Dimension */&lt;BR /&gt;
year={LABEL=''}*(product={LABEL=''}),&lt;BR /&gt;
&lt;BR /&gt;
/* Column Dimension */&lt;BR /&gt;
country*(predict*sum={label=''} actual*Sum={LABEL=''})&lt;BR /&gt;
&lt;BR /&gt;
/ BOX={LABEL=_PAGE_} PRINTMISS MISSTEXT='0' INDENT=0 		;&lt;BR /&gt;
RUN;&lt;BR /&gt;
DATA WORK.SALE2;&lt;BR /&gt;
	SET WORK.Summarytable1;&lt;BR /&gt;
&lt;BR /&gt;
	IF country = 'Canada' 	THEN DO;&lt;BR /&gt;
		IF actual_Sum=0 		THEN Rate_Can=.;&lt;BR /&gt;
		IF actual_Sum&amp;gt;0 		THEN Rate_Can=predict_sum/actual_sum;&lt;BR /&gt;
	END;&lt;BR /&gt;
		IF country = 'Mexico' 	THEN DO;&lt;BR /&gt;
		IF actual_Sum=0 		THEN Rate_Mex=.;&lt;BR /&gt;
		IF actual_Sum&amp;gt;0 		THEN Rate_Mex=predict_sum/actual_sum;&lt;BR /&gt;
	END;&lt;BR /&gt;
	IF country = 'U.S.A.' 	THEN DO;&lt;BR /&gt;
		IF actual_Sum=0 		THEN Rate_USA=.;&lt;BR /&gt;
		IF actual_Sum&amp;gt;0 		THEN Rate_USA=predict_sum/actual_sum;&lt;BR /&gt;
	END;&lt;BR /&gt;
RUN;&lt;BR /&gt;
PROC TABULATE&lt;BR /&gt;
DATA=WORK.SALE2&lt;BR /&gt;
	OUT=WORK.Summarytable2;&lt;BR /&gt;
	&lt;BR /&gt;
	TITLE1 "Summary table 2";&lt;BR /&gt;
&lt;BR /&gt;
	VAR predict_sum actual_sum rate_Can Rate_Mex Rate_USA;&lt;BR /&gt;
	CLASS country 		/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	CLASS year 			/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	CLASS product 		/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
	TABLE &lt;BR /&gt;
&lt;BR /&gt;
/* Row Dimension */&lt;BR /&gt;
year={LABEL=''}*(product={LABEL=''}),&lt;BR /&gt;
&lt;BR /&gt;
/* Column Dimension */&lt;BR /&gt;
country*&lt;BR /&gt;
(predict_sum*sum={label=''} &lt;BR /&gt;
actual_sum*Sum={LABEL=''}&lt;BR /&gt;
rate_Can*sum={LABEL=''}&lt;BR /&gt;
rate_mex*sum={LABEL=''}&lt;BR /&gt;
rate_USA*sum={LABEL=''})&lt;BR /&gt;
&lt;BR /&gt;
/ BOX={LABEL=_PAGE_} PRINTMISS MISSTEXT='0' INDENT=0 		;&lt;BR /&gt;
RUN;&lt;BR /&gt;
DATA WORK.SALE4;&lt;BR /&gt;
	SET WORK.Summarytable2;&lt;BR /&gt;
&lt;BR /&gt;
IF country='Canada' THEN&lt;BR /&gt;
key = year!!product;&lt;BR /&gt;
&lt;BR /&gt;
/*****    INDEX    *****/&lt;BR /&gt;
&lt;BR /&gt;
INDEX_CAN = Rate_Can_Sum;&lt;BR /&gt;
RUN;&lt;BR /&gt;
Proc Sort Data=WORK.SALE4;&lt;BR /&gt;
by key;&lt;BR /&gt;
RUN;&lt;BR /&gt;
DATA WORK.SALE5;&lt;BR /&gt;
	SET WORK.Summarytable2;&lt;BR /&gt;
key = year!!product;&lt;BR /&gt;
/*****    CALCULATION OF RATE    *****/&lt;BR /&gt;
FINAL_RATE=Sum(Rate_can_Sum, Rate_Mex_Sum, Rate_USA_Sum);&lt;BR /&gt;
RUN;&lt;BR /&gt;
PROC SORT Data=WORK.SALE5;&lt;BR /&gt;
by key;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
DATA WORK.SALE6;&lt;BR /&gt;
MERGE 	WORK.SALE4 (IN=A)&lt;BR /&gt;
		WORK.SALE5 (IN=B);&lt;BR /&gt;
BY key;&lt;BR /&gt;
IF B;&lt;BR /&gt;
RUN;&lt;BR /&gt;
DATA WORK.SALE7;&lt;BR /&gt;
	SET WORK.SALE6;&lt;BR /&gt;
IF INDEX_CAN = 0 		THEN FINAL_INDEX=.;&lt;BR /&gt;
ELSE IF INDEX_CAN &amp;gt;0 	THEN DO;&lt;BR /&gt;
IF FINAL_RATE=&amp;gt;0		THEN FINAL_INDEX=FINAL_RATE/INDEX_CAN; &lt;BR /&gt;
END;&lt;BR /&gt;
RUN;&lt;BR /&gt;
ODS html file='c:\temp\thisismyfinaltable.html' style=egdefault; &lt;BR /&gt;
PROC TABULATE &lt;BR /&gt;
DATA = WORK.SALE7;&lt;BR /&gt;
&lt;BR /&gt;
TITLE1 "FINAL Summary Table";&lt;BR /&gt;
&lt;BR /&gt;
VAR predict_sum_sum actual_sum_sum FINAL_RATE FINAL_INDEX;&lt;BR /&gt;
&lt;BR /&gt;
CLASS country 		/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
CLASS year 			/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
CLASS product 		/	ORDER=UNFORMATTED MISSING;&lt;BR /&gt;
&lt;BR /&gt;
TABLE &lt;BR /&gt;
&lt;BR /&gt;
/* Row Dimension */&lt;BR /&gt;
year={LABEL=''}*(product={LABEL=''} ALL={LABEL='TOTAL'}),&lt;BR /&gt;
&lt;BR /&gt;
/* Column Dimension */&lt;BR /&gt;
country*&lt;BR /&gt;
(predict_sum_sum*sum={label=''} &lt;BR /&gt;
actual_sum_sum*Sum={LABEL=''}&lt;BR /&gt;
FINAL_RATE*sum={LABEL=''}&lt;BR /&gt;
final_index*sum={LABEL=''})&lt;BR /&gt;
&lt;BR /&gt;
/ BOX={LABEL=_PAGE_} PRINTMISS MISSTEXT='0' INDENT=0 		;&lt;BR /&gt;
RUN;&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Thank you for your help &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;</description>
      <pubDate>Thu, 27 Aug 2009 12:58:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-TABULATE-Sum-statistic/m-p/67292#M19247</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-08-27T12:58:39Z</dc:date>
    </item>
    <item>
      <title>Re: PROC TABULATE Sum statistic</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/PROC-TABULATE-Sum-statistic/m-p/67293#M19248</link>
      <description>SamuelG&lt;BR /&gt;
&lt;BR /&gt;
If I understand what you are attempting, I would be very tempted to summarize the data using proc summary with the class variables to get the sums for each group and then run the resulting data set through a data step to calculate the rates. Then the _type_ variable can be selected and/or formatted to show the various groups you need. The country/year/product combinations that don't actually have any data to sum could be handled in Tabulate with a preloadfmt option to display blank rows or columns as needed.&lt;BR /&gt;
&lt;BR /&gt;
proc summary data=sashelp.prdsal3;&lt;BR /&gt;
   class country year product;&lt;BR /&gt;
   var predict actual;&lt;BR /&gt;
   output out=prodsum sum=;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
data prodrate;&lt;BR /&gt;
   set prodsum;&lt;BR /&gt;
   rate=predict/actual;&lt;BR /&gt;
   if _type_=6 and product='' then product='All'; /* add a useful description to display below*/&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
/* this is using tabulate just arrange precalculated values*/&lt;BR /&gt;
/* shows year and individual products or all products */&lt;BR /&gt;
proc tabulate data=prodrate;&lt;BR /&gt;
   where _type_ in (6,7);&lt;BR /&gt;
   class country year product/order=unformatted missing;&lt;BR /&gt;
   var predict actual rate;&lt;BR /&gt;
   table year=' '*product=' ',&lt;BR /&gt;
         country*max=' '*(predict actual rate)&lt;BR /&gt;
         /printmiss misstext='0';&lt;BR /&gt;
run;</description>
      <pubDate>Fri, 28 Aug 2009 21:54:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/PROC-TABULATE-Sum-statistic/m-p/67293#M19248</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2009-08-28T21:54:30Z</dc:date>
    </item>
  </channel>
</rss>

