BookmarkSubscribeRSS Feed
somebody
Lapis Lazuli | Level 10

Why do we need % in front of some syntax keyword such as do, end, for, put ...etc... when inside a macro? I tried without the % and it works fine. 

2 REPLIES 2
Kurt_Bremser
Super User

% and & are the so-called "macro triggers", which instruct SAS to hand over the interpretation of the next item to the macro preprocessor.

"do" and "%do" are NOT the same. "do" is a data step statement, "%do" is a macro statement, no matter if they are used in a macro or in open code.

If you supply your code that "worked" with and without %, I can explain why that happens.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

This is a misunderstanding of what macro is and how it works.  

Macro is a text generator, a find/replace system if you will.  The text of your program is fed into the macro pre-processor which does a find replace all occurrences of macro variables by pattern & -- ., and replaces those with the value in the macro variable.  It then expands any macro code indicated by the %.  

Out of that process you then have the full plain Base SAS code which then goes into the compiler to be executed.  Therefore if you had:

%macro something ();
  %do i=1 %to 3;
    a=1;
  %end;
%mend something ();

Then then macro pre-processor knows that the %do loop is its part to expand the text out e.g. this will create after being through the macro pre-processor:

 

 

a=1;
a=1;
a=1;

Repeat the text 3 times.

 

If you replaced that with no % as:

%macro something ();
  do i=1 to 3;
    a=1;
  end;
%mend something ();

This would put in the text file:

do i=1 to 3;
  a=1;
end;

Note that in this instance the macro pre-processor is not doing the loop, it just generates that text.  When this code is fed into the compiler within a datastep then the loop is executed.

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 1285 views
  • 1 like
  • 3 in conversation