How to End a Macro in SAS (Step-by-Step Guide)

VBA & macros · Updated November 23, 2026

Ending a macro in SAS is done with the %MEND statement, which closes the macro definition that began with %MACRO. This guide shows you the exact syntax, how to place it correctly, and what to do when your macro doesn’t end as expected.

What You’ll Need

  • A SAS environment (Base SAS, SAS Studio, or SAS Enterprise Guide).
  • A text editor or the SAS editor to write your macro code.
  • Basic understanding of SAS macro syntax (%MACRO and %MEND).

Step-by-Step

  1. Start your macro definition with the %MACRO statement. For example: %MACRO mymacro;. This begins the macro and must be the first statement in the macro definition.
  2. Write the macro body – the SAS statements or macro statements you want to execute when the macro is called. For instance, a simple DATA step or PROC step.
  3. Place the %MEND statement at the very end of the macro definition. The syntax is %MEND; or optionally %MEND macroname; (the name is optional but recommended for clarity when you have many macros).
  4. Ensure %MEND is on its own line (or at least separated from other statements) to avoid syntax confusion. SAS does not require it to be alone, but it’s best practice.
  5. Compile the macro by running the entire block of code (from %MACRO to %MEND) in your SAS session. You’ll see a note in the log like “NOTE: Macro MYMACRO was compiled.”
  6. Call the macro to test it, using %mymacro; (without a semicolon after the macro name in the call, though a semicolon at the end of the call statement is fine).

Common Mistakes

  • Forgetting the %MEND statement – the most common error. Without it, SAS will treat subsequent code as part of the macro, leading to errors.
  • Using a semicolon after %MEND incorrectly – %MEND; is correct; %MEND without a semicolon is also accepted, but adding a semicolon is standard.
  • Mismatched macro names – if you write %MEND mymacro; but the macro name is different, SAS will issue a warning.
  • Placing %MEND inside a conditional block – for example, inside an %IF statement. The %MEND must be outside any conditional logic.
  • Ending a macro with a DATA step’s RUN statement – a common mistake is to put RUN; after the last step but before %MEND. That’s fine, but the %MEND must still be present.

Tips

  • Always include the macro name in %MEND – e.g., %MEND mymacro; – to make your code self-documenting and catch mismatches.
  • Use consistent indentation for the macro body and place %MEND at the same indentation level as %MACRO for readability.
  • Comment your macros – add a comment before %MEND explaining what the macro does, especially if it’s long.
  • Test with a minimal macro – if you’re unsure, create a tiny macro like %MACRO test; %PUT Hello; %MEND test; and run it to verify your environment.
  • Use the SAS macro debugger (available in SAS Studio and Enterprise Guide) to step through macro execution if you have complex logic.

Troubleshooting

Issue: SAS says “ERROR: The macro MYMACRO is still open” or “ERROR: A macro definition is not terminated.”

This means SAS reached the end of the program without finding a %MEND. Check that you have the %MEND statement at the very end of your macro. Also, check for unmatched %DO/%END or %IF/%THEN blocks inside the macro – a missing %END can confuse SAS about where the macro ends.

Issue: The macro compiles but doesn’t run as expected.

If the macro compiles, the %MEND is likely correct. The problem may be in the macro body. Check the SAS log for errors after calling the macro. Also, ensure you didn’t accidentally place a semicolon immediately after %MACRO that terminates the macro definition early – that would cause SAS to treat the rest as normal code.

Issue: “ERROR: The macro MYMACRO was not found.”

This usually means the macro was not compiled (you didn’t run the %MACRO block) or you misspelled the name in the call. Re-run the macro definition and check the log for a compilation note.

Issue: “WARNING: The macro MYMACRO is still open” when you run a new program.

This happens when you have an unclosed macro in a previous program. Close it by adding %MEND; to the end of the previous code, or restart your SAS session to clear the open macro.

Frequently Asked Questions

Q: Do I need a semicolon after %MEND?
A: Yes, it is standard to include a semicolon: %MEND;. SAS accepts %MEND without a semicolon, but adding it is consistent with other SAS statements and avoids confusion.

Q: Can I have multiple %MEND statements in one macro?
A: No, each macro definition must have exactly one %MEND that closes it. Having more than one will cause an error.

Q: What happens if I forget %MEND?
A: SAS will treat all subsequent code as part of the macro until it finds a %MEND or hits the end of the program. This often leads to errors like “Macro definition is not terminated” or unexpected behavior.

Q: Is %MEND case-sensitive?
A: No, SAS is case-insensitive for macro keywords. %mend; works just as well as %MEND;.

Q: Can I put %MEND on the same line as the last statement?
A: Yes, technically you can, but it’s bad practice. Always put %MEND on its own line to make the macro’s end clear.

Q: How do I end a macro that is defined inside another macro (nested macros)?
A: Each nested macro needs its own %MEND. The inner macro’s %MEND comes before the outer macro’s %MEND. For example: %MACRO outer; %MACRO inner; ... %MEND inner; ... %MEND outer;

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.