![]() | ![]() | ![]() |
Sooner or later every SAS programmer faces the irritating problem of running only a portion of an existing SAS program. If you're a craftsman with respect to your code, you can't use bracket-style comments ( /* .... */ ) to block segments of code from execution because you've already used them to document your program and bracket-style comments can't be nested. For example, we all know what would happen if we tried to use bracket-style comments to prevent the following data step from being executed.
/*
DATA SALARY ;
/* ASSIGN COMMISSION RATES */
more SAS statements
RUN ;
*/
|
The ‘*/’ after the word ‘RATES’ would end the comment which began on the first line so the statements after ‘RATES */’ would generate an error. Clearly, not the intended result.
Comment statements ( *….. ; ) will do the job but, except in small programs, they’re too impractical to use. It’s simply too tedious to put an asterisk in front of a lot of SAS statements and remove them when you’re done.
What we really need is a SKIP statement. Like a comment statement, a SKIP statement would block lines of SAS code from being compiled and executed by the SAS Supervisor. But unlike statement-style comments, the SKIP statement would hide multiple SAS statements from the SAS Supervisor. And unlike bracket-style comments, SKIP statements could be nested.
You'll be pleased to know that this capability already exists in the SAS language not as an undocumented feature, just an unimagined one. And as the following example shows, you can easily code one yourself.
SAS statements you want to execute.....
%macro SKIP ;
SAS statements you want to skip.....
%mend SKIP ;
SAS statements you want to execute.....
|
This tip works by asking the SAS macro facility to store the portion of code that you don't want to execute as a SAS macro. If the 'SKIP' macro is never called in the program, it is never passed to the SAS Supervisor to be compiled and executed.
This technique prevents any SAS statements from being compiled and executed: lines within a DATA or PROC step, entire DATA and/or PROC steps, and definitions of other macros.
This technique is extremely flexible and easy to use. First, ‘SKIP’ statements can be repeated:
SAS statements you want to execute.....
%macro SKIP ;
SAS statements you want to skip.....
%mend SKIP ;
SAS statements you want to execute.....
%macro SKIP ;
More SAS statements you want to skip.....
%mend SKIP ;
SAS statements you want to execute.....
|
In this example, the second ‘SKIP’ macro definition simply replaces the first definition of the ‘SKIP’ macro. ‘SKIP’ statements can also be nested:
SAS statements you want to execute.....
%macro SKIP ;
SAS statements you want to skip.....
%macro SKIP ;
More SAS statements you want to skip.....
%mend SKIP ;
%mend SKIP ;
SAS statements you want to execute.....
|
In this example, the second ‘SKIP’ macro is defined in the local referencing environment of the first ‘SKIP’ macro. The first ‘SKIP’ macro definition is defined in the global referencing environment. The practical point to notice in both of these examples is that you don't have to assign different names (e.g., 'SKIP1,' 'SKIP2,' etc.) to each ‘SKIP’ macro. Also, you can give the ‘SKIP’ macro any name you want. I like ‘SKIP’ because it’s descriptive and looks a lot better than ‘JUNK’ or ‘OOPS’.
If the code segment you want to skip is long, you may want to use the NOSOURCE option to prevent the skipped code from being printed to your SAS log.
Options Nosource;
%macro SKIP ;
SAS statements you want to skip.....
%mend SKIP ;
Options Source;
SAS statements you want to execute.....
|
Anyone who has inadvertently omitted a %MEND statement at the conclusion of one of their macro definitions knows how effective this trick is. In fact, that's how I "discovered" it.
In addition to learning how to code this 'SKIP' macro, I hope you have also seen the importance of learning from your mistakes (even the "little" ones like forgetting a %MEND statement) and the role of imagination in putting what you've learned to good use.
Note: Don't confuse this tip with the SKIP statement. The SKIP statement generates blank lines in the SAS log. For more information, refer to the Statements chapter of the SAS 9.1 Language Reference: Dictionary, available from the SAS Onlinedoc.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
| Type: | Sample |
| Topic: | SAS Reference ==> Macro |
| Date Modified: | 2005-09-07 03:02:55 |
| Date Created: | 2004-11-10 15:47:46 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | All | n/a | n/a |


