Published on :
Macro NONE

SAS Macro for Capturing and Displaying a Log Section

This code is also available in: Deutsch Español Français
Attention : This code requires administrator privileges.
The `%cliplog` macro is designed to extract a portion of the SAS© log. It takes `marker1` and `marker2` as components of the start marker, `pos` (default 'last') for the search direction, and `file` (default 'c:\test.txt') for the output file path. It temporarily disables the SAS© `mprint` option to prevent its own commands from being printed in the log. Via the Display Manager interface (`dm` command), the macro locates the combined marker (`&marker1&marker2`) in the log (searching backward if `pos=last`), marks this position, moves to the end of the log, and places a second mark there. The text between these two marks (from the found marker to the end of the log) is then copied to the SAS© clipboard and pasted into the specified file. Finally, the `x` command is used to open the resulting text file with the Notepad application. The `mprint` option is restored at the end of the macro's execution.
Data Analysis

Type : NONE


The script neither reads nor creates conventional SAS datasets. It interacts directly with the SAS log to extract text and write it to an external text-format file.

1 Code Block
MACRO DEFINITION
Explanation :
This block defines the `%cliplog` macro. It is designed to capture a section of the SAS log using Display Manager (`dm`) commands. The macro temporarily disables the `mprint` option to prevent its own instructions from being included in the captured log. It locates a string (`marker1` concatenated with `marker2`), marks this position, then marks the end of the log. The content between these two points is copied to the SAS clipboard, then written to the file specified by the `file` parameter. Finally, the `x "notepad &file"` command is used to open the generated file in Notepad, and the `mprint` option is restored.
Copied!
1%macro cliplog(marker1,marker2,pos=last,file=c:\test.txt) ;
2 * note: split search text in half so we dont go and find it in our macro call ;
3 * note: save mprint option since we want mprint turned off for the macro RUN, otherwise
4 we get our search text written to the log and we will find it ;
5 %let o=%sysfunc(getoption(mprint)) ;
6 options nomprint ;
7/* log;
8 find '&marker1&marker2' &pos;
9 rfind;
10 mark;
11 bottom;
12 mark;
13 store;
14 unmark;
15 notepad;
16 clear;
17 paste;
18 file '&file';
19 end
20*/
21 dm "log;find '&marker1&marker2' &pos;mark;bottom;mark;store;unmark;notepad;clear;paste;file '&file';end" ;
22 * view the file in windows notepad ;
23 x "notepad &file" ;
24 options &o ;
25%mend cliplog ;
2 Code Block
USAGE EXAMPLE
Explanation :
This block illustrates the use of the `%cliplog` macro. The line `***BEGIN***;` is a SAS instruction that writes a marker to the log. The comment `/* now run all the SAS code you would like to capture */` indicates where to insert the code whose log content is to be captured. The call `%cliplog(***BEGIN,***);` executes the macro to capture the log. The macro will search for the first occurrence of `***BEGIN***` (searching backward if `pos=last`), mark this position, then the end of the log, and copy the text between these two points.
Copied!
1***BEGIN***;
2/* this marks where to start the copying from the log */
3/* now
4run all the SAS code you would like to capture */
5%cliplog(***BEGIN,***) ;
6/* finally we call the macro which captures the log from the point we previously marked */
This material is provided "as is" by We Are Cas. There are no warranties, expressed or implied, as to merchantability or fitness for a particular purpose regarding the materials or code contained herein. We Are Cas is not responsible for errors in this material as it now exists or will exist, nor does We Are Cas provide technical support for it.