DEBUGGING MACRO ERRORS:
Checking for errors and eliminating errors from program is called debugging
Macro Debugging:- Checking for errors in macro program to avoid errors.
How we can make a Bug free Macro Program
-> Write a standard program without bugs
-> Add your %MACRO and %MEND statements for standard sas program
-> add the macro logic one feature at a time
-> add your macro variables, one at a time, and so on, until your macro is complete and
bug-free
Macro Debugging Options
NOMERROR|MERROR
NOSERROR|SERROR
NOMLOGIC|MLOGIC
NOMPRINT|MPRINT
NOSYMBOLGEN|SYMBOLGEN
MERROR | NOMERROR
when this option is on, SAS will issue a warning if you invoke a macro that SAS cannot find.
OPTIONS MERROR;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &d2;
Where &cond;
Run;
%Mend;
%Mac1(ds1,sashelp.class,%str(sex=’F’));
%Mac1(ds2,sashelp.class,%nrstr(age>=14));
WARNING: Apparent invocation of macro MAC1 not resolved.
Why because we created a Macro called MAC11 but while invoking we specified Mac1.
So use %Mac11 while invoking a macro.
Check below program and see the log there should be no Warning because MERROR option is turned off.
OPTIONS NOMERROR;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &d2;
Where &cond;
Run;
%Mend;
%Mac1(ds1,sashelp.class,%str(sex=’F’));
%Mac1(ds2,sashelp.class,%nrstr(age>=14));
——–Correct Program——-
OPTIONS MERROR;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &d2;
Where &cond;
Run;
%Mend;
%Mac11(ds1,sashelp.class,%str(sex=’F’));
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
SERROR | NOSERROR
when this option is on, SAS will issue a warning if you use a macro variable that SAS cannot find.
OPTIONS SERROR;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &ds2;
Where &cond;
Run;
%Mend;
%Mac11(ds1,sashelp.class,%str(sex=’F’));
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
WARNING: Apparent symbolic reference DS2 not resolved.
Why because we created a Macro variable called d2 but while resolving we specified ds2
So use &d2 while resolving a macro variable.
Check below program and see the log there should be no Warning because SERROR option is turned off.
OPTIONS NOSERROR;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &ds2;
Where &cond;
Run;
%Mend;
%Mac1(ds1,sashelp.class,%str(sex=’F’));
%Mac1(ds2,sashelp.class,%nrstr(age>=14));
——–Correct Program——-
OPTIONS SERROR;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &d2;
Where &cond;
Run;
%Mend;
%Mac11(ds1,sashelp.class,%str(sex=’F’));
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
MLOGIC | NOMLOGIC
when this option is on, SAS prints in your log details about the execution of macros and Macro variables.
OPTIONS MLOGIC;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &d2;
Where &cond;
Run;
%Mend;
%Mac11(ds1,sashelp.class,%str(sex=’F’));
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
MLOGIC(MAC11): Beginning execution.
MLOGIC(MAC11): Parameter D1 has value ds1
MLOGIC(MAC11): Parameter D2 has value sashelp.class
MLOGIC(MAC11): Parameter COND has value _sex_’F’_
NOTE: There were 9 observations read from the data set SASHELP.CLASS.
WHERE sex=’F’;
NOTE: The data set WORK.DS1 has 9 observations and 5 variables.
MLOGIC(MAC11): Ending execution.
149 %Mac11(ds2,sashelp.class,%nrstr(age>=14));
MLOGIC(MAC11): Beginning execution.
MLOGIC(MAC11): Parameter D1 has value ds2
MLOGIC(MAC11): Parameter D2 has value sashelp.class
MLOGIC(MAC11): Parameter COND has value _age__14_
NOTE: There were 9 observations read from the data set SASHELP.CLASS.
WHERE age>=14;
NOTE: The data set WORK.DS2 has 9 observations and 5 variables.
If you don’t want to prints in your log, details about the execution of macros and macro variables
OPTIONS NOMLOGIC;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &d2;
Where &cond;
Run;
%Mend;
%Mac11(ds1,sashelp.class,%str(sex=’F’));
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
MPRINT | NOMPRINT
when this option is on, SAS prints in your log the standard SAS code generated by macro processor.
OPTIONS MPRINT;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &d2;
Where &cond;
Run;
%Mend;
%Mac11(ds1,sashelp.class,%str(sex=’F’));
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
%Mac11(ds1,sashelp.class,%str(sex=’F’));
MPRINT(MAC11): Data ds1;
MPRINT(MAC11): Set sashelp.class;
MPRINT(MAC11): Where sex=’F’;
MPRINT(MAC11): Run;
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
MPRINT(MAC11): Data ds2;
MPRINT(MAC11): Set sashelp.class;
MPRINT(MAC11): Where age>=14;
MPRINT(MAC11): Run;
If you don’t want to prints in your log, details about the standard SAS code generated by macros.
OPTIONS NOMPRINT;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &d2;
Where &cond;
Run;
%Mend;
%Mac11(ds1,sashelp.class,%str(sex=’F’));
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
SYMBOLGEN | NOSYMBOLGEN
when this option is on, SAS prints in your log the values of macro variables.
OPTIONS SYMBOLGEN;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &d2;
Where &cond;
Run;
%Mend;
%Mac11(ds1,sashelp.class,%str(sex=’F’));
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
%Macro Mac11(d1, d2, cond) ;
%Mac11(ds1,sashelp.class,%str(sex=’F’));
SYMBOLGEN: Macro variable D1 resolves to ds1
SYMBOLGEN: Macro variable D2 resolves to sashelp.class
SYMBOLGEN: Macro variable COND resolves to sex=’F’
SYMBOLGEN: Some characters in the above value which were subject to macro quoting have been unquoted for printing.
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
SYMBOLGEN: Macro variable D1 resolves to ds2
SYMBOLGEN: Macro variable D2 resolves to sashelp.class
SYMBOLGEN: Macro variable COND resolves to age>=14
SYMBOLGEN: Some characters in the above value which were subject to macro quoting have been unquoted for printing.
If you don’t want to prints in your log, details about the values of macro variables.
OPTIONS NOSYMBOLGEN;
%Macro Mac11(d1, d2, cond) ;
Data &d1;
Set &d2;
Where &cond;
Run;
%Mend;
%Mac11(ds1,sashelp.class,%str(sex=’F’));
%Mac11(ds2,sashelp.class,%nrstr(age>=14));
While you want the MERROR and SERROR options to be on at all times, you will probably
want to turn on MLOGIC, MPRINT, and SYMBOLGEN one at a time.