You are on page 1of 8

Debugging Options: Debugging a macro code isnt 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. Lets look at each option, and the output they produce in more detail

MPRINT:As we know that when we execute a macro code SAS doesnt 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: NOTE: NOTE: NOTE: NOTE: NOTE: There were 6 observations read from the data set WORK.DSN1. There were 6 observations read from the data set WORK.DSN2. There were 6 observations read from the data set WORK.DSN3. There were 6 observations read from the data set WORK.DSN4. The data set WORK.ALL has 24 observations and 1 variables. 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

How to create Global or Local macro variables: (%Global / %Local)


Global Macro Variables:The global macro variable gets created during the initialization of a SAS session and its get deleted at the end of the session. Global macro variable cane be created with a %LET statement (used outside a macro definition) a DATA step that contains a SYMPUT routine a DATA step that contains a SYMPUTX routine (New with SAS 9) a SELECT statement that contains an INTO clause in PROC SQL a %GLOBAL statement. Global macro variables include the following: *all automatic macro variables except SYSPBUFF. See Automatic Macro Variables for more information about SYSPBUFF and other automatic macro variables. *macro variables created outside of any macro. *macro variables created in %GLOBAL statements. See Creating Global Macro Variables for more information about the %GLOBAL statement. *most macro variables created by the CALL SYMPUT routine. See Special Cases of Scope with the CALL SYMPUT Routine for more information about the CALL SYMPUT routine. You can create global macro variables any time during a SAS session or job. Except for some automatic macro variables, you can change the values of global macro variables any time during a SAS session or job. In most cases, once you define a global macro variable, its value is available to you anywhere in the SAS session or job and can be changed anywhere. So, a macro variable referenced inside a macro definition is global if a global macro variable already exists by the same name (assuming that the variable is not specifically defined as local with the %LOCAL statement or in

a parameter list). The new macro variable definition simply updates the existing global one. The following are exceptions that prevent you from referencing the value of a global macro variable: When a macro variable exists both in the global symbol table and in the local symbol table, you cannot reference the global value from within the macro that contains the local macro variable. In this case, the macro processor finds the local value first and uses it instead of the global value. If you create a macro variable in the DATA step with the SYMPUT routine, you cannot reference the value with an ampersand until the program reaches a step boundary.
Source: SAS Documentation, SAS(R) 9.2 Macro Language: Reference

Local Macro Variables: A local symbol table is created when a macro that includes a parameter list is called or when a request is made to create a local variable during macro execution. The local macro variable gets deleted when the macro finishes its execution. Local macro variables cane be created with the parameters in a macro definition a %LET statement within a macro definition a DATA step that contains a SYMPUT routine within a macro definition a DATA step that contains a SYMPUTX routine within a macro definition (New with SAS 9) a SELECT statement that contains an INTO clause in PROC SQL within a macro definition a %LOCAL statement. Note: The SYMPUT routine and the SYMPUTX routine can only create a local macro variable if a local symbol table already exists. If no local symbol table exists when the SYMPUT routine orSYMPUTX routine executes, it will create a global macro variable. The SYMPUTX routine can also create the local table by setting the third argument to 'L' as shown here: %macro test; data _null_; set sashelp.class(where=(sex='M')); call symputx('gender',sex,'l'); run; %put _user_; %mend test; %test Macro Basics:

maxvarlen_macro: Check the Length of all character variables length is LT 200


MAXVARLEN Macro: According to FDA released the Guidance for Industry: Providing Regulatory Submission in Electronic Format NDAs which was released in Jan 1999, one of the important point to comply with the agencys regulations is : The length of any character values cannot be more than 200 characters. Here is the macro which will give us the list of character variables whose actual length (not the assigned) is greater than 200. You can use this program to check the maximum length of all variables of any dataset at once. This macro will create a table with list of variables and their

length. ********************************************************************** *; *** Program: macro_maxvarlen.sas ***; *** Version: 1.0 ***; *** Client: ***; *** Protocol: ***; *** Programmer: sarath Annapareddy ***; *** Date: 22APR2009 ***; *** Purpose: Macro used to check the list of variables whose ***; *** length is GT 200. ***; *** ***; ******************************************************************* ***;libname prodn 'H:\Company\Client\Testing\XXXX\XXXXX\Test map datasets'; option nofmterr; %macro maxvarlen(base,dsn);data dsn; set sashelp.vcolumn(where=(libname=upcase("&base") and memname=upcase("&dsn") and type='char')); call symput(cats("var",_n_),name); id=_n_; keep name id; run; *Counting number of character variables in the dataset; proc sql noprint; select count(*)into :nvars from dsn; quit; *Computing max. length of each character variable in the dataset; %do i = 1 %to &nvars; proc sql noprint; create table maxlen&i as select max(length(&&var&i)) as mlen from &base..&dsn quit; %end; *Concatenating all the datasets; data final; id=_n_; set %do i= 1 %to &nvars; maxlen&i %end; ; run; %mend; %maxvarlen(prodn,ae); *Final dataset with list of variables whose length is GT 200;proc sql; create table mxvarlen as select a.name ,b.mlen from dsn as a, final as b where a.id=b.id and mlen gt 200; quit;

You might also like