Dear SAS experts
I would like to create a table where the title is defined depending on the macro parameter specified.
Below is a simple example of scenario. If "M" is specified I would like a title specific for "M",
and the same for "F". I cannot get it to work, but I suspect I need to somehow use %if and %else in the macro.
Can someone help with the code? Thanks.
data mydata;
set sashelp.class;
run;
*Creating tables;
%macro table(Sex=);
proc means data=mydata;
var Age;
where Sex="&Sex.";
%if &Sex.="M" %then %do;
Title "Table of: &Sex. (M text)";
%else %do;
Title "Table of: &Sex. (F text)";
%end;
run;
%mend;
%table(Sex=M);
%table(Sex=F);
You have left out an %end; statement.
%macro table(Sex=);
proc means data=mydata;
var Age;
where Sex="&Sex.";
%if &Sex.="M" %then %do;
Title "Table of: &Sex. (M text)";
%end; /* This was left out and causes the problems */
%else %do;
Title "Table of: &Sex. (F text)";
%end;
run;
%mend;
%table(Sex=M)
%table(Sex=F)
If you indent your code properly, as I have done, these types of errors become more obvious.
You don't really need a macro for this, simply a macro variable.
%let sex=M;
proc means data=sashelp.class;
var Age;
where Sex="&sex";
Title "Table of: &sex";
run;
You have left out an %end; statement.
%macro table(Sex=);
proc means data=mydata;
var Age;
where Sex="&Sex.";
%if &Sex.="M" %then %do;
Title "Table of: &Sex. (M text)";
%end; /* This was left out and causes the problems */
%else %do;
Title "Table of: &Sex. (F text)";
%end;
run;
%mend;
%table(Sex=M)
%table(Sex=F)
If you indent your code properly, as I have done, these types of errors become more obvious.
You don't really need a macro for this, simply a macro variable.
%let sex=M;
proc means data=sashelp.class;
var Age;
where Sex="&sex";
Title "Table of: &sex";
run;
Dear PaigeMiller
Thanks.
I was not aware of the fact that I also needed a "%end" there. So, what was lacking was because of ignorance.
Regarding your second point, I want to include some additional text and not just the "&Sex." part. In the actual data what I want to specificy is that the statistics I am summarizing are based on different age group restrictions depending on the (disease) subgroup of the population. Therefore, I would like to be able to some how write this out.
@mgrasmussen wrote:
Regarding your second point, I want to include some additional text and not just the "&Sex." part. In the actual data what I want to specificy is that the statistics I am summarizing are based on different age group restrictions depending on the (disease) subgroup of the population. Therefore, I would like to be able to some how write this out.
Be specific. Show us an actual example (or even a made up example) of what you are talking about here. Don't start us off with a problem that isn't really what you are trying to do, because obviously solving that original problem hasn't helped you.
@mgrasmussen wrote:
I was not aware of the fact that I also needed a "%end" there. So, what was lacking was because of ignorance.
Every %DO has to have an associated %END;
All of which brings up the interesting, and extremely important question: why would you use a macro at all here? Why don't you just use a BY statement in PROC MEANS? This is probably the easiest way to do this. Splitting data sets up using macros or macro variables is usually a poor choice when BY processing is possible.
proc sort data=sashelp.class out=class;
by sex;
run;
options nobyline;
ods noproctitle;
title "Table of #byval1";
proc means data=class;
var age;
by sex;
run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.