You declare a hash iterator
object by using the DECLARE statement. After you declare the new hash
iterator object, use the _NEW_ operator to instantiate the object,
using the hash object name as an argument tag. For example:
declare hiter myiter;
myiter = _new_ hiter('h');
The DECLARE statement
tells the compiler that the object reference MYITER is of type hash
iterator. At this point, you have declared only the object reference
MYITER. It has the potential to hold a component object of type hash
iterator. You should declare the hash iterator object only once. The
_NEW_ operator creates an instance of the hash iterator object and
assigns it to the object reference MYITER. The hash object, H, is
passed as a constructor argument. The hash object, not the hash object
variable, is specifically assigned to the hash iterator.
As an alternative to
the two-step process of using the DECLARE statement and the _NEW_
operator to declare and instantiate a component object, you can declare
and instantiate a hash iterator object in one step by using the DECLARE
statement as a constructor method. The syntax is as follows:
declare hiter object_name(hash_object_name);
In
the above example, the hash object name must be enclosed in single
or double quotation marks.
For example:
declare hiter myiter('h');
The
previous statement is equivalent to these:
declare hiter myiter;
myiter = _new_ hiter('h');
For example:
if _N_ = 1 then do;
length key $10;
declare hash myhash(dataset:"work.x", ordered: 'yes');
declare hiter myiter('myhash');
myhash.defineKey('key');
myhash.defineDone();
end;
This code creates an
instance of a hash iterator object with the variable name MYITER.
The hash object, MYHASH, is passed as the constructor argument. Because
the hash object was created with the ORDERED argument tag set to
'yes'
,
the data is returned in ascending key-value order.