Macro Debugging Options:MPRINT, MLOGIC, SYMBOLGEN, MACROGEN, MFILE

Macro Debugging Options:

Debugging a macro code isn’t easy process. It is difficult to identify the problem in the SAS code just by seeing the ERROR message in the LOG file. It is lot easier to debug a macro if we use the following SAS options.

There are four system options that are helpful in debugging SAS Macros:

SYMBOLGEN, MPRINT, MLOGIC, and MFILE.
Each option adds different information to the SAS LOG File.

Like any other SYSTEM OPTIONS, you turn these on using the OPTIONS statement:

Options macrogen symbolgen mlogic mprint mfile;
You can turn them off by specifying the following OPTIONS statement:

Options nomacrogen NoSymbolgen nomlogic nomprint nomfile;


Both statements are needed in side SAS. Once you set any option and it will remain in effect throught the SAS session. So if you want to debug the macro use first OPTIONs Statemnt else use 2nd one.

Let’s look at each option, and the output they produce in more detail…


MPRINT:As we know that when we execute a macro code SAS doesn’t display it in the LOG file, but using the MPRINT option displays all the SAS statements of the resolved macro code.

MPRINT option prints one statement per line along with resolved macro code.

LOG FILE:
31 %macro concat;
32 data all;
33 set
34 %do i = 1 %to 4;
35 dsn&i
36 %end;
37 ;
38 run;
39 %mend;
40 %concat;
MPRINT(CONCAT): data all;
MPRINT(CONCAT): set dsn1 dsn2 dsn3 dsn4 ;


NOTE: There were 6 observations read from the data set WORK.DSN1.
NOTE: There were 6 observations read from the data set WORK.DSN2.
NOTE: There were 6 observations read from the data set WORK.DSN3.
NOTE: There were 6 observations read from the data set WORK.DSN4.
NOTE: The data set WORK.ALL has 24 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.06 seconds

MPRINT(CONCAT): run;

Note: Even though we wrote the macro code, the MPRINT option prints the resolved macro code into the LOG file as seen above.


MLOGIC:This option is very helpful when we deal with nested macros (Macro inside another macro). Often we use %DO loops and or %IF-%THEN-%ELSE statements inside the macro code and LOGIC option will display how the macro variable resolved each time in the LOG file as TRUE or FALSE .

To be more specific, MLOGIC option identifies and displays the macro logic. It even follows the macro execution pattern.

LOG FILE:
31 %macro concat;
32 data all;
33 set
34 %do i = 1 %to 4;
35 dsn&i
36 %end;
37 ; /* this additional ';' is necessary, the first ';' is for
38 the "%end", while the second ';' is for "set " */
39 run;
40 %mend;
41
42 %concat;
MLOGIC(CONCAT): Beginning execution.
MLOGIC(CONCAT): %DO loop beginning; index variable I; start value is 1; stop value is 4; by value is
1.
MLOGIC(CONCAT): %DO loop index variable I is now 2; loop will iterate again.
MLOGIC(CONCAT): %DO loop index variable I is now 3; loop will iterate again.
MLOGIC(CONCAT): %DO loop index variable I is now 4; loop will iterate again.
MLOGIC(CONCAT): %DO loop index variable I is now 5; loop will not iterate again.
NOTE: There were 6 observations read from the data set WORK.DSN1.
NOTE: There were 6 observations read from the data set WORK.DSN2.
NOTE: There were 6 observations read from the data set WORK.DSN3.
NOTE: There were 6 observations read from the data set WORK.DSN4.
NOTE: The data set WORK.ALL has 24 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.06 seconds

MLOGIC(CONCAT): Ending execution.

SYMBOLGEN:Often we use multiple ampersands (ex: &&dsn.&i) and SYMBOLGEN option prints the message in the LOG file about how the macro variable is resolved.

To be more specific, it prints message in the LOG whenever a macro variable get resolved.

LOG FILE:
31 %macro concat;
32 data all;
33 set
34 %do i = 1 %to 4;
35 dsn&i
36 %end;
37 ; /* this additional ';' is necessary, the first ';' is for
38 the "%end", while the second ';' is for "set " */
39 run;
40 %mend;
41
42 %concat;
SYMBOLGEN: Macro variable I resolves to 1
SYMBOLGEN: Macro variable I resolves to 2
SYMBOLGEN: Macro variable I resolves to 3
SYMBOLGEN: Macro variable I resolves to 4


NOTE: There were 6 observations read from the data set WORK.DSN1.
NOTE: There were 6 observations read from the data set WORK.DSN2.
NOTE: There were 6 observations read from the data set WORK.DSN3.
NOTE: There were 6 observations read from the data set WORK.DSN4.
NOTE: The data set WORK.ALL has 24 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.06 seconds


MFILE:
MFILE is useful when we want to create a newfile with the resolved macro code.
To create a newfile you have to specify FILENAME statement as follows:

Filename mprint ‘C:\Users\Sarath Annapareddy\Desktop\macroresol.sas’;
Options mprint mfile;


Note: Whenever the macro code executes, the resultant resolved macro code will be written to the macroresol.sas file.

