Variables written with a leading "&" inside a text will be resolved into the character or numeric expression the corresponding object is holding.
Details
Macro variables in 'SAS' can be set up with %Let and can act as global accessible variables. This in itself is nothing special to 'R' because basically every object created outside a function is a global variable.
To use these global objects within a text one has to use e.g. paste0("The current year is: ", year). With the macro function one can write it like this: macro("The current year is &year").
So where is the benefit? If implemented within a function, a parameter like "title" or "footnote" in the tabulation functions, can resolve these variables without the need of another function. You can just pass the character expression "The current year is &year" and the implementation inside the function resolves the macro variable directly in place.
See also
Functions that can make use of macro variables: any_table(), frequencies(),
crosstabs(), export_with_style(), summarise_plus(), transpose_plus(),
rename_multi(), compute.
Within the tabulation functions titles, footnotes, var_labels and stat_labels
can resolve macros. In var_labels and stat_labels macros can be used in both
the variable names and the labels. Additionally they can be used in the any_table() rows,
columns, values, by, pct_group, pct_value, pct_block and box parameter, in
the summarise_plus() class, values and types parameter and in the
transpose_plus() values parameter. In the statistics parameter of
any_table(), summarise_plus() and transpose_plus() macros can be used
in the variable names of a statistics list.
Macros can also be used inside the formats parameter of all functions that accept it. Both the variable names and the format names can be built with macros, e.g. list("&variable_name" = "&format_name").
In rename_multi() macros can be used in both the old and the new names,
e.g. rename_multi("&old_var" = "&new_var").
In compute. macros can be used in the names of the variables to be created, e.g. compute.("income_®ion" = income).
Examples
# Resolving macro variable in single character
year <- 2026
text <- macro("The current year is &year")
# You can also combine multiple macro variables
some_variable <- "current"
current2026 <- "The current year is"
text_combi <- macro("&&some_variable&year &year")
# Resolving macro variable in character vector
char_vector <- c("The current year is &year",
"The &some_variable year is &year",
"&&some_variable&year &year")
text_vector <- apply_macro(char_vector)
