SAS-Macro Variable Values in SAS LOG

OPTION SYMBOLGEN & %PUT STATEMENT



Often when you use a macro variable in code, unless you have used proper options at the top of the code, it generally doesn’t show up in the log i.e. the value it is being resolved to doesn’t get displayed. When you are absolutely sure of the value that will be taken by the variable then you can probably ‘not care’ about displaying the macro value in the log. So how do display the macro value in log: There are two easy ways of doing this: One would be using the Option symbolgen and other is using the %Put statement.
1.       1)Option SYMBOLGEN-


General form of this option is nothing but: OPTIONS NOSYMBOLGEN | SYMBOLGEN;


NOSYMBOLGEN, also the default set option, tells SAS that log messages about macro variable references will not be displayed and SYMBOLGEN specifies that log messages about macro variable references will be displayed. When the SYMBOLGEN option is turned on, SAS writes a message to the log for each macro variable that is referenced in your program. The message states the macro variable name and the resolved value. Note: Remember that since SYMBOLGEN is a system option, its setting remains in effect until you modify it or until you end your SAS session.


Example


DATA NAMES_DATA;
INPUT ID NAME $5. AGE;
CARDS;
1 DAVID 37
2 JOHN 45
;
RUN;

OPTIONS SYMBOLGEN;
%MACRO DEFINE_NAME();
%DO ID=1 %TO 2;
PROC SQL;
SELECT NAME INTO:NAME_VAR FROM NAMES_DATA WHERE ID=&ID.;
QUIT;

DATA &NAME_VAR.;
NAME="&NAME_VAR";
RUN;
%END;
%MEND;

%DEFINE_NAME();

Here I have a dataset which has ID, Names and Age.  I simply run a do loop to form datasets which are nothing but names being picked up from the dataset Names_data. Here I would like to see the names being assigned. With Symbolgen we get to see the same .
Here is the SAS log that shows the messages that are generated by the SYMBOLGEN option for this code.


LOG:


SYMBOLGEN:  Macro variable ID resolves to 1
NOTE: Writing HTML Body file: sashtml10.htm
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.06 seconds
      cpu time            0.03 seconds


SYMBOLGEN:  Macro variable NAME_VAR resolves to DAVID
SYMBOLGEN:  Macro variable NAME_VAR resolves to DAVID

NOTE: The data set WORK.DAVID has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.03 seconds


SYMBOLGEN:  Macro variable ID resolves to 2
NOTE: Writing HTML Body file: sashtml11.htm
NOTE: PROCEDURE SQL used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


SYMBOLGEN:  Macro variable NAME_VAR resolves to JOHN
SYMBOLGEN:  Macro variable NAME_VAR resolves to JOHN

NOTE: The data set WORK.JOHN has 1 observations and 1 variables.
NOTE: DATA statement used (Total process time):
      real time           0.03 seconds
      cpu time            0.03 seconds

METHOD 2:


The %PUT Statement


An alternate way of verifying the values of macro variables is to write your own messages to the SAS log. The %PUT statement writes text to the SAS log.
 %PUT statement can be written as: %PUT text; (Where text is any text string.)
The following are the properties of the %PUT statement
a)        a)writes only to the SAS log
b)        b)always writes to a new log line, starting in column one
c)        c)writes a blank line if text is not specified
d)        d)does not require quotation marks around text
e)        e)resolves macro triggers in text before text is written
f)          f)removes leading and trailing blanks from text unless a macro quoting function is used
g)       e)wraps lines when the length of text is greater than the current line size setting
h)        f)can be used either inside or outside a macro definition.
Example
For the code given above you can add   %PUT &Name_var.;
Here:

SELECT NAME INTO:NAME_VAR FROM NAMES_DATA WHERE ID=&ID.;
QUIT;
%PUT &NAME_VAR.;
DATA &NAME_VAR.;
NAME="&NAME_VAR";
…..
This will print the value of the variable every time. The log will look something like this (Note: I have used NOSYMBOLGEN OPTION for this part)

OPTIONAL ARGUMENTS IN %PUT STATEMENT


 Apart from the general %PUT statement you can use some arguments with the %PUT statement.
1. _ALL_ - Lists the values of all macro variables.
2. _AUTOMATIC_ -Lists the values of all automatic macro variables.
3. _USER_ -Lists the values of all user-defined macro variables.
When you use optional arguments such as _ALL_, each macro variable name is also written to the SAS log, along with a label of either AUTOMATIC or GLOBAL.

Blog Widget by LinkWithin

Search this blog..

Loading