BookmarkSubscribeRSS Feed
ruchi11dec
Obsidian | Level 7
I know many situations when macros can be really helpful but what are the different situations when macros are of no help
12 REPLIES 12
RW9
Diamond | Level 26 RW9
Diamond | Level 26

SAS Macros are never needed.  They do not actually do anything.  There only purpose is to generate some text which can, but doesn't necessarily need to be, SAS code.  Therefore anything you can do in macro you can do in Base SAS, but not everything you can do in Base SAS you can do in Macro (well, you can use sysfunc, but you would really have to be mad to go that way).

 

One of the most posted questions on here seems to be macro lists and loops.  It is not a good a good idea as Macro is text only dates and numeric along with lists become problematic.  Base SAS has all the datatypes, and a datastep is inherently a loop, not to mention you have all the loop constructs, and the most powerful thing in the language, by group processing.  

 

At the end of the day, if you design your code, and by that I mean sit down, plan the code in a functional design spec, show the interactions, get agreements on the inputs and outputs - throw a wobbly or push the budget up if needed - then there really is very little you can't do in straight code.  It is useful to have macro at the system level, global tools, setup macro variables and such like, but anything else...

PaigeMiller
Diamond | Level 26

SAS Macros are never needed.  

 

Macros are very useful tools. Saying they are never need is like saying automobiles are never needed.

--
Paige Miller
RW9
Diamond | Level 26 RW9
Diamond | Level 26

The statement is true, macros are never needed.

 

 

They can be useful in certain circumstances as I mention, however as I have seen it they are 60% used to cover a lack of understanding in Base SAS, and another 30% used in legacy creaking libraries without documentation, validation, or in a large proportion of the time, sense.  

 

Your examples of automobiles is exactly the same, they are never needed.  They are however sometimes useful if you need to get somewhere quickly or long distance and don't want to use public transport, however with a slight move that way and we have a society who cannot move between the bed and the loo without getting in the car and driving there. 

 

There is a difference between showing usefulness and dependence.  

Kurt_Bremser
Super User

@RW9 wrote:

The statement is true, macros are never needed.

 

 

They can be useful in certain circumstances as I mention, however as I have seen it they are 60% used to cover a lack of understanding in Base SAS, and another 30% used in legacy creaking libraries without documentation, validation, or in a large proportion of the time, sense.  

 

Your examples of automobiles is exactly the same, they are never needed.  They are however sometimes useful if you need to get somewhere quickly or long distance and don't want to use public transport, however with a slight move that way and we have a society who cannot move between the bed and the loo without getting in the car and driving there. 

 

There is a difference between showing usefulness and dependence.  


A very nice clarification. Might make make it into Maxim 11.

 

Edit: Maxim 11 expanded

art297
Opal | Level 21

I don't think there are specific tasks where macros can't be helpful. The questions I think one has to ask include:

1. Is it a recurring task?

2. Can it be done without involving a macro?

3. Does it simplify the task (i.e., require less code to accomplish a recurring task)?

4. Does it speed up, or slow down, processing?

 

Art, CEO, AnalystFinder.com

 

 

 

Kurt_Bremser
Super User

The macro facility is a preprocessor (much like the C preprocessor that's so important in C programming) that lets you create dynamic SAS code, if you use it wisely.

In > 90% of the questions concerning macros here on the community, it is a given that someone abuses this facility for the simple reason it's there, without having the slightest clue what it does.

Especially with the case of macro lists, there's a 100% probability you're better off storing the data in a dataset and using call execute from that.

 

This is the reason why @RW9 is so adamant about it, and why my Maxim 11 exists; most of the people coming here have not yet reached the level of SAS understanding that would enable them to make wise use of the macro facility, and should therefore learn to use SAS without macros first.

 

A simple rule of thumb:

  • does my macro handle data - ABUSE!
  • does my macro create code that handles data in a way otherwise not possible or inefficient - valid use.
Astounding
PROC Star

The most common situation where macros are of no help ...

 

If you don't know how to program a solution without macros, then macros are of no help.  Macros generate a program, and if you can't visualize what that program should look like, forget about macros.

ballardw
Super User

@ruchi11dec wrote:
I know many situations when macros can be really helpful but what are the different situations when macros are of no help

I have seen macro code that the only "purpose" I could discern was to obfuscate processing. I suspect that it may have been a "job security measure" i.e. no one knows what this does so they can't fire me. A very obvious "no help" situation.

 

I have also inherited code that replaced a few lines of data step code with a macro to generate hard coded lines of code. And was NOT reused anywhere. As far as I could tell the only result was to create a data step with fewer than 100 or so lines by replacing blocks of 3 to 8 lines with single macro statements. Resulting in a data step that was hard to understand. I consider this a "no help" situation.

 

I suggest looking very strongly at any line of code with more than one %sysfunc call and perhaps that should move into a data _null_ to generate values. Mostly because multiple nested %sysfunc calls become ugly and hard to parse with multiple () involved.

 

If there is nothing repetitive (%do loops), no parameters passed into a macro or no conditional code generation then the macro might be another candidate for evaluation as to actually usefulness.

 

 

Amir
PROC Star

Hi,

 

The question is quite open can have a number of answers (as we've already seen).

 

Is there some context to this question?

 

What has caused you to ask this question? Were you asked / told this or did you read it somewhere?

 

Is there something you're trying to do or is this just an academic endeavour?

 

 

Regards,

Amir.

SASKiwi
PROC Star

I've had to deal with SAS applications (not mine) where ALL code was in macros and there were no macro statements apart from those defining and calling the macros. This is the classic case of macros being totally useless. It was simply a habit of the developers.

 

On the other hand I support a custom ETL process I built using macros that is completely data-driven, and avoids me writing and supporting over 10,000 lines of SQL - the SQL is written automatically. Now that is what I call useful.  

ruchi11dec
Obsidian | Level 7

Hey Amir,

 

It was asked to me during a recent interview, so I expect that they were expecting, that I should tell some scenarios were macros would not help me solving a problem or something like that...

Quentin
Super User

That's an excellent interview question.  Particularly for any experienced programmer who puts MACRO on their resume.  It would (could) start an interesting discussion about the costs and benefits of code generation, modularization, etc.

 

Too many people learn the macro language too soon, and over-use it seeing every problem as having a macro solution.  Or maybe over-use of macro is just a natural step on the learning curve.  When you understand *when* it's appropriate to use macro, and can explain *why* in your judgement macro is appropriate in those contexts, then you're an experienced macro user.

 

It's not surprising to me that there will be as many different answers to the question as there are programmers.  Lots of folks hate the macro language, or see it as something of limited use which should be avoided as much as possible.  Even among macro enthusiasts, we differ dramatically on our thoughts on the appropriateness of things like :

  • autocall libraries
  • permanent compiled macro libraries
  • macros with no parameters
  • macros with no macro code (i.e. using macros solely for modularlzation, like %include)
  • global macro variables
  • function-style macros
  • etc etc etc

 

BASUG is hosting free webinars Next up: Jane Eslinger presenting PROC REPORT and the ODS EXCEL destination on Mar 27 at noon ET. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 12 replies
  • 1467 views
  • 18 likes
  • 10 in conversation