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;
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*/
Perfect! Thank you!
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.
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.