Today, I will demonstrate how to determine whether two hash objects are equal in SAS. There is only one method, that can do this. The Equals() Method. Before we look at actual objects, we first have to define what we consider as ‘Equal’. The SAS Equals Method Documentation sets up four conditions. When all of the conditions are fulfilled, the hash objects are considered equal and the method returns a return code with the value 1. Otherwise, the method returns 0. In this post, I will go through the conditions one by one. For each condition, I write small code examples to demonstrate when the condition is fulfilled and when it is not.

Both hash objects are the same size

In this condition, size means the size of the HASHEXP Argument Tag. Take a look at the code below. I declare and instantiate two hash objects. The only difference between them is the size of HASHEXP. At the bottom, I use the Equals Method to compare the two objects. I save the return code in the variable r (result: r). Finally, I print the value of r in the log. In this example, SAS prints r=0 in the log.

data _null_;
    declare hash h1(hashexp:8);
    h1.definekey('k');
    h1.definedone();
 
    declare hash h2(hashexp:20);
    h2.definekey('k');
    h2.definedone();
 
    k=.; 
 
    rc=h1.equals(hash: 'h2', result: r);
 
    put r=;
run;

Both hash objects have the same number of items

This condition is pretty self-explanatory. Intuitively, we will not consider h1 and h2 as equal if h1 has one element and h2 has 2. Behind the scenes, SAS compares the NUM_ITEMS Attribute for h1 and h2 and returns a zero value if they are not equal. See an example below.

data _null_;
    declare hash h1();
    h1.definekey('k');
    h1.definedata('d');
    h1.definedone();
 
    declare hash h2();
    h2.definekey('k');
    h2.definedata('d');
    h2.definedone();
 
    k=1; d=1; h1.add();
 
    k=1; d=1; h2.add();
    k=2; d=2; h2.add();
 
    rc=h1.equals(hash: 'h2', result: r);
 
    put r=;
run;

Both hash objects have the same key and data structure

In this case ‘same key and data structure’ means that h1 and h2 should have the same key variables and the same data variables. The order of them does not matter. See an example below. In h1, I define d1 and d2 as data variables. In h2, I do the same thing, but in reverse order. As you can see from the return code, SAS does not care about the order. SAS considers the two hash objects as equal.

data _null_;
    declare hash h1();
    h1.definekey('k');
    h1.definedata('d1', 'd2');
    h1.definedone();
 
    declare hash h2();
    h2.definekey('k');
    h2.definedata('d2', 'd1');
    h2.definedone();
 
    call missing(k, d1, d2);
 
    rc=h1.equals(hash: 'h2', result: r);
 
    put r=;
run;

In unordered iterations over two hash objects, they have the same key and data values for each entry

This condition deserves some clarification. The documentation is quite short and does not provide more information than this. The important thing to understand is that this condition is solely related to the ORDERED: Argument Tag. Take a look at the code below. The two hash objects contain the same data. The only difference between them is the ‘A’ (Ascending) and ‘D’ (‘Descending’) in the Ordered: Argument. Yet, the two hash objects are considered different in the code below. I encourage you to change the values ‘A’ and ‘D’. If you change one of them to ‘N’, or even omit the Ordered Tag, then h1 and h2 are considered equal and r=1.

I like to think of condition 4 a bit different than expressed in the documentation. The ORDERED: Argument tags of the two hash objects must be explicitly opposing each other for SAS to consider h1 and h2 as different. Explicitly opposing means that one must have the value ‘A’ or ‘Y’ and the other ‘D’. SAS will consider them equal if one of them does not have the Ordered: argument specified or it has the value ‘N’.

data _null_;
    declare hash h1(ordered:'A');
    h1.definekey('k');
    h1.definedata('d');
    h1.definedone();
 
    declare hash h2(ordered:'D');
    h2.definekey('k');
    h2.definedata('d');
    h2.definedone();
 
    do k=1 to 10;
        d=k;
        h1.add();
    end;
 
    do k=1 to 10;
        d=k;
        h2.add();
    end;
 
    rc=h1.equals(hash: 'h2', result: r);
 
    put r=;
run;

Summary

In this post, we have investigated the four conditions that should be fulfilled for SAS to consider two hash objects equal. We have used the Equals() Method to do so. We have seen a few code examples of when h1 and h2 are equal and when they are not.

If you want to learn more, check out the book SAS Hash Object Programming Made Easy by Michelle M. Burlew. There is an entire section on the Equals() Method. For other rare methods, see the blog posts Explore the SAS Hash Object Replacedup Method and Explore the SAS Hash Object Removedup Method.

You can download the entire code from this post here.