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

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 491 views
  • 0 likes
  • 2 in conversation