<?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 How can I merge multiple tables from PROC TABULATE? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908162#M358430</link>
    <description>&lt;P&gt;Dear SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am asked to create a table with this formatting:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="table.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91438i0CDAA90E989450F2/image-size/large?v=v2&amp;amp;px=999" role="button" title="table.png" alt="table.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How should I modify my code to merge the four tables?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc tabulate data=sashelp.class out=temp;
  	var age height weight;
  	class sex;
  	table height='height: mean (std)', (sex all)*(mean='' std='');
	table height='height: min-max', (sex all)*(min='' max='');
	table weight='weight: mean (std)', (sex all)*(mean='' std='');
	table weight='weight: min-max', (sex all)*(min='' max='');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For simplicity, the code for the brackets around std and the dash between min and max is omitted. The actual application involves multiple tables with many variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 15 Dec 2023 10:07:53 GMT</pubDate>
    <dc:creator>arra</dc:creator>
    <dc:date>2023-12-15T10:07:53Z</dc:date>
    <item>
      <title>How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908162#M358430</link>
      <description>&lt;P&gt;Dear SAS community,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am asked to create a table with this formatting:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="table.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91438i0CDAA90E989450F2/image-size/large?v=v2&amp;amp;px=999" role="button" title="table.png" alt="table.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;How should I modify my code to merge the four tables?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=""&gt;proc tabulate data=sashelp.class out=temp;
  	var age height weight;
  	class sex;
  	table height='height: mean (std)', (sex all)*(mean='' std='');
	table height='height: min-max', (sex all)*(min='' max='');
	table weight='weight: mean (std)', (sex all)*(mean='' std='');
	table weight='weight: min-max', (sex all)*(min='' max='');
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For simplicity, the code for the brackets around std and the dash between min and max is omitted. The actual application involves multiple tables with many variables.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 10:07:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908162#M358430</guid>
      <dc:creator>arra</dc:creator>
      <dc:date>2023-12-15T10:07:53Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908181#M358442</link>
      <description>&lt;P&gt;Your desired table has columns with different statistics across rows (mean then min in one column, std then max in another column).&amp;nbsp; &amp;nbsp;PROC TABULATE can't do this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I think it's doable in PROC REPORT, for which others can likely provide guidance.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 13:33:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908181#M358442</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2023-12-15T13:33:34Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908188#M358443</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/460729"&gt;@arra&lt;/a&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Welcome to the SAS Communities.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Whenever you ask coding questions then it's really appreciated if you also provide some representative sample data together with the desired result. It's also really helpful if you provide the code you've tried already whether it's fully working or not - as you did &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Representative sample data is what allows the ones responding to actually test the proposed code. Such data also often removes a lot of ambiguity in the description of the problem.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Please provide the sample data via a tested SAS data step with a datalines/card statement that creates a SAS table. This allows people to spend the time answering your question instead of preparing sample data.&lt;/P&gt;
&lt;P&gt;Asking a question this way takes a bit more time but I promise you that you will get all the time back by getting a solution faster.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks,&lt;/P&gt;
&lt;P&gt;Patrick&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 14:13:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908188#M358443</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-15T14:13:31Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908191#M358445</link>
      <description>This is why I used the built-in sample data (data=sashelp.class). Or do you mean something else?</description>
      <pubDate>Fri, 15 Dec 2023 14:20:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908191#M358445</guid>
      <dc:creator>arra</dc:creator>
      <dc:date>2023-12-15T14:20:31Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908194#M358446</link>
      <description>&lt;P&gt;Do you have a picture in mind for the final result?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here is a possible (untested) approach for you to consider:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc tabulate data=sashelp.class out=temp;
  	var age height weight;
  	class sex;
  	table height='height: mean (std)'*(mean=' ' std=' ')
	      height='height: min-max'*(min=' ' max=' ')
	      weight='weight: mean (std)'*(mean=' ' std=' ')
 	      weight='weight: min-max'*(min=' ' max=' ')
          ,
           (sex all);
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 15 Dec 2023 14:33:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908194#M358446</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2023-12-15T14:33:18Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908195#M358447</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/460729"&gt;@arra&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;This is why I used the built-in sample data (data=sashelp.class). Or do you mean something else?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Oh, missed that! You're too perfect!&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 15:14:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908195#M358447</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-15T15:14:31Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908197#M358448</link>
      <description>Thanks. Sure, my post includes an image of the required result. (The proposed solution does not put different summary statistics in the same row.)</description>
      <pubDate>Fri, 15 Dec 2023 14:39:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908197#M358448</guid>
      <dc:creator>arra</dc:creator>
      <dc:date>2023-12-15T14:39:25Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908217#M358452</link>
      <description>&lt;P&gt;I believe using Proc Tabulate below are your two options - which aren't exactly what you had in mind.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;title 'Option 1';
