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.


Idea Statuses
Top Liked Authors