BookmarkSubscribeRSS Feed
nsns
Quartz | Level 8

Hello,

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.  The reason why I need it in the middle is because the order of tables needs to remain.

Thanks in advance.

1 REPLY 1
Patrick
Opal | Level 21

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.

Below some sample code to illustrate the approach.

%macro demo(type,name);
  %if &type=1 %then
    %do;
      title "Table type 1 for &name"; 
      proc tabulate data=sashelp.class;
        where name = "&name";
        class sex;
        keylabel n=' ';
        table sex;
      run;
      title;
    %end;
  %else
  %if &type=2 %then
    %do;
      title "Table type 2 for &name";
      proc tabulate data=sashelp.class;
        where name = "&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;

Patrick_0-1739921067207.png

 

sas-innovate-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 590 views
  • 0 likes
  • 2 in conversation