<?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: Create cross table by PROC REPORT in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/642426#M191639</link>
    <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Here's an simpler example than the one in the paper:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1587688857525.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38620iE0B6F14ADE45B596/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1587688857525.png" alt="Cynthia_sas_0-1587688857525.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
    <pubDate>Fri, 24 Apr 2020 00:41:15 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2020-04-24T00:41:15Z</dc:date>
    <item>
      <title>Create cross table by PROC REPORT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/642405#M191629</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am new to formatting in SAS and I am struggling to generate some cross tables using PROC REPORT and macro.&amp;nbsp;&lt;/P&gt;&lt;P&gt;The data looks like this:&lt;/P&gt;&lt;TABLE border="0" cellspacing="0" cellpadding="0"&gt;&lt;TBODY&gt;&lt;TR&gt;&lt;TD&gt;ID&lt;/TD&gt;&lt;TD&gt;var1&lt;/TD&gt;&lt;T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&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;n (%)&lt;/P&gt;&lt;/TD&gt;&lt;TD&gt;&lt;P&gt;N&lt;/P&gt;&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I tried to mimic some code as follow but can not get the percentage in the parentheses, and can not get the percentage by row (a%+b%=100%; c%+d%=100%):&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%MACRO report2(dataset, var1, var2);
data mydat;
	set &amp;amp;dataset;
	keep id &amp;amp;var1 &amp;amp;var2;
run;

ods excel options(sheet_name="&amp;amp;var1.|&amp;amp;var2.");

proc report data = mydat missing nowd;
 column &amp;amp;var2 &amp;amp;var1, (n pctn) ('Total' n);
 define &amp;amp;var2 /group "&amp;amp;var2";
 define &amp;amp;var1 /across order = internal "&amp;amp;var1";
 define n/format =8. "N";
 define pctn / "%" format = percent8.1;
 rbreak after / summarize;
 compute after;
 	&amp;amp;var2 = "Total";
 endcomp;
run; 

%MEND report2;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;There are some trickier problem, but I am starting with this simpler one. Hope someone can help. Thanks a lot!&lt;/P&gt;</description>
      <pubDate>Thu, 30 Apr 2020 22:33:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/642405#M191629</guid>
      <dc:creator>xeonchenzi</dc:creator>
      <dc:date>2020-04-30T22:33:00Z</dc:date>
    </item>
    <item>
      <title>Re: Create cross table by PROC REPORT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/642408#M191631</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I do not see how the data you show can generate the report that you want. PROC REPORT is able to produce this type of demographic report. I have 3 examples of a report like this in my 2008 paper on Creating Complex Reports. Look at this previous forum posting &lt;A href="https://communities.sas.com/t5/ODS-and-Base-Reporting/Proc-Report-Create-a-custom-table/td-p/486398" target="_blank"&gt;https://communities.sas.com/t5/ODS-and-Base-Reporting/Proc-Report-Create-a-custom-table/td-p/486398&lt;/A&gt; for the paper links.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Typically with this kind of demographic report, you get each statistic, such as N and PCTN or MIN-MAX in separate cells and then you create a character string that does things like adding parentheses or punctuation to make the string you want to show on your report.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Thu, 23 Apr 2020 21:30:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/642408#M191631</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-04-23T21:30:31Z</dc:date>
    </item>
    <item>
      <title>Re: Create cross table by PROC REPORT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/642411#M191633</link>
      <description>&lt;P&gt;Thanks for your reply. I have updated the report table, now it directly linked to the data.&lt;/P&gt;&lt;P&gt;Thanks for the paper too, I think I read your paper before and got a bit lost. I am going to look into it again.&lt;/P&gt;</description>
      <pubDate>Thu, 23 Apr 2020 21:46:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/642411#M191633</guid>
      <dc:creator>xeonchenzi</dc:creator>
      <dc:date>2020-04-23T21:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: Create cross table by PROC REPORT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/642426#M191639</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Here's an simpler example than the one in the paper:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1587688857525.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38620iE0B6F14ADE45B596/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1587688857525.png" alt="Cynthia_sas_0-1587688857525.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Fri, 24 Apr 2020 00:41:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/642426#M191639</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-04-24T00:41:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create cross table by PROC REPORT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/644401#M192464</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;Cynthia, I find the compute statement you showed really helps putting the calculated percentage in the &lt;SPAN&gt;parentheses&lt;/SPAN&gt;.&amp;nbsp;&lt;/P&gt;&lt;P&gt;But I am still struggling with how to&amp;nbsp;get the percentage by row &lt;SPAN&gt;(a%+b%=100% in the example of my post&lt;/SPAN&gt;), I can only get the&amp;nbsp;percentage by column using the code I have above. Could you please help me with that?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Thu, 30 Apr 2020 22:16:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/644401#M192464</guid>
      <dc:creator>xeonchenzi</dc:creator>
      <dc:date>2020-04-30T22:16:54Z</dc:date>
    </item>
    <item>
      <title>Re: Create cross table by PROC REPORT</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/644623#M192568</link>
      <description>&lt;P&gt;Hi:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; I assume you want something like THIS:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_0-1588368934182.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38980iAD996222DC5A6120/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_0-1588368934182.png" alt="Cynthia_sas_0-1588368934182.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Where the number in each cell is the percent of the ROW total, not the percent of the Grand Total. PROC TABULATE has special percent keywords for this (ROWPCTN and ROWPCTSUM, along with COLPCTN and COLPCTSUM) but PROC REPORT does not have those keywords available.&lt;/P&gt;
