Technology Tales

Notes drawn from experiences in consumer and enterprise technology

TOPIC: C PREPROCESSOR

Fixing CALL EXECUTE Macro Variable Resolution and Macro Execution Timing in SAS

31st August 2026

A recent run of SAS macro writing has seen me save some effort by using CALL EXECUTE within data steps. This goes well when everything is dataset-based since you are using the loop that is at the heart of data step processing. Things need more need when you start mixing in macro variables and macro calls because CALL EXECUTE blocks usually execute at the step boundary, which is not when macro processing takes place.

That is because the macro processor acts before the compilation and execution of normal SAS code, which makes sense because this is a form of metaprogramming. After all, SAS Macro builds up code to be compiled and executed; this happens in one sequence making the process feel like script interpretation and execution to a user. With CALL EXECUTE, this has the consequence of calling an executing a macro out of sequence with the inputs that it needs to complete successfully. Thus, the code below will fail to do that.

data _null_;
    line = cat("data x; a= ", var1 ,"; run;";)
    call execute(line);
    line = cat("%macro1(invar=", var2, ")");
    call execute(line);
run;

The solution to this sequencing issue is to enclose any macro variables and calls in single quotes within CALL EXECUTE function input to delay macro processing (usually, you use double quotes to ensure that macro code is resolved but not here). Making that change turns the code above into what follows below:

data _null_;
    line = cat("data x; a= ", var1 ,"; run;";)
    call execute(line);
    line = cat('%macro1(invar=', var2, ')');
    call execute(line);
run;

Then, everything works in the expected sequence to give the expected results and without issuing confusing errors and warnings to the SAS log. While it took a search to find it, SAS does have documentation showing how to accomplish this sort of thing, which helps when understanding what is happening and what is needed.

  • The content, images, and materials on this website are protected by copyright law and may not be reproduced, distributed, transmitted, displayed, or published in any form without the prior written permission of the copyright holder. All trademarks, logos, and brand names mentioned on this website are the property of their respective owners. Unauthorised use or duplication of these materials may violate copyright, trademark and other applicable laws, and could result in criminal or civil penalties.

  • All comments on this website are moderated and should contribute meaningfully to the discussion. We welcome diverse viewpoints expressed respectfully, but reserve the right to remove any comments containing hate speech, profanity, personal attacks, spam, promotional content or other inappropriate material without notice. Please note that comment moderation may take up to 24 hours, and that repeatedly violating these guidelines may result in being banned from future participation.

  • By submitting a comment, you grant us the right to publish and edit it as needed, whilst retaining your ownership of the content. Your email address will never be published or shared, though it is required for moderation purposes.