<?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: Conditioning within Proc Tabulate in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditioning-within-Proc-Tabulate/m-p/959555#M374364</link>
    <description>&lt;P&gt;If I understand your problem correctly then no, you can't have conditional table statements within Proc Tabulate. What you can do is call Proc Tabulate multiple times and you can do this data driven.&lt;/P&gt;
&lt;P&gt;Below some sample code to illustrate the approach.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro demo(type,name);
  %if &amp;amp;type=1 %then
    %do;
      title "Table type 1 for &amp;amp;name"; 
      proc tabulate data=sashelp.class;
        where name = "&amp;amp;name";
        class sex;
        keylabel n=' ';
        table sex;
      run;
      title;
    %end;
  %else
  %if &amp;amp;type=2 %then
    %do;
      title "Table type 2 for &amp;amp;name";
      proc tabulate data=sashelp.class;
        where name = "&amp;amp;name";
        class age;
        keylabel n=' ';
        table age;
      run;
      title;
    %end;
%mend;
%demo(James);

proc format;
  value $type
    'Alfred'
    ,'Joyce'
    ,'Judy'
    ,'Louise'
    ,'Henry'
    ,'James'
    ,'Jane'
    ,'Janet'= '1'
    'Jeffrey'
    ,'Alice'
    ,'Barbara'
    ,'Carol'   
    ,'John'
    ,'Mary'
    ,'Philip'
    ,'Robert'
    ,'Ronald'
    ,'Thomas'
    ,'William'='2'
    other = 'na'
    ;
run;

data _null_;
  set sashelp.class;
  by name;
  if first.name then
    do;
      cmd=cats('%demo(',put(name,$type.),',',name,')');
      call execute(cmd);
    end;
run;&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-1739921067207.png" style="width: 127px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104738i5182979B2E70ECE7/image-dimensions/127x247?v=v2" width="127" height="247" role="button" title="Patrick_0-1739921067207.png" alt="Patrick_0-1739921067207.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 18 Feb 2025 23:24:44 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2025-02-18T23:24:44Z</dc:date>
    <item>
      <title>Conditioning within Proc Tabulate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditioning-within-Proc-Tabulate/m-p/959507#M374358</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;Is there anyway that I can have a condition within a proc tabulate such that if for example when ord equals 1,3,4,5 (summarized continuous data - n mean std) then I have class, var and table statements and when ord equals 2,6,and 7 (summarized categorical data - counts and percents) I use different class, var and table statements.&amp;nbsp; The reason why I need it in the middle is because the order of tables needs to remain.&lt;/P&gt;
&lt;P&gt;Thanks in advance.&lt;/P&gt;</description>
      <pubDate>Tue, 18 Feb 2025 17:05:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditioning-within-Proc-Tabulate/m-p/959507#M374358</guid>
      <dc:creator>nsns</dc:creator>
      <dc:date>2025-02-18T17:05:18Z</dc:date>
    </item>
    <item>
      <title>Re: Conditioning within Proc Tabulate</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditioning-within-Proc-Tabulate/m-p/959555#M374364</link>
      <description>&lt;P&gt;If I understand your problem correctly then no, you can't have conditional table statements within Proc Tabulate. What you can do is call Proc Tabulate multiple times and you can do this data driven.&lt;/P&gt;
&lt;P&gt;Below some sample code to illustrate the approach.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro demo(type,name);
  %if &amp;amp;type=1 %then
    %do;
      title "Table type 1 for &amp;amp;name"; 
      proc tabulate data=sashelp.class;
        where name = "&amp;amp;name";
        class sex;
        keylabel n=' ';
        table sex;
      run;
      title;
    %end;
  %else
  %if &amp;amp;type=2 %then
    %do;
      title "Table type 2 for &amp;amp;name";
      proc tabulate data=sashelp.class;
        where name = "&amp;amp;name";
        class age;
        keylabel n=' ';
        table age;
      run;
      title;
    %end;
%mend;
%demo(James);

proc format;
  value $type
    'Alfred'
    ,'Joyce'
    ,'Judy'
    ,'Louise'
    ,'Henry'
    ,'James'
    ,'Jane'
    ,'Janet'= '1'
    'Jeffrey'
    ,'Alice'
    ,'Barbara'
    ,'Carol'   
    ,'John'
    ,'Mary'
    ,'Philip'
    ,'Robert'
    ,'Ronald'
    ,'Thomas'
    ,'William'='2'
    other = 'na'
    ;
run;

data _null_;
  set sashelp.class;
  by name;
  if first.name then
    do;
      cmd=cats('%demo(',put(name,$type.),',',name,')');
      call execute(cmd);
    end;
run;&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-1739921067207.png" style="width: 127px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/104738i5182979B2E70ECE7/image-dimensions/127x247?v=v2" width="127" height="247" role="button" title="Patrick_0-1739921067207.png" alt="Patrick_0-1739921067207.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 18 Feb 2025 23:24:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditioning-within-Proc-Tabulate/m-p/959555#M374364</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2025-02-18T23:24:44Z</dc:date>
    </item>
  </channel>
</rss>