&lt;P&gt;Instead, if you want to calculate a ROW percent (so that each row adds up to 100% than you need to calculate it yourself. I used an ACROSS item to show a separate column for F vs M.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; The COMPUTE block in this case, is more complex, since I could not use the simple PCTN for the calculation. But first, I had to make some fake data since I only wanted to show 2 rows plus the total row on the report:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data fakedata;
  length grpvar $10;
  set sashelp.class;
  if age le 13 then grpvar = 'Group 1';
  else grpvar = 'Group 2';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then I had to create some calculated variables to hold the totals, but, due to the use of ACROSS items, the variables needed to be referred to as their absolute column numbers:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Cynthia_sas_1-1588371410893.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/38987i6F56947FF953E2CA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Cynthia_sas_1-1588371410893.png" alt="Cynthia_sas_1-1588371410893.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;So when you see references to _c3_, _c4_ and _c5_, etc, those are needed any time you use an ACROSS item and you need to calculate a value that is also nested under an ACROSS item. In this case, the SEX variable for F and M was defined as an ACROSS item. That means the STOT value and the PCT value for F needs to be distinguished from the STOT and the PCT value for M in order to calculate a separate CALCCOL value for each unique group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; Here's the PROC REPORT code to generate the above report and the report that I initially showed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
proc report data=fakedata;
title '3a) Only showing the calculated column and total';
  column ('Group' grpvar) n=rtot sex,(n=stot calcpct calccol) ('Total' totcol);
  define grpvar / group 'Grpvar';
  define rtot / 'rtot'  /* noprint */;
  define sex / across "With computed % and Row %";
  define stot / 'stot' /* noprint */;
  define calcpct / computed 'Pct' f=percent7.1 /* noprint */;
  define calccol / computed 'CALCCOL' 
         style(column)={just=r};
  define totcol / computed 'TOTCOL'
         style(column)={just=r};
  compute calcpct;
     _c4_ = divide(_c3_ , rtot);
	 _c7_ = divide(_c6_ , rtot);
  endcomp;
  compute calccol / character length = 15;
     if _c3_ gt . then
        _c5_ = put(_c3_,2.0)||' ('||right(put(_c4_,percent7.1))||')';
	 if _c6_ gt . then
        _c8_ = put(_c6_,2.0)||' ('||right(put(_c7_,percent7.1))||')';
  endcomp;
  compute totcol / character length = 15;
    tmp = divide(rtot, rtot);
	totcol = put(rtot,2.0)||' ('||right(put(tmp,percent7.1))||')';
  endcomp;
  rbreak after / summarize;
  compute after;
    grpvar = 'Total';
  endcomp;
run;


proc report data=fakedata;
title '3b) Only showing the calculated column and total';
  column ('Group' grpvar) n=rtot sex,(n=stot calcpct calccol) ('Total' totcol);
  define grpvar / group ' ';
  define rtot / 'rtot'  noprint ;
  define sex / across "With computed % and Row %";
  define stot / 'stot'   noprint  ;
  define calcpct / computed 'Pct' f=percent7.1   noprint  ;
  define calccol / computed 'CALCCOL' 
         style(column)={just=r};
  define totcol / computed ' '
         style(column)={just=r};
  compute calcpct;
     _c4_ = divide(_c3_ , rtot);
	 _c7_ = divide(_c6_ , rtot);
  endcomp;
  compute calccol / character length = 15;
     if _c3_ gt . then
        _c5_ = put(_c3_,2.0)||' ('||right(put(_c4_,percent7.1))||')';
	 if _c6_ gt . then
        _c8_ = put(_c6_,2.0)||' ('||right(put(_c7_,percent7.1))||')';
  endcomp;
  compute totcol / character length = 15;
    tmp = divide(rtot, rtot);
	totcol = put(rtot,2.0)||' ('||right(put(tmp,percent7.1))||')';
  endcomp;
  rbreak after / summarize;
  compute after;
    grpvar = 'Total';
  endcomp;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps,&lt;/P&gt;
&lt;P&gt;Cynthia&lt;/P&gt;</description>
      <pubDate>Fri, 01 May 2020 22:25:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Create-cross-table-by-PROC-REPORT/m-p/644623#M192568</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2020-05-01T22:25:34Z</dc:date>
    </item>
  </channel>
</rss>

