Amadeus cookies policy - you'll see this message only once.

Amadeus use cookies on this website. They help us to know a little bit about you and how you use our website, which improves the browsing experience and marketing - both for you and for others. They are stored locally on your computer or mobile device. To accept cookies, continue browsing as normal. Or, go to the privacy policy for more information.

Using Formatted PUT Statements when Debugging Code

When debugging a program, it is often helpful to include PUT statements within a DATA step, in order to list the values of one or more variables in the log window.

In this context, the most common form of PUT statement that tends to be used is: PUT _ALL_, which lists the values of all DATA step variables as shown in the following example:

* List all DATA step variables and their values;
data _null_;
  set sashelp.class(obs=10);
  put _all_;
run;

The listing that appears in the log looks as follows:

Name=Alfred Sex=M Age=14 Height=69 Weight=112.5 _ERROR_=0 _N_=1
Name=Alice Sex=F Age=13 Height=56.5 Weight=84 _ERROR_=0 _N_=2
Name=Barbara Sex=F Age=13 Height=65.3 Weight=98 _ERROR_=0 _N_=3
Name=Carol Sex=F Age=14 Height=62.8 Weight=102.5 _ERROR_=0 _N_=4
Name=Henry Sex=M Age=14 Height=63.5 Weight=102.5 _ERROR_=0 _N_=5
Name=James Sex=M Age=12 Height=57.3 Weight=83 _ERROR_=0 _N_=6
Name=Jane Sex=F Age=12 Height=59.8 Weight=84.5 _ERROR_=0 _N_=7
Name=Janet Sex=F Age=15 Height=62.5 Weight=112.5 _ERROR_=0 _N_=8
Name=Jeffrey Sex=M Age=13 Height=62.5 Weight=84 _ERROR_=0 _N_=9
Name=John Sex=M Age=12 Height=59 Weight=99.5 _ERROR_=0 _N_=10

There are two issues with the above listing:

  • It includes the automatic variables _N_ and _ERROR_, which may not be required.
  • The listing style of output is not particularly easy to read and it gets worse as the number of variables increases.

Improvements can easily be made by using formatted PUT statements. This involves placing the list of variables (or the _ALL_ keyword) within round brackets and following it with a list of format arguments, which are also placed within round brackets.

PUT (|<_all_>) (format-args); 

The following examples show how formatted PUT statements can be used to improve the way in which variables are listed in the log window:

The first example gives a slight improvement on the output shown above, in that it excludes the automatic variables from the listing. The variable list argument is _ALL_, which requests that all variables in the dataset are listed apart from the automatic ones. The equals sign is used as the format argument, so that the variable name precedes its value:

* Exclude automatic variables;
data _null_;
  set sashelp.class(obs=10);
  put (_all_)(=);
run;

The resulting log is as follows:

Name=Alfred Sex=M Age=14 Height=69 Weight=112.5
Name=Alice Sex=F Age=13 Height=56.5 Weight=84
Name=Barbara Sex=F Age=13 Height=65.3 Weight=98
Name=Carol Sex=F Age=14 Height=62.8 Weight=102.5
Name=Henry Sex=M Age=14 Height=63.5 Weight=102.5
Name=James Sex=M Age=12 Height=57.3 Weight=83
Name=Jane Sex=F Age=12 Height=59.8 Weight=84.5
Name=Janet Sex=F Age=15 Height=62.5 Weight=112.5
Name=Jeffrey Sex=M Age=13 Height=62.5 Weight=84
Name=John Sex=M Age=12 Height=59 Weight=99.5

To make the output easier to read, the '/' format argument can be included so that each value is output on a new line:

* Put each value on a new line;
data _null_;
  set sashelp.class(obs=10);
  put (_all_)(=/);
run;

The resulting log is as follows:

Name=Alfred
Sex=M
Age=14
Height=69
Weight=112.5
Name=Alice Sex=F Age=13 Height=56.5 Weight=84
Name=Barbara Sex=F Age=13 Height=65.3 Weight=98

If there was a requirement to apply a common format to a given type of variable, then this can be specified as an argument:

* Put each value on a new line and apply a
  common format to all numeric variables;
data _null_;
  set sashelp.class(obs=10);
  put (_all_)(=/12.2);
run;

Note that SAS ignores the numeric format specification with regards to the NAME and SEX variables, which are character:

Name=Alfred
Sex=M
Age=14.00
Height=69.00
Weight=112.50
Name=Alice Sex=F Age=13.00 Height=56.50 Weight=84.00

To obtain a tabular listing, then drop the '/' and '=' arguments and specify one or more formats that are to be applied. In the following example, four different formats are specified in the order that they are to be applied and the number of variables that they are to be applied to, which is denoted by the n*format-name syntax:

* List values as a table and apply formats to groups of variables;
data _null_;
  set sashelp.class(obs=10);
  if _n_=1 then put @1 'NAME' @19 'SEX' @23 'AGE' 
                    @30 'HEIGHT' @38 'WEIGHT';
  put (_all_)(1*$20.,1*$2.,1*3.,2*8.2);
run;
NAME              SEX AGE  HEIGHT  WEIGHT
Alfred              M 14    69.00  112.50
Alice               F 13    56.50   84.00
Barbara             F 13    65.30   98.00
Carol               F 14    62.80  102.50
Henry               M 14    63.50  102.50
James               M 12    57.30   83.00
Jane                F 12    59.80   84.50
Janet               F 15    62.50  112.50
Jeffrey             M 13    62.50   84.00
John                M 12    59.00   99.50

The above techniques are just a few of the many ways in which formatted PUT statements can be used to improve output. Although the focus here is on making log listings easier to interpret, the same techniques can be applied when outputting DATA step reports. For further details and examples of using formatted PUT statements, consult the SAS documentation.