SAS (Base SAS)+
*==============================================================================;
*Macro variable to store a list of values;
*==============================================================================;
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;
%let age_filter = %str(59,72);
data adsl_filtered;
set adsl;
if age in (&age_filter.);
run;- To store a list of values in a SAS macro variable, we use
%letwith%str()to protect the commas from being interpreted as parameters. %let age_filter = %str(59,72);creates a macro variable whose resolved value is the literal text "59,72".- We need
%str()because bare commas in macro parameters have meaning to the macro processor — wrapping them in%str()tells SAS to treat them as ordinary characters. - In the DATA step,
if age in (&age_filter.);resolves toif age in (59,72);. - Note we are not using quotes here because
ageis numeric — for a character list we would need to build "A","B" and use%bquoteor careful quoting. - This pattern is the SAS idiom for a parameterised IN-list: change the macro, rerun the step.
- After running, we check
adsl_filteredand confirm only the rows with age 59 or 72 appear.