BookmarkSubscribeRSS Feed

I suggest the creation of additional methods for hash objects which provide the capability to first "position" a pointer in the search tree (e.g. hash.pos_key()), even if the key is not found, and then find the next key in the sorting sequence (hash.next_key()). The second method should have an option to search "up" or "down", the default being "up".

The first method will always return a zero, while the second method will return a non-zero return code if the beginning (while searching "down") or the end (while searching "up") of the hash has been reached. Multiple entries for a given key should be treated as one.

 

3 Comments
PeterClemmensen
Tourmaline | Level 20

Upvoted. 

 

You can simulate this using the logic below, but a dedicated method would be cool 🙂

 

  1. Inserting the key value to be searched for into the hash object (if it's not there already).
  2. Use a hash iterator and the Setcur Method to place the iterator on the key value.
  3. Iterate in the desired direction.
  4. Remove the key value from the hash object again if it was not there in the first place.

 

 

mkeintz
PROC Star

I presume you intend this for ordered hash objects only, as I don't see a significant use case for "unordered" hash objects.  In fact, I would want the proposed "poskey" and related methods to return a non-zero code if the object is not ordered.  Acter all, for such objects, these methods could not guarantee consistent results, as they would be impacted by the table size specified in the hashexp object parameter.

 

Because I have used the work-around suggested by @PeterClemmensen with relatively little effort, I'm not as enthusiastic about this suggestion as other hash improvements that have no work-around.  (I've long wished for an "append" method to complement the "output" method - which could relieve memory demands in many instances).

FreelanceReinh
Jade | Level 19

I've just come across a SAS employee's 2014 post Re: Lookup Values from a Rate Matrix Table where a similar (meanwhile archived) ballot idea was linked. However, the link there doesn't really work anymore, I get "Access Denied," so can't see details, status or comments.