BookmarkSubscribeRSS Feed
peter_sjogarde
Fluorite | Level 6

Hi,

I am wondering if it is possible to add conditions within proc tabulate. I would like to be able to set a variable in the beginning of the script that includes or excludes a variable from proc tabulate.

E.g. Is it be possible to set a condition for the calculation of test2 below. If a variable is set to ‘yes’ it will be calculated but it is set to ‘no’ it will not.

Any help would be appreciated!

/Peter

TITLE1 "Test";

PROC TABULATE

DATA=WORK.T_PUBL_CIT;

  WHERE( unit = "&Unit");

  VAR test1 test2;

  CLASS type/ ORDER=UNFORMATTED MISSING;

  TABLE /* Row Dimension */

type

ALL={LABEL="Total"},

/* Column Dimension */

test1={LABEL="test1"}*

  Mean={LABEL="" STYLE={JUST=RIGHT VJUST=BOTTOM NOBREAKSPACE=ON}}*F=PERCENTN6.

test2={LABEL="test2" STYLE={JUST=RIGHT VJUST=BOTTOM}}*

  Mean={LABEL="" STYLE={JUST=RIGHT VJUST=BOTTOM NOBREAKSPACE=ON}}*F=6.1

;;run;

2 REPLIES 2
ballardw
Super User

Basic approach would be macro code. I

%macro includeflag (flag = YES); /* this defaults to including the variable*/

"Test";

PROC TABULATE

DATA=WORK.T_PUBL_CIT;

  WHERE( unit = "&Unit");

  VAR test1 test2;

  CLASS type/ ORDER=UNFORMATTED MISSING;

  TABLE /* Row Dimension */

type

ALL={LABEL="Total"},

/* Column Dimension */

test1={LABEL="test1"}*

  Mean={LABEL="" STYLE={JUST=RIGHT VJUST=BOTTOM NOBREAKSPACE=ON}}*F=PERCENTN6.

%if &flag = YES %then %do;

test2={LABEL="test2" STYLE={JUST=RIGHT VJUST=BOTTOM}}*

  Mean={LABEL="" STYLE={JUST=RIGHT VJUST=BOTTOM NOBREAKSPACE=ON}}*F=6.1

%end;

;;run;

%mend;

%includeflag(Flag=NO); /* actually any value other than YES will skip test2*/

peter_sjogarde
Fluorite | Level 6

Perfect! Thank you!

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
  • 2 replies
  • 4205 views
  • 1 like
  • 2 in conversation