DATA Step, Macro, Functions and more

SAS Macro Language

Reply
New Contributor
Posts: 3

SAS Macro Language

[ Edited ]

what is the use of macro in sas?

PROC Star
Posts: 763

Re: sas

Do you mean how to run a macro? Smiley Happy

 

Please be more specific

New Contributor
Posts: 3

Re: sas

yea, i want to know about the structure and the functions of macro..
Super User
Posts: 7,832

Re: SAS Macro Language

[ Edited ]

The macro language is a pre-processor that allows one to create program code dynamically, equally to the function of the C preprocessor.

 

Macro statements are acted upon immediately when encountered, the resulting code is then fed on to the regular SAS interpreter for running (procedures) or compiling and executing (data step).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 7,832

Re: SAS Macro Language

The most comprehensive source for information is the SAS documentation itself, a good starting point for the macro language is here: https://support.sas.com/documentation/cdl/en/mcrolref/67912/HTML/default/viewer.htm#p1ccprwibo8gvqn1...

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
PROC Star
Posts: 763

Re: SAS Macro Language

There is a ton of material on the macro language in SAS, a quick google search will get you far.

 

http://www.ats.ucla.edu/stat/sas/seminars/sas_macros_introduction/

 

This is eg a good starting point with a low of simple examples along with a few videos with explanation. 

 

Good luck Smiley Happy

Super User
Super User
Posts: 7,977

Re: SAS Macro Language

[ Edited ]

Generally speaking from 99% of the examples I have seen it is to obfuscate and complicate code.  SAS Base (also known as dataste) is the programming language - this should be the main part to learn and will cover 99% of all encountered needs.  It contains all the data structures, functions and such like to manipulate and process data.

Macro language is an additional tool, used to generate Base SAS code.  It does nothing on its own other than generate text.  It has no data structures other than text, and it has no procedures/functions other than a couple of basic ones to generate text.

If you do need to write macro code - maybe for libraries or general code - then you should really be following Software Development Lifecycle mehodology - design the code in a documen first, create a teting plan, create a management plan etc.  Then write some code.  Don't fall into the "have to" use macro code, or over using it - Base SAS is where you can do almost every task imaginable.

 

I would also add, in the large majority of cases where macro is used, it is necessary for one of two reasons:

- Poor data structuring, some examples of which - holding similar data in many different datasets, using data as dataset names/variable names, using wide rather than long format (i.e. using a review type layout, rather than one designed for programming)

- Lack of understanding of Base SAS, something like using macro to loop over variables when arrays can do that, or generating lots of proc print/report statements rather than using by group processing.

 

Ask a Question
Discussion stats
  • 6 replies
  • 223 views
  • 5 likes
  • 4 in conversation