FIRST Method

Returns the first value in the underlying hash object.
Applies to: Hash iterator object

Syntax

rc=object.FIRST( );

Arguments

rc
specifies whether the method succeeded or failed.
A return code of zero indicates success; a nonzero value indicates failure. If you do not supply a return code variable for the method call and the method fails, an appropriate error message will be printed to the log.
object
specifies the name of the hash iterator object.

Details

The FIRST method returns the first data item in the hash object. If you use the ordered: 'yes' or ordered: 'ascending' argument tag in the DECLARE statement or _NEW_ operator when you instantiate the hash object, then the data item that is returned is the one with the 'least' key (smallest numeric value or first alphabetic character), because the data items are sorted in ascending key-value order in the hash object. Repeated calls to the NEXT method will iteratively traverse the hash object and return the data items in ascending key order. Conversely, if you use the ordered: 'descending' argument tag in the DECLARE statement or _NEW_ operator when you instantiate the hash object, then the data item that is returned is the one with the 'highest' key (largest numeric value or last alphabetic character), because the data items are sorted in descending key-value order in the hash object. Repeated calls to the NEXT method will iteratively traverse the hash object and return the data items in descending key order.
Use the LAST method to return the last data item in the hash object.
Note: The FIRST method sets the data variable to the value of the data item so that it is available for use after the method call.

Example: Retrieving Hash Object Data

The following example creates a data set that contains sales data. You want to list products in order of sales. The data is loaded into a hash object and the FIRST and NEXT methods are used to retrieve the data.
data work.sales;
   input prod $1-6 qty $9-14;
   datalines;
banana  398487
apple   384223
orange  329559
;
data _null_;
   /* Declare hash object and read SALES data set as ordered */
   if _N_ = 1 then do;
      length prod $10;
      length qty $6;
      declare hash h(dataset: 'work.sales', ordered: 'yes');
      declare hiter iter('h');
      /* Define key and data variables */
      h.defineKey('qty');
      h.defineData('prod');
      h.defineDone();
      /* avoid uninitialized variable notes */
      call missing(qty, prod);
   end;
   /* Iterate through the hash object and output data values */
   rc = iter.first();
   do while (rc = 0);
      put prod=;
      rc = iter.next();
   end;
run;
The following lines are written to the SAS log:
prod=orange
prod=apple
prod=banana