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 (
%MACROand%MEND).
Step-by-Step
- Start your macro definition with the
%MACROstatement. For example:%MACRO mymacro;. This begins the macro and must be the first statement in the macro definition. - 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.
- Place the
%MENDstatement 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). - Ensure
%MENDis 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. - Compile the macro by running the entire block of code (from
%MACROto%MEND) in your SAS session. You’ll see a note in the log like “NOTE: Macro MYMACRO was compiled.” - 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
%MENDstatement – the most common error. Without it, SAS will treat subsequent code as part of the macro, leading to errors. - Using a semicolon after
%MENDincorrectly –%MEND;is correct;%MENDwithout 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
%MENDinside a conditional block – for example, inside an%IFstatement. The%MENDmust 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%MENDmust 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
%MENDat the same indentation level as%MACROfor readability. - Comment your macros – add a comment before
%MENDexplaining 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
- Battery Bank Calculation for UPS: Step-by-Step Guide
- Casio Ink for Calculator? What to Use & Avoid
- Google Sheets SUM Formula: How to Add Numbers
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- Excel Macro Location: Where VBA Code Is StoredIn Excel for Microsoft 365 on Windows, a macro is usually stored as VBA code inside the workbook…
- Macro en VBA? What It Means & How to Use ItIn Excel, a macro is a recorded or written sequence of actions that automates repetitive tasks, and VBA…
- Get Year in VBA: Date Functions ExplainedTo get the year from a date in VBA, use the Year function: Year(Date) returns the current year…
- VBA UserForm ScrollBar Property: How to Use ItThe ScrollBar property in VBA UserForms is a control you add from the Toolbox that lets users select…
- VBA Dictionary Multiple Items per Key: Practical GuideYes. A VBA Scripting.Dictionary can hold multiple items for one key if you store a container—most often a…
- Access VBA MsgBox: Syntax, Buttons, and ExamplesIn Microsoft Access VBA, use MsgBox to display a message or ask a user a simple question; its…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.