An Idea Exchange for SAS software and services

by SAS Super FREQ
on ‎05-19-2016 03:44 PM

Hi Paul,

It sounds like you are frustrated, which I completely understand. Macro programming can be very frustrating.


I am not a macro expert, but I think I understand why this seemingly excellent suggestion might not be possible to implement. From a language point of view the content of a macro is just text.  Text can be inserted anywhere, and without context that text can't get color coded.


Here's an example. Suppose that you write the following macro:

%macro doit(procname, dsname, varnames);

proc &procname data=&dsname;

var &varnames;




Now to you the programmer, you think that the text contains special keywords like PROC and DATA= and VAR and RUN. You would like to see those keywords colorcoded because you intend to call the macro like this:

%doit(means, sashelp.class, height weight age)



Unfortunately, there are other instances in which text in the macro is syntactically valid. For example, the macro might be inserted into a string:

data text;

text = "%doit(X,Y,Z)";


 In this context, nothing in the macro is a keyword. The syntax highlighter does not color keywords in the string "proc freq; run;" even though the string contains SAS keywords that would be colored in open code.


Another possibility is that this macro is actually part of a DATA step. Again, in this usage none of the tokens in the %doit macro are keywords:

data test;

data = 2;

%doit(=, 2, =3)

proc print; run;


In short, macros are just text. As such, they have no context. Without context, there is no way to apply syntax highlighting.  I hope this makes the situation a little clearer.

by Regular Contributor
on ‎05-19-2016 03:50 PM

From a language point of view the content of a macro is just text.  Text can be inserted anywhere, and without context that text can't get color coded.

Thanks, Rick. Going back to my experience in C programming, this makes a lot of sense. You're right; because of unpredictable context, it doesn't make sense to syntax highlight everything.


Correct me if I'm wrong, but I believe %do loops could still be highlighted. Maybe not if they are within a macro, but outside of a macro, aren't they just loops?

by Super User
on ‎05-19-2016 03:57 PM

%do loops have to be inside a macro. Similar with %if / %then constructs.

by SAS Super FREQ
on ‎05-19-2016 03:59 PM

I do not think of a %DO loop as a loop. I think of it as a preprocessor instruction to generate text a certain number of times. The text might change if it depends on the value of the counter macro variable.  The %DO loop merely resolves to some chunk of text, which again has no context.

by PROC Star
on ‎06-02-2016 11:42 PM

We can always argue it is not feasible or a bad idea on theoritical grounds because macros process text, etc.


Yet it is useful, even if the color parser will sometimes get it wrong (it is already confused at times anyway). Better still, it is possible.


I upvoted the sensible suggestion, and here is a (partial) workaround. Just add this dummy macro at the start.


%macro nocolor;

data CLASS;





%macro color; %macro _; %mend _;

data CLASS;
















by Regular Contributor
on ‎06-03-2016 08:43 AM

Thanks, @ChrisNZ, this is fantastic!


I agree that we could argue all day. Maybe this could be an option, so users can always turn it off if they don't like/need it.

by New Contributor AlSherkow
on ‎08-31-2016 10:57 AM

As a work around, you can add this "local" macro variable near the top of your macro and it tricks SAS into thinking the MACRO has ended, and the syntax coloring is turned on. I don't recall where I found this, but I've been using this since 2011. 



%LOCAL a ;%LET a=%NRSTR(%MEND);   %*--to get SAS-Coloring Back;



Al Sherkow

I/S Management Strategies, Ltd.


by New Contributor Gardhi
2 weeks ago



Im sorry, but I find it beside the point, and a little whimsical to argue that the reason is based on how the interpreter works.

We are talking user interface here, you can program that without changing how SAS interprets Macros as text, easily.


My speculations is that this is a case of an endless backlog poorly attended to, and the fact that these comments are being deflected like this is really not appreciated, considering the prices of SAS products.

by Regular Contributor
2 weeks ago

@Gardhi, I stand by the conversation above. While I still agree it would be nice to have this as an option (perhaps off by default), it makes sense logically as whatever is inside a macro isn't necessarily following the same syntax.


For example, I've used macros to contain optional where clauses for proc sql queries. And I've used them to represent functions that get evaluated in multiple places in code. Neither of these things would I want to see syntax highlighted as if they were open code.


I've been using the snippets mentioned above when I want to write and/or debug macro code that could be syntax highlighted. I typically include a link back to this thread or this tip.

by New Contributor Gardhi
2 weeks ago

@paulkaefer I think that seems very sensible, dont get me wrong


I was just a little disgruntled by the presentation of a non-existing constraint, especially seeing how poor programming standards are in SAS Base and that people suffer these work arounds for years.

Idea Statuses
Top Liked Authors