As you noticed, you can do this. However, notice a couple of features about your program.
The value of &CONDITION does not include a semicolon ending the WHERE statement. The semicolon at the end of the %LET statement is needed to end the %LET statement. So the value of &CONDITION is:
where a>5
The program that refers to &CONDITION uses:
&condition;
That added semicolon following &CONDITION is what ends the WHERE statement.
If you wanted to include a semicolon as part of the value of &CONDITION, that is possible using:
%let condition = %str(where a>5; );
The %STR function treats the first semicolon as text, not as a symbolic character that ends the %LET statement.
You can answer your question by executing the code, reading the log and fixing errors that may be shown. If you need help fixing errors, post the log in code window see {i}-icon above the editing window.
As you noticed, you can do this. However, notice a couple of features about your program.
The value of &CONDITION does not include a semicolon ending the WHERE statement. The semicolon at the end of the %LET statement is needed to end the %LET statement. So the value of &CONDITION is:
where a>5
The program that refers to &CONDITION uses:
&condition;
That added semicolon following &CONDITION is what ends the WHERE statement.
If you wanted to include a semicolon as part of the value of &CONDITION, that is possible using:
%let condition = %str(where a>5; );
The %STR function treats the first semicolon as text, not as a symbolic character that ends the %LET statement.
I would NOT do it that way; if I have to do something similar, I do this:
%let condition=a>5;
data test;
set source;
where &condition.;
run;
That way it is clear which statement is executed, and only the condition itself comes from the macro variable. This makes code much more read- and maintainable.
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.