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.
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.
Ready to level-up your skills? Choose your own adventure.