- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi folks,
I ran into this user's problem today: https://communities.sas.com/t5/SAS-Programming/Apostrophe-in-a-comment-in-a-macro-why-after-40-years...
TL;DR: I got the following error when attempting to run a large block of code encompassed within a macro because I put an apostrophe (I used the word "it's"...) in one of my comments within that block of code: "The quoted string currently being processed has become more than 262 characters long. You might have unbalanced quotation marks."
I certainly didn't have reason to be as miffed as the user referenced in the link above (I only wasted about half an hour or so...), but I was wondering what the pitfalls were of just always enclosing comments with /*comment*/, particularly within a block of code encompassed within a macro, in order to avoid this issue. I'm not a seasoned SAS programmer by any means, so I know there are some things I'm not thinking of.
Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
This is a useful explanation of comments inside macros.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Depends on how you code. Take this program:
data a;
x=5; /* set x to 5 */
y=3;
run;
And imagine that it were a lot larger and more complicated.
Now, you want to temporarily remove some line(s) of code from the program, so you do this:
data a;
/*
x=5; /* set x to 5 */
*/
y=3;
run;
(in the example, we now only want the Y variable). But this last bit of code will not work as intended, as SAS does not understand nested comments. So the code between DATA and RUN will be interpreted as two comments, the first is
/*
x=5; /* set x to 5 */
and the second is an asterix-type comment
*/
y=3;
Meaning that using "*" and "%*" comments inside the code can make it a lot easier to comment out unneeded code.
But you are right, there are also problems with that comment style, e.g. yours with the unmatched quotes. So it becomes a matter of preferences and convenience, until (perhaps) SAS some day starts supporting nested /*...*/ comments.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Yes--I've had that issue with nested comments before 😕
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
The following worked for me now for many years:
- I use principally only /* .... */ for comments.
- I never mix code and comments on the same line. A line contains either code or comment but never both.
- If I have to comment larger code blocks during unit testing and debugging I just wrap it into a macro like: %macro null; ....all the code I don't want to run.... %mend;
This avoids any challenges with commenting code blocks that already have lines with comments.
The only two cases I'm aware of where you have to be watchful using /*....*/
- With SAS on a Mainframe (z-OS) don't start /* on the first column as this means "end of program" and nothing below will execute.
- Oracle uses the following syntax for hints: /* <hint> */
For explicit pass-through Oracle SQL one can either have SAS style comments or Oracle hints. If you want it to be Oracle hints then use connection option PRESERVE_COMMENTS. Else SAS will remove anything in /* ... */ as SAS comment prior to sending the SQL to Oracle for execution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks! I didn't consider the "%macro null; [...] %mend;" option. Might use that for the application you're describing here.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
With SAS on a Mainframe (z-OS) don't start /* on the first column as this means "end of program" and nothing below will execute.
That's only when you put the code right into the JCL, using "//SYSIN DD *". If you put your program in another dataset or member, e.g. "//SYSIN DD DSN=ASF13.DIV.SASPROG(PROG1)", you will not have that problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
I think /* comments are generally a good idea. I tend to use a mix of * comments and /* comments. And I basically avoid typing apostrophes into comments (even though they are 'safe' in /* comments, I feel like avoiding unmatched quotes in comments is a good 'rule'.
One factor to consider in deciding for * vs /* comments in a macro setting is whether you want the comment to be seen in the log when the macro executes (assuming MPRINT is turned on).
If you use a * comment in a macro, the macro language will generate the * comment when the macro executes (because * is not a comment in the macro language):
If you use a /* comment in a macro (or a %* comment), it is a macro language comment, and will not be generated when the macro executes.
1 %macro test1() ; 2 data want ; 3 x=1 ; *Show this comment in MPRINT ; 4 run ; 5 %mend ; 6 7 %test1() MPRINT(TEST1): data want ; MPRINT(TEST1): x=1 ; MPRINT(TEST1): *Show this comment in MPRINT ; MPRINT(TEST1): run ; NOTE: The data set WORK.WANT has 1 observations and 1 variables. 8 9 %macro test2() ; 10 data want ; 11 x=1 ; /*Do not show this comment in MPRINT */ 12 run ; 13 %mend ; 14 15 %test2() MPRINT(TEST2): data want ; MPRINT(TEST2): x=1 ; MPRINT(TEST2): run ; NOTE: The data set WORK.WANT has 1 observations and 1 variables.
Next up: SAS Trivia Quiz hosted by SAS on Wednesday May 21.
Register now at https://www.basug.org/events.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for the detailed response!