Macro Programming (from Fraktal SAS Programming)

Wechseln zu: Navigation, Suche





Frankly, a SAS Macro is a code generator.

Even if SAS Macros are feeding their output directly into the SAS compiler, there is no need to restrict the code generated to SAS syntax. Instead, SAS syntax can be used to direct the code generated to any destination in reach, be it local files, portable media or some i-node on the internet. The same flexibility holds for local processes or ports on some IP-Address, but this will be discussed in detail elsewhere in these guidelines.


The authors did not design a new syntax to program the code generator, but used the same as they did for data processing, which is, the data step language. However, there's one small difference: Where datastep language does distinguish data types such as numeric and character, the macro language does not. All values processed are character, which is, the code composed and finally output, i.e. sent to the SAS compiler.

On the one hand, this is an advantage, since it did not require another learning phase for the novice user, on the other hand this is a solid mess when macro code and data step language are mixed. The latter is possible and widely used by all levels of programmers.

Hello World

The programmer's entry code gives an impression:

%MACRO first;
%PUT Hello World;
%MEND first;

To execute, issue the statement:


No Open Code

To achieve the same effect as in the example above, this code could also be used:

%PUT Hello World;

Such coding is called OPEN CODE and is possible for rapid prototyping in SAS when there's no logic, looping and branching required. This is not SAS Macro Programming as we want to use the term here.

Throughout these guidelines we will use the full range of Macro Coding to program real SAS Macros, i.e. start each definition with a %MACRO statement and terminate it with a %MEND statement. A referential Example Macro Structure is developed step-by-step and shown in the General Programming Remarks in the introductory chapters.

Annotated SAS Macros

The code generator properties are best demonstrated when no SAS code is output. To start, we will paraphrase some commands known from the MS-DOS command shell as SAS Macros.