BookmarkSubscribeRSS Feed
crawfjor
Calcite | Level 5

I'm having a difficult time 'understanding' macros. What is their purpose and the value in putting a collective of values into a macro?

 

Thanks!

8 REPLIES 8
SASKiwi
PROC Star

SAS macros are primarily SAS code generators. For example I have a group of macros that generate in excess of 10,000 lines of SAS SQL code yet the macros themselves are about 300 lines of code. That means I'm saving myself writing all of that SQL manually. Not only that I'm saving myself from doing any maintenance on those > 10K code lines. 

Shmuel
Garnet | Level 18

There are some advantages of using macros in sas programming:

1) As @SASKiwi wrote - saving writing by calling same macro code.
    It is like calling a function or a procedure and just change the arguments when need.
2) Generating different code according to situation defined by argument(s).

    It is like a traffic light of a train to take the right track.

 

Argument are actually macro variables.

To use a macro variable in code it should be preceded by "&".

 

You can assign a value to a macro variable by different ways:

1)  %let name = value; 

     It should be defined out of any data step or procedure but it can be used inside a macro program.

     Refer to it in the code by  &name.

 

2) call symput('name',<value>);

    It is a sas statement in a data step.

    Value can be given as a literal or as a variable or as an expression;

 

In next example I shall use MP for macro program and MV for macro variable:

3) %macro MPname (MVname1, MVname2, ... MVnamex=<MV value>, ... );

     where MVname1, MVname2 should be supplied in same order as in %macro statement,

     while MVnamex= is a named argument, should be supplied last.

     if you have more then one named argument you can supply them in any order.


There is much more to learn about macro programming.

I hope this answers your question.     

    

PaigeMiller
Diamond | Level 26

Here's one example of a use of macros:

 

I have a program that runs every month. The program figures out that today's date is 02DEC2020. Then, it has to extract the data from a database from the last 12 months (or specifically, December 2019 to November 2020) for analysis. The macro (and macro variables) determine the time frame to use when extracting the data automatically, no human involvement needed. If I didn't use a macro, I would have to change the code every month to pick the proper 12 months, so human involvement would be needed.

 

This is just one example of the use of macros. Macros can make your code dynamic, so that it changes as the situation changes.

--
Paige Miller
Cynthia_sas
SAS Super FREQ
HI:
I joke in class that the SAS Macro Facility is just a big, dumb typewriter that does code "find and replace" and code generation for you. Here's a paper I wrote that has some concrete examples: https://support.sas.com/resources/papers/proceedings13/120-2013.pdf

Cynthia
Kurt_Bremser
Super User

I would not call it dumb. Our current problems with macro usage (as reflected by hundreds of threads here on the communities) comes from the fact that macros are taught MUCH too early in the Base SAS Programming courses.

 

When I started, I spent 3 (as in three - THREE!) weeks in Heidelberg learning DATA and PROC steps, reading external files, sorting, merging, doing summaries, creating output, before, in the 4th week, we got to SQL and then macro programming. So, at that stage, I had sufficient knowledge about the code I would create with the macro processor.

 

 

Cynthia_sas
SAS Super FREQ
Hi:
I understand -- I use that example and the word "dumb" usually to emphasize that the Macro Facility is not responsible for doing the analysis or producing the report. As when someone says that they want to "write a macro that analyzes their data and does a forecast" or "write a macro that will create all their reports".

One of the really important things that beginning Macro Facility users need to understand is that the Macro Facility is only generating code -- not doing the analysis or creating the report. The Macro Facility is essentially generating code. The Macro Facility is NOT the DATA step, NOT the PROC STEP. It is not SAS/STAT or SAS/ETS. Programmers need to understand the correct code to do what they need to do first and then introduce the use of macro variables and macro programs into their process.

I spent almost the same amount of time learning SAS before I learned about the Macro Facility. I also had the benefit of working with a mentor who insisted that I show her my working program before she would help me figure out whether using macro variables or a macro program would help the process. That was the best education on the iterative process of developing a macro program and using macro variables that I could have had.

Cynthia
bhufman
Calcite | Level 5
You basically use Macros if you find yourself needing a certain code constantly, that way you don't keep recoding the same thing. Essentially they are meant to save you a lot of time!
Tom
Super User Tom
Super User

@crawfjor wrote:

I'm having a difficult time 'understanding' macros. What is their purpose and the value in putting a collective of values into a macro?

 

Thanks!


A macro symbol (or macro variable) is a way to re-use the same string of characters many places in your program without re-typing them over and over.  So if you wanted use the same list of variable names in multiple statements you could put the list in a macro variable and then reference the macro variable where ever you needed that list in your code.  It can also serve as a simple way to make your program dynamic so that if you wanted to do the same thing another day but with a different list of variables (or whatever type of string the macro variable holds) you only need to modify that one line of the program.

%let varlist=AGE HEIGHT ;
proc means data=sashelp.class ;
  var &varlist;
run;
proc glm data=sashelp.class;
  class sex;
  model weight=sex &varlist;
run;

An actual macro allows you to use conditional logic and generate much more complex text than the simple text expansion of a macro variable.  

%macro analysis(data=,varlist=);
proc means data=&data ;
  var &varlist;
run;
proc glm data=&data;
  class sex;
  model weight=sex &varlist;
run;
%mend analysis;

%analysis(data=sashelp.class,varlist=age height);

 

But the key thing to remember when using either macro variables or macros is that the goal of them it to help you generate valid SAS code that SAS can understand and run.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1169 views
  • 3 likes
  • 8 in conversation