Hello
I want to ask a general question.
Many times I am asked to create a summary report for different populations.
In order to do it efficiency I am using macro.
In the Macro I define a parameter that define the population.
The problem is that sometimes I need that there will not be any rows screen.
The code here just show you what i want but the way it is written is wrong
for example:
Data tbl;
input ID X $ Z;
cards;
1 a 2
2 a 2
3 a 1
4 b 1
5 b 2
6 c 2
7 c 1
8 c 1
9 c 2
10 c 2
;
run;
%macro mmacro(condition);
PROC SQL;
create table xxx as
select
from tbl
where &condition.
;
QUIT;
%mend;
/*Run1*/
%mmacro(condition=(x='a' and z='1');
/*Run2*/
%mmacro(condition=(x='a' and z='2');
/*Run3*/
%mmacro(condition=((x='b' OR x='c') and z='2');
/*Run4*/
%mmacro(condition=((x='b' OR x='c'));
/*Run5*/
%mmacro(condition=((x='b' OR x='c'));
/*Run6*/
%mmacro(condition=((no condition));
Since you are already working within a macro, the change should be straightforward. You already have this:
where condition=&condition.
Change it to:
%if %length(&condition) %then where condition=&condition.;
You still need the semicolon as the next line to end the SELECT statement. On this replacement line, the semicolon ends the %IF %THEN statement.
I would suggest your thinking about it in "not the SAS way". Step 1 assign groups, Step 2 freq or means or whichever, by groups. E.g.
Data tbl; input ID X $ Z;
if x="a" and z=1 then group=1;
if...; cards; 1 a 2 2 a 2 3 a 1 4 b 1 5 b 2 6 c 2 7 c 1 8 c 1 9 c 2 10 c 2 ; run;
For example, a simple illustration based on inbuilt table:
data class; set sashelp.class; if age <12 then do; agegrp="Up to 12"; output; end; else if 12 <= age < 14 then do; agegrp="Up to 14"; output; end; else do; agegrp="All"; output; end; run; proc freq data=class noprint; tables agegrp*sex / out=want; run;
This is far more resource efficient, easier to code, and more robust.
Do note there are a fair few logical errors in your code also. The create table will overwrite each time. Variable Z is a numeric variable, you are logically trying to match to a text string with:
and z='1');
Not a good idea.
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.