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!

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 3134 views
  • 1 like
  • 2 in conversation