01-30-2015 04:12 PM
I am trying to figure the way to properly put comments within macros. But I am trying to do it as a trick while working my way through Cody's Collection of Popular SAS Programming Tasks. I am basically going through the book, example by example, and learning a lot as I go along. As part of this I have a set of syntax saved in my Enterprise Guide. I do not delete the previous code, but I use a "trick" that Marty Hultgren showed to me a few weeks ago. Specifically by putting %macro SKIP; at the beginning of the code then a %mend; at the end it skips over all of the previous code I had written in SAS and starts fresh with the new code at the bottom. It is sort of like a grand way of commenting out large blocks of code.
Some of the newer examples in the Cody book have the reader try a few different ways of accomplishing simple tasks. Specifically I am looking at ways of combining summary information with detail data - sort of appending a grand mean to each individual record. I can do it via PROC MEANS or with PROC SQL. I commented out the previous way that I did it while working through the code. I am including a picture of what I did and the associated warning. Overall it gives a message about the quoted string being more than 262 characters. I did some investigating and there are varying rules for including comments within a macro. Below is a link to the technical support with SAS:
But overall I cannot figure what I am doing wrong. This warning has come about previously for me - the solution I have always used in the past is to shut SAS down and get into it again, but it is not working this time. Thank you very much in advance.
01-30-2015 11:50 PM
1) Your problem is unbalanced quotes. DO NOT use unbalanced quotes. That means don't use don't (unless you use it in pairs). You can get away with them in a statement style comment in open code, but inside of a macro they cause trouble.
2) If you are using EG then the equivalent of re-starting SAS is to disconnect and re-connect your Server Session. Someone more familiar with EG can explain.
As to the general question of comments in macros. Personally I use statement style comments for comments that I want to appear in the LOG when MPRINT is turned on. I make each such comment its own line starting * and ending with ; so that when generated by MPRINT it is still readable. The comments are BEFORE the code that they are referencing. Putting them at the end of the line of code makes the MPRINT lines look backwards.
For comments that are really for the person editing the program (not running the program and reading the log) I use MACRO comments. Start the comment with %* and end it with ; . You can make these longer than a line since they will not be emitted by MPRINT.
Rarely I will use PL/1 style comments (/* ... */). There are two situations where they are needed.
1) when the comment needs to be in the middle of a statement, especially a macro statement.
2) when the comment needs to include example lines of code with semi-colons and other macro triggers.
02-02-2015 03:25 PM
It turns out that a few parts were messing me up. First, I had an apostrophe within my macro for a comment within there. That sort of threw things off. No issues of unbalanced quotes.
I actually thought for a while that because I had such a large macro it was interpreting this as a very long text string of sort. That was also not true.
I also was using statement-style comments within the macro - but these do not work, at least under tricky conditions. Only %* as well as /* ... */ work within macros.
Lastly, the good SAS rep recommended %mend; being done repeatedly rather than re-booting SAS if it gets hung up.
02-02-2015 08:19 PM
Statement style comments are actually more useful in macros than in open code. In open code all comments will appear in the log, But in a macro only the statement style comments will appear in the log with the statements when MPRINT is turned on. Just don't use unbalanced quotes in the comments.
The only time I have seen statement style comments cause trouble in macros is when attempting to use RSUBMIT blocks to submit macro definitions.
02-04-2015 01:33 PM
Tom, remember the scope of a macro. When using rsubmit blocks those are operating in a different machine having a different scope. During macro compilation its hardly noticed but during macro execution it makes a big difference as you are back at global level and the first macro definition in a macro for that part is starting at level 1 again. Not being nested as you could think of seeing some coding.
02-04-2015 03:52 PM
That can get complicated, but the problem I have seen is internal to SAS. It just does the wrong thing. It always looked to me like the process of moving the code from the one session to the remote session was causing tokens to get broken in the middle.
02-04-2015 04:16 PM
It did not get too complicated when following that scope logic. It was not well documented in the old versions, recently seen this.
SAS(R) 9.4 Macro Language: Reference, Third Edition %SYSRPUT Statement - Example: Checking the Value of a Return Code on a Remote Host
That rsubmit/endrsubmit is now easilty to see as part of a grand macro process. Those rsubmit / endrsubmit can be made conditional so the code can run balanced remote or as whole local. Using the %syslput %sysrput for synching macrovars and not the %let or %put (global ones) as those are timed wrong. Getting compiled local where the intention was being run remote. When embedded in that remote macro there is no scope issue anymore. Also you cannot get hurt what you would see as broken tokens.
some things with quoting cahllenges still remains.
01-31-2015 03:11 AM
It is about tokens: SAS(R) 9.4 Macro Language: Reference, Third Edition. And that token processing as given is not always the same as the color highlighting.
Need further help from the community? Please ask a new question.