A SAS macro is a named block of reusable code defined with `%macro` and ended with `%mend`
Once defined, the macro is invoked by writing its name preceded by a percent sign — for example `%mymacro;`
When SAS encounters a macro call, it replaces it with the code inside the macro definition before the program is compiled and run
Macros reduce repetition: instead of writing the same block of code multiple times, you write it once inside a macro and call it wherever it is needed
Macros can also accept parameters, making them adaptable to different inputs each time they are called
The macro language is a separate layer that runs before SAS code — it generates SAS statements rather than running them directly
Macro variables versus macros
A macro variable (covered in the macro variables lessons) stores a single text value and is referenced with `&name`
A macro (this lesson) is a named block of code — it can contain DATA steps, PROC steps, logic, and macro variables all together
Think of a macro variable as a text substitution, and a macro as a reusable subroutine
Basic macro syntax
The definition starts with `%macro macroname;` and ends with `%mend macroname;`
Everything between those two lines is the macro body — the SAS code that will be inserted when the macro is called
The macro name in `%mend` is optional, but recommended because it makes long programs easier to read
SAS Log
A macro with no parameters
The simplest macro runs the same fixed code every time it is called
This is useful for setup steps such as setting options, printing a standard header dataset, or running a recurring quality check
The macro below prints the current date and a short message to the log each time it is called
SAS Log
The macro is defined once, but called twice — check the SAS log and confirm that the date message appears twice
The two calls produce identical output because there are no parameters — the same code runs each time
A macro with parameters
Parameters make a macro flexible — they are named placeholders in the macro definition that receive values when the macro is called
Parameters are listed inside parentheses after the macro name in the `%macro` line, separated by commas
Inside the macro body, parameters are referenced as macro variables using the `&name` syntax
At the call site, values are passed inside parentheses matching the parameter names
SAS Log
The first call prints 3 observations from SASHELP.CLASS, and the second prints 5 from SASHELP.CARS
The same macro definition handles both because the dataset name and row count are passed as parameters
The `=` syntax in the parameter list sets default values — `n=5` means that if `n` is not passed, 5 is used automatically
Default parameter values
When a default is specified in the macro definition, the caller does not need to provide that parameter
If the caller omits a parameter that has no default, it resolves to an empty string, which may cause syntax errors — so always provide sensible defaults or document which parameters are required
The example below shows a call that omits `n` and therefore uses the default value of 5
SAS Log
This call produces 5 rows from SASHELP.CLASS because `n` was not supplied and the default of 5 was used
Compare the output with the earlier call that passed `n=3` to confirm the difference
A practical macro - summarise a variable in any dataset
The macro below uses PROC MEANS to summarise a chosen numeric variable from any dataset
Three parameters control the behaviour: the dataset name, the variable to summarise, and the output dataset name
This is a common pattern in real programs, where the same summary logic is needed for multiple variables or domains
SAS Log
Two summary datasets are created from two macro calls with different `var` and `out` values
Inspect `height_summary` and `weight_summary` and confirm that the statistics match expectations for the SASHELP.CLASS data
Notice how the macro call is much shorter than writing two separate PROC MEANS steps with different variable names
Dataset View
Conditional logic inside a macro using %IF %THEN %ELSE
Macros can contain `%if` / `%then` / `%else` statements that evaluate conditions at macro execution time
These are macro-level statements and run before any SAS DATA step or PROC step is executed
A common use is to choose between two different code paths based on a parameter value
SAS Log
The first call runs PROC PRINT, the second runs PROC CONTENTS, and the third triggers the `%else` warning branch
Check the SAS log — the third call should produce a WARNING message written by `%put`, but no SAS procedure output
`%put` writes text directly to the SAS log and is very useful for debugging macro behaviour
Key points to remember
Define macros with `%macro name;` and end with `%mend name;` — call them with `%name;`
Parameters are listed in parentheses after the macro name and referenced inside the body as `&name`
Default values in the parameter list make parameters optional at the call site
Use `%if` / `%then` / `%else` / `%do` / `%end` for conditional macro logic — these run at macro execution time, before SAS compiles the generated code
`%put` writes messages to the SAS log and is the primary macro debugging tool
Macros do not return values — they generate SAS code that is then executed by SAS