Thursday, November 18, 2010

Funky behavior with * ; comments in macros

SAS supports several types of comments:

/* comment */
* comment ;

where the asterisk in the second case must be the first character of the line. There is a key difference unknown to most SAS programmers, including me until today, that the interpreter parses through the second style but not the first. This usually isn't a big issue, unless it is embedded in a macro in which case it can give truly strange behavior. For example, this macro has a * ; style comment preceding a data step:

%macro crap;
%local v d;
%let v = somemacro;
%let d = somedataset;
*%put This line is actually parsed and displayed to the log, v = &v;
data &d;
variable_v = "&v";

This macro actually returns an error when submitted! Here's the log:

This line is actually parsed and displayed to the log, v = somemacro
NOTE: Line generated by the invoked macro "CRAP".
115 data &d; variable_v = "&v";

ERROR 180-322: Statement is not valid or it is used out of proper order.

However, submitting lines 2-7 of the macro in open code works fine, as it should:

NOTE: The data set WORK.SOMEDATA has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.58 seconds
cpu time 0.00 seconds

In this case the %put statement wasn't executed either. Strange. I discovered the strange macro behavior with the error that shouldn't be an error today while working. Imagine my frustration. "No it isn't an error!" "Why does it keep returning an error?!" Thanks SAS, I really could have used that half hour it took me to figure out what was going wrong for something else. Like this blog entry. WTF?!