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.
% 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.
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.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.