In this post, I will demonstrate how to print the content of a SAS hash object in the log. No out-of-the-box tool exists for this purpose. Luckily, Richard A. Devenezia wrote a beautiful macro %Puthash, which does the job flawlessly. In this post, I will demonstrate how to use it. The Puthash macro is one of my favorite macro. I use it all the time when I write SAS code with hash objects and has to debug code. In that situation, the macro is invaluable.

The SAS PutHash Macro Definition

Below, you can see the macro definition. The first part is all about finding names to variables, that does not already exist in the PDV. This is done by generating a 8-digit random number and prefixing it with hi and rc respectively. This is done because then we are able to create assigned calls to the iterator object. Next, a hash iterator is used to traverse the entire hash object. Finally, the iterator object is deleted.

/* Richard A. DeVenezia
 * Feb 11, 2003
 * Show the values of the data items of a DATA Step hash object in the log
%macro putHash (hash, vars);
  %* hash - variable that references a hash object
  %* vars - space separated list of variables linked to the data items of hash
  %*        separate with pound sign (#) to get varname=varvalue format
  %* generate a random variable name;
  %local random hi rc;
  %let random = %substr(%sysfunc(ranuni(0),10.8),3);
  %let hi = hi_&random;
  %let rc = rc_&random;
  %* emit DATA Step code that iterates the hash and
  %* puts the data items values in the log;
  declare hiter &hi ("&hash");
  do &rc = &hi..first() by 0 while (&rc = 0);
    put %sysfunc(translate(&vars,=,#));
    &rc = &;
  %put WARNING: Values of variables &vars will change;

A Few Examples

Let us see how to use the PutHash macro. Below, I create a hash object and fill it with the sashelp.class data set. Finally, I use the PutHash macro to print the data values in the log. I put # between the variable names to put variable names and equal signs in the log as well.

data _null_;
   declare hash h (dataset : 'sashelp.class');
   h.definekey ('name');
   h.definedata (all : 'Y');
   h.definedone ();
   if 0 then set sashelp.class;
   %putHash (H,name#age#weight#sex#);

This prints the following to the log

Name=John Age=12 Weight=99.5 Sex=M
Name=Alice Age=13 Weight=84 Sex=F
Name=Henry Age=14 Weight=102.5 Sex=M
Name=Joyce Age=11 Weight=50.5 Sex=F
Name=Janet Age=15 Weight=112.5 Sex=F
Name=Judy Age=14 Weight=90 Sex=F
Name=William Age=15 Weight=112 Sex=M
Name=Mary Age=15 Weight=112 Sex=F
Name=James Age=12 Weight=83 Sex=M
Name=Barbara Age=13 Weight=98 Sex=F
Name=Carol Age=14 Weight=102.5 Sex=F
Name=Ronald Age=15 Weight=133 Sex=M
Name=Louise Age=12 Weight=77 Sex=F
Name=Thomas Age=11 Weight=85 Sex=M
Name=Alfred Age=14 Weight=112.5 Sex=M
Name=Robert Age=12 Weight=128 Sex=M
Name=Jane Age=12 Weight=84.5 Sex=F
Name=Philip Age=16 Weight=150 Sex=M
Name=Jeffrey Age=13 Weight=84 Sex=M

A SAS hash object is a dynamic structure. It grows and shrinks at run time. Therefore, I like to use the Puthash macro to display how the content of a hash object changes. The macro gives me a smooth overview of the content of the hash object and how it changes as the program proceeds. Take a look at the code below. Here I call the macro thrice. Once for each value of k=1, 2, 3.

data _null_;
   declare hash h ();
   h.definekey ('k');
   h.definedata ('d');
   h.definedone ();
   do k = 1, 2, 3;
      d = k*100;
      %putHash (H, d#);

The following is printed to the log.



In this post, I demonstrate how to print the content of a SAS hash object in the log. I know of no better tool than the PutHash macro written by the great Richard A. Devenezia. If you have not already, check out his site, There are tons of code examples to learn from. The PutHash macro is originally presented here.

As a related post, read how to Read Many Variables Into the SAS Hash Object.

You can download the entire code from this post here.