SAS (Base SAS)+
*=============================================================================;
* Create sample ADSL data;
*=============================================================================;
data adsl;
input usubjid $ trtp $ age sex $;
datalines;
SUBJ001 Placebo 67 M
SUBJ002 DrugA 59 F
SUBJ003 Placebo 72 F
SUBJ004 DrugB 65 M
SUBJ005 Placebo 60 F
;
run;
*=============================================================================;
* Define macro variable and use in filter;
*=============================================================================;
%let trtp_filter = Placebo;
data adsl_filtered;
set adsl;
if trtp = "&trtp_filter.";
run;- To make a reusable scalar in SAS, we define a macro variable with
%letand reference it with the&name.syntax. %let trtp_filter = Placebo;creates a macro variable whose value is the text "Placebo" — no quotes needed in the assignment.- In the DATA step,
if trtp = "&trtp_filter.";resolves toif trtp = "Placebo";before SAS compiles the step. - We put the reference inside double quotes so SAS still sees a quoted string literal after macro resolution. Single quotes would prevent resolution — the
&trtp_filter.would be taken as literal text. - The trailing dot terminates the macro variable name — helpful when the next character could be mistaken for part of the name.
- This is the go-to pattern for parameterising a program by study, treatment, cut-off date, or any other single value that might change between runs.
- After running, we check
adsl_filteredand confirm it contains only the Placebo rows fromadsl.