Adding conditions within proc tabulate

Reply
Contributor
Posts: 22

Adding conditions within proc tabulate

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;

Super User
Posts: 11,134

Re: Adding conditions within proc tabulate

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*/

Contributor
Posts: 22

Re: Adding conditions within proc tabulate

Perfect! Thank you!

Ask a Question
Discussion stats
  • 2 replies
  • 233 views
  • 0 likes
  • 2 in conversation