You cannot call a macro directly inside a SAS data step the way you call a function; the macro processor runs before the data step executes. To get a macro's logic into a data step you use one of three routes: CALL EXECUTE to run macro code generated row by row, resolve the macro call outside a quoted string so its output is written into the data step source, or wrap the logic in what is really a macro that emits plain SAS statements. The one you pick depends on whether the macro needs to run once per observation, once for the whole data step, or produce a value the data step can use.

What You'll Need

  • A working SAS session (Base SAS or SAS Enterprise Guide with the macro facility enabled). If your site uses SAS Studio, the same syntax applies; only the editor differs.
  • An existing macro definition or permission to write one with %macro and %mend.
  • Access to the input data set you intend to process, and an idea of which variable holds the value you want to pass to the macro.
  • Comfort reading the SAS log, because macro-generated code is compiled from what appears there.

Step-by-Step

  1. Decide when the macro should run. A macro that assigns a value, builds a list of variables, or writes out statements should run once, before or around the data step. A macro that does different work for each row should run per observation through CALL EXECUTE. Getting this decision wrong is the source of most confusion.

  2. Write the macro so it can accept what the data step will give it. Define parameters with %macro and close it with %mend. Keep the body free of run; statements if you intend to call it inside a data step, because an embedded run; will end the data step prematurely.

  3. Use CALL EXECUTE for row-by-row macro calls. Inside the data step, write call execute('%myMacro(' || trim(variable) || ')');. As the data step reads each row, CALL EXECUTE stacks the resolved text, and SAS runs that text after the data step finishes. Remember the quoting rules: use single quotes inside the call so macro references are not resolved while the data step is still reading rows.

  4. Understand what CALL EXECUTE actually does. It does not return a value to the current data step. Any new variables, data sets or reports the macro creates appear after the data step completes. If you need a value inside the same row, use a different technique.

  5. Let the macro write data step code instead. A clean pattern is to call the macro outside any quotes so its output becomes part of the data step source. For example, a macro that resolves to newvar = oldvar * 2; can be placed on its own line inside the data step, and the macro processor substitutes the text before the data step compiles. This produces a value assigned per row.

  6. Avoid quoting when you want substitution. If you wrap the macro call in double quotes it becomes a text literal, and you get the literal characters instead of the generated statement. This is the single most common reason a macro inside a data step appears to do nothing.

  7. Pass values from the data step up to a macro variable when you need one result per data step. Use call symputx('name', variable); to store a value in a macro variable, then reference &name. later to build statements.

  8. Run a small test. Build a three-row data set, call your macro with options mprint mlogic symbolgen; set, and read the log. MPRINT shows the generated statements, SYMBOLGEN shows how macro variables resolved, and MLOGEN traces the logic as it executes.

  9. Check the order in your log. Statements created by CALL EXECUTE are printed after the data step's own notes. If your generated code references a data set the step has not yet created, you will see a missing data set error. Restructure so the dependency runs in the right order.

Common Mistakes

  • Using double quotes around single quotes you thought you had inside the macro call; the macro reference resolves immediately instead of later.
  • Believing CALL EXECUTE happens line by line with the data step. The data step completes first, then the queued code runs, so the data step cannot see the results.
  • Calling a macro that returns a value as if it were a function. Macros do not return values to a data step expression.
  • Letting string concatenation produce malformed code when a variable contains blanks. Use TRIM, LEFT or STRIP to avoid stray spaces breaking the generated statement.
  • Nesting more than one CALL EXECUTE in a way that produces a run; in the middle of the generated text, which splits your data step.

Tips

  • Turn on options mprint; every time you debug. The log becomes a readable transcript of the generated code.
  • Keep macros that generate data step code small and single-purpose. Long macros called via CALL EXECUTE are hard to trace.
  • Store generated values with SYMPUTX rather than SYMPUT when you need numeric values without trailing blanks.
  • Test the macro outside the data step first, so any syntax error there is not confused with a data step error.
  • Prefer a macro that writes a full data step over a macro that builds one through CALL EXECUTE when the logic is static; it is easier to read.

Troubleshooting

  • Nothing happens and no errors appear: check that the macro ran after the data step, and that the macro reference was not quoted. Add symbolgen to confirm resolution.
  • A syntax error points at your macro name: the macro call was left outside quotes and resolved to something that is not valid SAS. Run the macro alone to see what text it produces.
  • Generated code refers to a missing data set: the statement ran before the data set existed. Move the dependency earlier or write the data set before the step that calls the macro.
  • The data step terminates early: look for a run; inside the macro body. Remove it from the macro and put it after the data step.
  • Values have trailing spaces: strip the concatenated text in the CALL EXECUTE argument.

Frequently Asked Questions

Can a macro return a value directly to a data step?

No. The macro facility is a text-substitution engine, not a function system. Use SYMPUTX to store a value in a macro variable, or have the macro emit a statement the data step compiles.

Does CALL EXECUTE run immediately for each row?

No. The code is queued as the data step reads, and runs after the data step finishes.

Why does my macro call get quoted literally?

It was placed inside quotes. Remove them so the macro processor sees and substitutes the reference.

Can I use CALL EXECUTE in PROC SQL?

No. CALL EXECUTE is a data step routine. In PROC SQL you would build and submit macro-resolved statements differently.

Do I need the macro facility licensed?

Yes, macros require the SAS macro facility, which is part of Base SAS at most sites. Check with your administrator if unsure.