proc tabulate data=sashelp.class out=temp;
  var age height weight;
  class sex;
  table (height='height: mean (std)' weight='weight: mean (std)'), 
        (sex all)*(mean=' ' std=' ');
  table (height='height: min-max' weight='weight: min-max'), 
        (sex all)*(mean=' ' std=' ');
run;

title 'Option 2';
proc tabulate data=sashelp.class out=temp;
  var age height weight;
  class sex;
  table (height weight)*(mean std min max), 
        (sex all);
run;
title;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Patrick_0-1702653702005.png" style="width: 324px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91448iB47C62E87BA76E4D/image-dimensions/324x476?v=v2" width="324" height="476" role="button" title="Patrick_0-1702653702005.png" alt="Patrick_0-1702653702005.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I'm not sure if Proc Report would allow for the layout you have in mind.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;....and if you really need full flexibility then there is the&amp;nbsp;Report Writing Interface - but that's a whole different ball game and the code&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;shared in &lt;A href="https://communities.sas.com/t5/SAS-Programming/How-to-create-report-with-varying-column-and-row-numbers-using/m-p/907985#M358379" target="_self"&gt;this thread&lt;/A&gt; should give you the idea what it entails.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 15:25:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908217#M358452</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-12-15T15:25:25Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908219#M358453</link>
      <description>Thanks. Option 2 is what I currently have. I will try to get it through.</description>
      <pubDate>Fri, 15 Dec 2023 15:39:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908219#M358453</guid>
      <dc:creator>arra</dc:creator>
      <dc:date>2023-12-15T15:39:43Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908265#M358459</link>
      <description>&lt;P&gt;For creating custom format tables, which includes inconsistent values per column,&amp;nbsp; data is usually manipulated in data step.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;In this specific case I would do the following steps (there might be more efficient ways to do it):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;*step 1; 
proc means data=sashelp.class nway;
	ways 1; 
	class sex;
	var height weight;
	output out=means1; 
run; 
proc means data=sashelp.class;
	var height weight;
	output out=means2; 
run; 

*step 2; 
proc transpose data=means1 out=wide1;
	by sex;
	var height weight;
	id _stat_;
proc print;run; 
proc transpose data=means2 out=wide2;
	by _type_;
	var height weight;
	id _stat_;
proc print;run; 

*step 3; 
data wide1a;
	set wide1;
	length M_STD MIN_MAX $15; 
	M_STD= strip(put(mean, 6.2))||' ('||strip(put(std, 6.2))||')';
	MIN_MAX= strip(put(min, best.))||' - '||strip(put(max, best.));
proc print; run; 
proc sort data= wide1a; by _name_; run; 
proc transpose data=wide1a out=narrow1;
	by _name_ ;
	var m_std min_max;
	id sex; 
proc print; run;  

data wide2a;
	set wide2; 
	length M_STD MIN_MAX $15; 
	M_STD= strip(put(mean, 6.2))||' ('||strip(put(std, 6.2))||')';
	MIN_MAX= strip(put(min, best.))||' - '||strip(put(max, best.));
proc print; run; 
proc transpose data=wide2a out=narrow2 (rename=(col1=TOTAL));
	by _name_ ;
	var m_std min_max;
proc print; run;  

*step 4; 
data final;
	merge narrow1 narrow2;
	by _name_; 
	if find(f, '(') gt 0 then stat= 'mean (std)';
	else do;
		stat= 'min - max';
		_name_='';
	end; 
proc print; run; 

proc report data= final split='~';
	column _name_ stat('Sex' f m) ('Both~' total);
	define _name_/'' display;
	define stat/'' display;
	define f/ 'F' display; 
	define m/'M' display; 
	define total/'' display; 
run; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;result;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Capture.PNG" style="width: 418px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/91480i54CC8B5D55182367/image-size/large?v=v2&amp;amp;px=999" role="button" title="Capture.PNG" alt="Capture.PNG" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Dec 2023 17:13:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908265#M358459</guid>
      <dc:creator>A_Kh</dc:creator>
      <dc:date>2023-12-15T17:13:05Z</dc:date>
    </item>
    <item>
      <title>Re: How can I merge multiple tables from PROC TABULATE?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908559#M358535</link>
      <description>Many thanks for this solution! This is much more efficient than manual formatting! (I now see of course that my question should not have mentioned PROC TABULATE.)</description>
      <pubDate>Mon, 18 Dec 2023 08:19:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-can-I-merge-multiple-tables-from-PROC-TABULATE/m-p/908559#M358535</guid>
      <dc:creator>arra</dc:creator>
      <dc:date>2023-12-18T08:19:12Z</dc:date>
    </item>
  </channel>
</rss>

