/***************************************************************************************************************** SAS file name: iterator_methods.sas File location: __________________________________________________________________________________________________________________ Purpose: To demonstrate the 5 hash iterator methods available in SAS, first, last, next, prev, setcur Author: Peter Clemmensen Creation Date: 01/08/2019 This post suuports the blog post "The 5 Hash Iterator Methods in SAS" on SASnrd.com *****************************************************************************************************************/ /* Example data */ data have; input key data; datalines; 1 10 1 20 2 30 2 40 2 50 3 60 3 70 ; /* First and Last Method */ data _null_; key=.; data=.; declare hash h(dataset:'have', ordered:'Y'); h.definekey('key'); h.definedata('key', 'data'); h.definedone(); declare hiter hi('h'); hi.first(); put key= // data=; hi.last(); put key= // data=; run; /* Next and Prev Method */ data _null_; key=.; data=.; declare hash h(dataset:'have', ordered:'Y'); h.definekey('key'); h.definedata('key', 'data'); h.definedone(); declare hiter hi('h'); hi.first(); put key= / data= //; hi.next(); put key= / data= //; hi.last(); put key= / data= //; hi.prev(); put key= / data= //; run; /* Setcur Method */ data _null_; key=.; data=.; declare hash h(dataset:'have', ordered:'Y'); h.definekey('key', 'data'); h.definedone(); declare hiter hi('h'); rc=hi.setcur(key:2, key:30); put key= / data= //; do while (rc=0); put key= / data= //; rc=hi.next(); end; run;