简体   繁体   中英

What is the benefit of macro programming as offered by the SAS language?

In SAS, what exactly is the utility of the whole 'macro' concept? I mean, what is the point of writing something that will be converted to a program as opposed to writing a program that can be compiled and run straight away?

I only have experience with JAVA, I don't think JAVA allows macro or meta programming. Hence I was wondering what is the point of macro programming. It cannot just be to encapsulate some business logic because for that we use functions/methods/subroutines etc.

tl;dr Answer

The utility of SAS Macro, or of a text preprocessor in general, is to support constructions that would be cumbersome to write directly in the generated language.

Details

SAS Macro is a text preprocessor. It is similar in role to CPP within C/C++; M4 within several Unix utilities; Python- and Ruby-based template languages within HTML; etc. Text preprocessors have two main purposes:

  1. to minimize redundant code by providing a data driven template mechanism

  2. to transcend the limits imposed by the syntax of the generated language

Languages whose typical workflow lacks a text processor rely on string manipulation to support dynamic code generation and execution. Dynamic generation is especially difficult in Java .

Several SAS languages have a CALL EXECUTE statement, but over time, users have preferred to use SAS Macro. Users rely on SAS Macro in part because it is an exceptionally good text processor. Unlike most other text processors, it supports flow control, scope, function definitions, arithmetic, invoking library functions, comments, interactive input, and has (for a preprocessor, of course) an unobtrusive syntax.

Because it is perhaps too good, users have preferred SAS macro where Object-Oriented approaches (ie, using PROC DS2 , or more recently PROC GROOVY or PROC LUA ) would have led to more maintainable code. New languages and new ways to access SAS are added frequently, but because SAS has so many users who have written pages linking to the old docs, sometimes PageRank can be slow to catch up. So people end up overusing Macro.

In my workflow, I search for whatever SAS language or PROC is best-suited to the task at hand, and perform the bulk of the processing in that language. However, every single day there is a use case where Macro makes things simpler than anything else. In those cases I use Macro. If you follow the principle of generated language first, Macro second, I think you'll find Macro to be an invaluable tool.

Java supports an object-oriented programming paradigm. This allows you to use classes, methods, etc, to control the program flow and achieve the equivalent of basic programming structures such as loops, if-then logic etc...

I'm not 100% sure what name has been assigned to the programming paradigm SAS uses, I guess I'd call it a sequential language, as the code is simply executed from top to bottom 1 statement at a time.

This means that in the SAS language, outside of macros, there is no part of the language that will allow you to achieve control structures most languages take for granted. It has no concept of a 'procedure' (used in the general programming sense of the word). SAS simply executes code from the top to the bottom one statement at a time.

The uses for macro code in SAS (as I see it) are:

  • making programs data-driven
  • wrapping proc's in control structures (ie. conditionally executing certain pieces of code, executing sections of code in a loop, etc.)
  • generating required code that is unknown pre-execution

Some examples would be:

  • run a datastep, only if a certain table already exists
  • run a proc freq statement over 100 different tables (as opposed to copy/pasting 100 individual proc freq statements and maintaining them as the data changes)
  • generate a single number or word that I don't happen to know pre-execution time, as these values can simply be substituted into the code

Macros can make our life easier but it comes at a cost. Macro code is much harder to read/write/debug and generally should be avoided if there is a non-macro driven solution available.

The primary purpose of the SAS macro language is to be a SAS code generator. A SAS macro will typically generate SAS CODE (could be a lot of data steps and proc steps or a part of one statement). Often it is useful (less typing) to generate SAS code rather than type it (or copy-paste). People do use macros for functions and subroutines.

That said, if you are new to SAS, I would suggest you do not learn the macro language now. It makes more sense to learn the SAS language well first, before learning the macro language to generate SAS code. Many experienced SAS programmers completely avoid the macro language.

I'll try to address this portion of the question:

What is the point of writing something that will be converted to a program as opposed to writing a program that can be compiled and run straight away?

  • Macro's allow conditional execution that isn't easily possible outside of the macro language.
  • Macro's allow looping and iterative control - I don't want to write the same code 100 times.
  • Macro's provide a method for generic code that can be considered a routine in Java or other languages.

Although SAS has functions, I don't think it has subroutines and methods and this is part of the gap that the macro language is intended to fill.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM