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!
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.