LOG FILE:

31 %macro concat;
32 data all;
33 set
34 %do i = 1 %to 4;
35 dsn&i
36 %end;
37 ; /* this additional ';' is necessary, the first ';' is for
38 the "%end", while the second ';' is for "set " */
39 run;
40 %mend;
41
42 %concat;
NOTE: The macro generated output from MPRINT will also be written to external file C:\Users\Sarath
Annapareddy\Desktop\macroresol.sas while OPTIONS MPRINT and MFILE are set.

NOTE: There were 6 observations read from the data set WORK.DSN1.
NOTE: There were 6 observations read from the data set WORK.DSN2.
NOTE: There were 6 observations read from the data set WORK.DSN3.
NOTE: There were 6 observations read from the data set WORK.DSN4.
NOTE: The data set WORK.ALL has 24 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.06 seconds

macroresol.sas file.

data all;
set dsn1 dsn2 dsn3 dsn4 ;
run;


MACROGEN:

Option MACROGEN will turn on the macro expansion and is necessary when you use macros.


MPRINTNEST and MPRINTLOGIC:These options are available with SAS v9.0. These options can be useful when you use NESTED macros. You will see more information in the LOG file than what you usually see with MPRINT and MLOGIC options combine.

Both these options require the use of MPRINT and MLOGIC respectively.

Example code used here:

options macrogen mlogic mprint symbolgen mfile;
filename mprint 'C:\Users\Sarath Annapareddy\Desktop\macroresol.sas';

data dsn1;
input a @@;
cards;
1 2 3 5 6 7
;
run;

data dsn2;
input a @@;
cards;
4 5 6 9 8 6
;
run;

data dsn3;
input a @@ ;
cards;
21 22 23 24 25 26
;
run;

data dsn4;
input a @@;
cards;
10 11 12 13 14 15
;
run;

*Concatenating all the 4 datasets using a macro with the %DO LOOP;%macro concat;
data all;
set
%do i = 1 %to 4;
dsn&i
%end;
;
run;
%mend;

%concat;


  • LOG FILE:
Debugging Techniques:

Here are few techniques to debug a macro:

Check if %macro-%mend, %DO-%END and %IF-%THEN-%ELSE are used correctly in the code.
Check whether single or double quotes used for the macro variables.
Check for balancing of quotes.
Check whether you have used %LOCAL and or %GLOBAL in appropriate places.
Check the macro variable resolution using the %PUT statement whenever required.
Use SAS debugging options MACROGEN, MPRINT, MLOGIC and SYMBOLGEN
to make sure the macro code is executed as expected.


31 %macro concat;
32 data all;
33 set
34 %do i = 1 %to 4;
35 dsn&i
36 %end;
37 ; /* this additional ';' is necessary, the first ';' is for
38 the "%end", while the second ';' is for "set " */
39 run;
40 %mend;
41
42 %concat;

MLOGIC(CONCAT): Beginning execution.
MPRINT(CONCAT): data all;
NOTE: The macro generated output from MPRINT will also be written to external file C:\Users\Sarath
Annapareddy\Desktop\macoresol.sas while OPTIONS MPRINT and MFILE are set.
MLOGIC(CONCAT): %DO loop beginning; index variable I; start value is 1; stop value is 4; by value is
1.
SYMBOLGEN: Macro variable I resolves to 1
MLOGIC(CONCAT): %DO loop index variable I is now 2; loop will iterate again.
SYMBOLGEN: Macro variable I resolves to 2
MLOGIC(CONCAT): %DO loop index variable I is now 3; loop will iterate again.
SYMBOLGEN: Macro variable I resolves to 3
MLOGIC(CONCAT): %DO loop index variable I is now 4; loop will iterate again.
SYMBOLGEN: Macro variable I resolves to 4
MLOGIC(CONCAT): %DO loop index variable I is now 5; loop will not iterate again.
MPRINT(CONCAT): set dsn1 dsn2 dsn3 dsn4 ;
MPRINT(CONCAT): run;

NOTE: There were 6 observations read from the data set WORK.DSN1.
NOTE: There were 6 observations read from the data set WORK.DSN2.
NOTE: There were 6 observations read from the data set WORK.DSN3.
NOTE: There were 6 observations read from the data set WORK.DSN4.
NOTE: The data set WORK.ALL has 24 observations and 1 variables.
NOTE: DATA statement used (Total process time):
real time 0.06 seconds
cpu time 0.06 seconds


MLOGIC(CONCAT): Ending execution.

Comments

  1. Thank you so much for the valuable information presented in an excellent manner.

    ReplyDelete
  2. Nice overview, nice structure of text, nice collection of information.
    Thank you very much.

    ReplyDelete
  3. Really it will help full information.
    Thank you so much

    ReplyDelete

Post a Comment

Popular posts from this blog

SAS Interview Questions and Answers: CDISC, SDTM and ADAM etc

Comparing Two Methods for Removing Formats and Informats in SAS: DATA Step vs. PROC DATASETS

Studyday calculation ( --DY Variable in SDTM)