Dictionary


Hash Object Attributes and Methods

The hash object contains the following attributes and methods.

Attribute

NUM_ITEMS Attribute on page 1650

Methods

ADD Method on page 1635

CHECK Method on page 1637

DEFINEDATA Method on page 1639

DEFINEDONE Method on page 1641

DEFINEKEY Method on page 1642

DELETE Method on page 1643

FIND Method on page 1644

OUTPUT Method on page 1651

REMOVE Method on page 1654

REPLACE Method on page 1656

Hash Iterator Object Methods

FIRST Method on page 1646

LAST Method on page 1648

NEXT Method on page 1649

PREV Method on page 1654

ADD Method

Adds the specified data that is associated with the given key to the hash object.

Applies to: Hash object

Syntax
  • rc = object .ADD(<KEY: keyvalue -1 ,..., KEY: keyvalue-n , DATA: datavalue-1 , ..., DATA: datavalue-n >);

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash object.

KEY: keyvalue

  • specifies the key value whose type must match the corresponding key variable that is specified in a DEFINEKEY method call.

    The number of KEY: keyvalue pairs depends on the number of key variables that you define by using the DEFINEKEY method.

DATA: datavalue

  • specifies the data value whose type must match the corresponding data variable that is specified in a DEFINEDATA method call.

    The number of DATA: datavalue pairs depends on the number of data variables that you define by using the DEFINEDATA method.

Details

You can use the ADD method in one of two ways to store data in a hash object.

You can define the key and data item, and then use the ADD method as shown in the following code:

 data _null_;     length k ;     length d ;     /* Declare hash object and key and data variable names*/     if _N_ = 1 then do;        declare hash h(hashexp: 4);        rc = h.defineKey('k');        rc = h.defineData('d');        rc = h.defineDone();     end;     /* Define constant key and data values */     k = 'Joyce';     d = 'Ulysses';     /* Add key and data values to hash object */   rc = h.add();   run; 

Alternatively, you can use a shortcut and specify the key and data directly in the ADD method call as shown in the following code:

 data _null_;     length k ;     length d ;     /* Define hash object and key and data variable names*/     if _N_ = 1 then do;        declare hash h(hashexp: 4);        rc = h.defineKey('k');        rc = h.defineData('d');        rc = h.defineDone();        /* avoid uninitialized variable notes */        call missing(k, d);     end;     /* Define constant key and data values and add to hash object */   rc = h.add(key: 'Joyce', data: 'Ulysses');   run; 

Note: If you add a key that is already in the hash object, then the ADD method will return a non-zero value to indicate that the key is already in the hash object. Use the REPLACE method to replace the data that is associated with the specified key with new data.

See Also

Statements:

  • DEFINEDATA Method on page 1639

  • DEFINEKEY Method on page 1642

Storing and Retrieving Data in SAS Language Reference: Concepts

CHECK Method

Checks whether the specified key is stored in the hash object.

Applies to: Hash object

Syntax

rc = object .CHECK(<KEY: keyvalue-1 ,..., KEY: keyvalue-n >);

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash object.

KEY: keyvalue

  • specifies the key value whose type must match the corresponding key variable that is specified in a DEFINEKEY method call.

    The number of KEY: keyvalue pairs depends on the number of key variables that you define by using the DEFINEKEY method.

Details

You can use the CHECK method in one of two ways to store data in a hash object.

You can specify the key, and then use the CHECK method as shown in the following code:

 data _null_;        length k ;        length d ;        /* Declare hash object and key and data variable names*/        if _N_ = 1 then do;           declare hash h(hashexp: 4);           rc = h.defineKey('k');           rc = h.defineData('d');           rc = h.defineDone();              /* avoid uninitialized variable notes */              call missing(k, d);        end;        /* Define constant key and data values and add to hash object */        rc = h.add(key: 'Joyce', data: 'Ulysses');        /* Verify that JOYCE key is in hash object */        k = 'Joyce';   rc = h.check();   if (rc = 0) then           put 'Key is in the hash object.';     run; 

Alternatively, you can use a shortcut and specify the key directly in the CHECK method call as shown in the following code:

 data _null_;     length k ;     length d ;     /* Declare hash object and key and data variable names*/     if _N_ = 1 then do;        declare hash h(hashexp: 4);        rc = h.defineKey('k');        rc = h.defineData('d');        rc = h.defineDone();           /* avoid uninitialized variable notes */           call missing(k, d);     end;     /* Define constant key and data values and add to hash object */     rc = h.add(key: 'Joyce', data: 'Ulysses');     /* Verify that JOYCE key is in hash object */   rc = h.check(key: 'Joyce');   if (rc   =0) then        put 'Key is in the hash object.';  run; 
Comparison

The CHECK method only returns a value that indicates whether the key is in the hash object. The data variable that is associated with the key is not updated. The FIND method also returns a value that indicates whether the key is in the hash object. However, if the key is in the hash object, then the FIND method also sets the data variable to the value of the data item so that it is available for use after the method call.

See Also

Methods:

FIND Method on page 1644

DEFINEKEY Method on page 1642

DEFINEDATA Method

Defines data, associated with the specified data variables, to be stored in the hash object

Applies to: Hash object

Syntax

1 rc = object .DEFINEDATA( datavarname-1 <,... datavarname-n >);

2 rc = object .DEFINEDATA(ALL: YES YES);

Arguments

rc

  • specifies whether the method succeeded or failed.

  • A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash object.

datavarname

  • specifies the name of the data variable.

  • The data variable name can also be enclosed in double quotation marks.

ALL: YES YES

  • specifies all the data variables as data when the data set is loaded in the object constructor.

    If the dataset argument tag is used in the DECLARE or _NEW_ statement to automatically load a data set, then you can define all the data set variables as data by using the ALL: YES option.

    Note: If you use the shortcut notation for the ADD or REPLACE method (for example, h.add(key: 99, data: apple , data: orange)), then you should specify the data in the same order as it exists in the data set.

Note: The hash object does not assign values to key variables (for example, h.find(key: abc)), and the SAS compiler cannot detect the implicit key and data variable assignments that are performed by the hash object and the hash iterator. Therefore, if no explicit assignment to a key or data variable appears in the program, then SAS will issue a note stating that the variable is uninitialized. To avoid receiving these notes, you can perform one of the following actions:

  • Set the NONOTES system option.

  • Provide an initial assignment statement (typically to a missing value) for each key and data variable.

  • Use the CALL MISSING routine with all the key and data variables as parameters. Here is an example:

     length d ;  length k ;  if _N_ = 1 then do;     declare hash h(hashexp: 4);     rc = h.defineKey('k');     rc = h.defineData('d');     rc = h.defineDone();        call missing(k, d);  end; 
Details

The hash object works by storing and retrieving data based on lookup keys. The keys and data are DATA step variables, which you use to initialize the hash object by using dot notation method calls. You define a key by passing the key variable name to the DEFINEKEY method. You define data by passing the data variable name to the DEFINEDATA method. When you have defined all key and data variables, you must call the DEFINEDONE method to complete initialization of the hash object. Keys and data consist of any number of character or numeric DATA step variables.

For detailed information about how to use the DEFINEDATA method, see Defining Keys and Data in SAS Language Reference: Concepts .

Example

The following example creates a hash object and defines the key and data variables:

 data _null_;     length d ;     length k ;     /* Declare the hash object and key and data variables */     if _N_ = 1 then do;        declare hash h(hashexp: 4);        rc = h.defineKey('k');        rc = h.defineData('d');        rc = h.defineDone();        /* avoid uninitialized variable notes */        call missing(k, d);     end;  run; 
See Also

Methods:

  • DEFINEDONE Method on page 1641

  • DEFINEKEY Method on page 1642

Statements:

  • DECLARE Statement on page 1133

  • _NEW_ Statement on page 1325

Defining Keys and Data in SAS Language Reference: Concepts

DEFINEDONE Method

Indicates that all key and data definitions are complete.

Applies to: Hash object

Syntax

rc = object .DEFINEDONE();

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash object.

Details

The hash object works by storing and retrieving data based on lookup keys. The keys and data are DATA step variables, which you use to initialize the hash object by using dot notation method calls. You define a key by passing the key variable name to the DEFINEKEY method. You define data by passing the data variable name to the DEFINEDATA method. When you have defined all key and data variables, you must call the DEFINEDONE method to complete initialization of the hash object. Keys and data consist of any number of character or numeric DATA step variables.

For detailed information about how to use the DEFINEDONE method, see Defining Keys and Data in SAS Language Reference: Concepts .

See Also

Methods:

  • DEFINEDATA Method on page 1639

  • DEFINEKEY Method on page 1642

Defining Keys and Data in SAS Language Reference: Concepts

DEFINEKEY Method

Defines key variables for the hash object

Applies to: Hash object

Syntax

1 rc = object .DEFINEKEY( keyvarname-1 <..., keyvarname-n >);

2 rc = object .DEFINEKEY(ALL: YES YES);

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash object.

keyvarname

  • specifies the name of the key variable.

    The key variable name can also be enclosed in double quotation marks.

ALL: YES YES

  • specifies all the data variables as keys when the data set is loaded in the object constructor.

    If you use the dataset argument tag in the DECLARE or _NEW_ statement to automatically load a data set, then you can define all the key variables by using the ALL: YES option.

Note: If you use the shortcut notation for the ADD, CHECK, FIND, REMOVE, or REPLACE methods (for example, h.add(key: 99, data: apple , data: orange)), then you should specify the keys and data in the same order as they exist in the data set.

Details

The hash object works by storing and retrieving data based on lookup keys. The keys and data are DATA step variables, which you use to initialize the hash object by using dot notation method calls. You define a key by passing the key variable name to the DEFINEKEY method. You define data by passing the data variable name to the DEFINEDATA method. When you have defined all key and data variables, you must call the DEFINEDONE method to complete initialization of the hash object. Keys and data consist of any number of character or numeric DATA step variables.

For detailed information about how to use the DEFINEKEY method, see Defining Keys and Data in SAS Language Reference: Concepts .

Note: The hash object does not assign values to key variables (for example, h.find(key: abc)), and the SAS compiler cannot detect the implicit key and data variable assignments done by the hash object and the hash iterator. Therefore, if no explicit assignment to a key or data variable appears in the program, SAS will issue a note stating that the variable is uninitialized. To avoid receiving these notes, you can perform one of the following actions:

  • Set the NONOTES system option.

  • Provide an initial assignment statement (typically to a missing value) for each key and data variable.

  • Use the CALL MISSING routine with all the key and data variables as parameters. Here is an example:

     length d ;  length k ;  if _N_ = 1 then do;     declare hash h(hashexp: 4);     rc = h.defineKey('k');     rc = h.defineData('d');     rc = h.defineDone();        call missing(k, d);  end; 
See Also

Methods:

  • DEFINEDATA Method on page 1639

  • DEFINEDONE Method on page 1641

Statements:

  • DECLARE Statement on page 1133

  • _NEW_ Statement on page 1325

Defining Keys and Data in SAS Language Reference: Concepts

DELETE Method

Deletes the hash object

Applies to: Hash object

Syntax

rc = object .DELETE();

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is printed to the log.

object

  • specifies the name of the hash object.

Details

When you no longer need the hash object, delete it by using the DELETE method. If you attempt to use a hash object after you delete it, you will receive an error in the log.

FIND Method

Determines whether the specified key is stored in the hash object.

Applies to: Hash object

Syntax

rc = object .FIND(<KEY: keyvalue “1 ,..., KEY: keyvalue-n >);

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash object.

KEY: keyvalue

  • specifies the key value whose type must match the corresponding key variable that is specified in a DEFINEKEY method call.

    The number of KEY: keyvalue pairs depends on the number of key variables that you define by using the DEFINEKEY method.

Details

You can use the FIND method in one of two ways to find data in a hash object.

You can specify the key, and then use the FIND method as shown in the following code:

 data _null_;     length k ;     length d ;     /* Declare hash object and key and data variables */     if _N_ = 1 then do;        declare hash h(hashexp: 4);        rc = h.defineKey('k');        rc = h.defineData('d');        rc = h.defineDone();           /* avoid uninitialized variable notes */           call missing(k, d);     end;     /* Define constant key and data values */     rc = h.add(key: 'Joyce', data: 'Ulysses');     /* Find the key JOYCE */     k = 'Joyce';   rc = h.find();   if (rc = 0) then        put 'Key is in the hash object.';  run; 

Alternatively, you can use a shortcut and specify the key directly in the FIND method call as shown in the following code:

 data _null_;     length k ;     length d ;     /* Declare hash object and key and data variables */     if _N_ = 1 then do;        declare hash h(hashexp: 4);        rc = h.defineKey('k');        rc = h.defineData('d');        rc = h.defineDone();           /* avoid uninitialized variable notes */           call missing(k, d);     end;      /* Define constant key and data values */     rc = h.add(key: 'Joyce', data: 'Ulysses');    /* Find the key JOYCE */   rc = h.find(key: 'Joyce');   if (rc   =0) then        put 'Key is in the hash object.';  `run; 
Comparison

The FIND method returns a value that indicates whether the key is in the hash object. If the key is in the hash object, then the FIND method also sets the data variable to the value of the data item so that it is available for use after the method call. The CHECK method only returns a value that indicates whether the key is in the hash object. The data variable is not updated.

Example

The following example creates a hash object. Two data values are added. The FIND method is used to find a key in the hash object. The data value is returned to the data set variable that is associated with the key.

 data _null_;        length k ;        length d ;        /* Declare hash object and key and data variable names */        if _N_ = 1 then do;           declare hash h(hashexp: 4);           rc = h.defineKey('k');           rc = h.defineData('d');           /* avoid uninitialized variable notes */           call missing(k, d);           rc = h.defineDone();        end;        /* Define constant key and data values and add to hash object */        rc = h.add(key: 'Joyce', data: 'Ulysses');        rc = h.add(key: 'Homer', data: 'Odyssey');        /* Verify that key JOYCE is in hash object and */        /* return its data value to the data set variable D */   rc = h.find(key: 'Joyce');   put d=;     run; 

d=Ulysses is written to the SAS log.

See Also

Methods:

  • CHECK Method on page 1637

  • DEFINEKEY Method on page 1642

Storing and Retrieving Data in SAS Language Reference: Concepts

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 non-zero 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 or _NEW_ statement 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 or _NEW_ statement 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

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 -6 qty -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 ;           length qty ;           declare hash h(hashexp: 4, 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=banana  prod=apple 
See Also

Method:

  • LAST Method on page 1648

Statements:

  • DECLARE Statement on page 1133

  • _NEW_ Statement on page 1325

Using the Hash Iterator Object in SAS Language Reference: Concepts

LAST Method

Returns the last value in the underlying hash object.

Applies to: Hash iterator object

Syntax

rc = object .LAST();

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash iterator object.

Details

The LAST method returns the last data item in the hash object. If you use the ordered: yes or ordered: ascending argument tag in the DECLARE or _NEW_ statement 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 ascending key-value order in the hash object. Conversely, if you use the ordered: descending argument tag in the DECLARE or _NEW_ statement 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 descending key-value order in the hash object.

Use the FIRST method to return the first data item in the hash object.

Note: The LAST method sets the data variable to the value of the data item so that it is available for use after the method call.

See Also

Method:

  • FIRST Method on page 1646

Statements:

  • DECLARE Statement on page 1133

  • _NEW_ Statement on page 1325

Using the Hash Iterator Object in SAS Language Reference: Concepts

NEXT Method

Returns the next value in the underlying hash object.

Applies to: Hash iterator object

Syntax

rc = object .NEXT();

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash iterator object.

Details

Use the NEXT method iteratively to traverse the hash object and return the data items in key order.

The FIRST method returns the first data item in the hash object.

You can use the PREV method to return the previous data item in the hash object.

Note: The NEXT method sets the data variable to the value of the data item so that it is available for use after the method call.

Note: If you call the NEXT method without calling the FIRST method, then the NEXT method will still start at the first item in the hash object.

See Also

Method:

  • FIRST Method on page 1646

  • PREV Method on page 1654

Statements

  • DECLARE Statement on page 1133

  • _NEW_ Statement on page 1325

Using the Hash Iterator Object in SAS Language Reference: Concepts

NUM_ITEMS Attribute

Returns the number of items in the hash object.

Applies to: Hash object

Syntax

variable_name = object .NUM_ITEMS;

Arguments

variable_name

  • specifies the number of items in the hash object after the method is complete.

object

  • specifies the name of the hash object.

Example

This example creates a data set and loads the data set into a hash object. An item is added to the hash object and the total number of items in the resulting hash object is returned by the NUM_ITEMS attribute.

 data work.stock;     input item -10 qty -14;     datalines;  broccoli 345  corn 389  potato 993  onion 730  ;  data _null_;     if _N_ = 1 then do;        length item ;        length qty 3;        length totalitems 3;        /* Declare hash object and read STOCK data set as ordered */        declare hash myhash(hashexp: 4, dataset: "work.stock");        /* Define key and data variables */        myhash.defineKey('item');        myhash.defineData('qty');        myhash.defineDone();     end;     /* Add a key and data value to the hash object */     item = 'celery';     qty = 183;     rc = myhash.add();     if (rc ne 0) then        put 'Add failed';     /* Use NUM_ITEMS to return updated number of items in hash object */   totalitems = myhash.num_items;   put totalitems=;  run; 

totalitems=5 is written to the SAS log.

OUTPUT Method

Creates one or more data sets each of which contain the data in the hash object.

Applies to: Hash object

Syntax

rc = object .OUTPUT(DATASET: dataset-1 <..., DATASET: dataset-n >);

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash iterator object.

DATASET: dataset

  • specifies the name of the output data set.

    The name of the SAS data set can be a literal or character variable. The data set name can also be enclosed in double quotation marks. Macro variables must be enclosed in double quotation marks.

Details

Hash object keys are not automatically stored as part of the output data set. The keys must be defined as data items by using the DEFINEDATA method to be included in the output data set.

If you use the ordered: yes or ordered: ascending argument tag in the DECLARE or _NEW_ statement when you instantiate the hash object, then the data items are written to the data set in ascending key-value order. If you use the ordered: descending argument tag in the DECLARE or _NEW_ statement when you instantiate the hash object, then the data items are written to the data set in descending key-value order.

Example

Using the data set ASTRO that contains astronomical data, the following code creates a hash object with the Messier (OBJ) objects sorted in ascending order by their right-ascension (RA) values and uses the OUTPUT method to save the data to a data set.

 data astro;     input obj -4 ra -12 dec -19;     datalines;   M31 00 42.7 +41 16   M71 19 53.8 +18 47   M51 13 29.9 +47 12   M98 12 13.8 +14 54   M13 16 41.7 +36 28   M39 21 32.2 +48 26   M81 09 55.6 +69 04  M100 12 22.9 +15 49   M41 06 46.0   20 44   M44 08 40.1 +19 59   M10 16 57.1   04 06   M57 18 53.6 +33 02    M3 13 42.2 +28 23   M22 18 36.4   23 54   M23 17 56.8   19 01   M49 12 29.8 +08 00   M68 12 39.5   26 45   M17 18 20.8   16 11   M14 17 37.6   03 15   M29 20 23.9 +38 32   M34 02 42.0 +42 47   M82 09 55.8 +69 41   M59 12 42.0 +11 39   M74 01 36.7 +15 47   M25 18 31.6   19 15  ;  run;  data _null_;     if _N_ = 1 then do;        length obj ;        length ra ;        length dec ;        /* Read ASTRO data set as ordered */        declare hash h(hashexp: 4, dataset:"work.astro", ordered: 'yes');        /* Define variables RA and OBJ as key and data for hash object */        declare hiter iter('h');        h.defineKey('ra');        h.defineData('ra', 'obj');        h.defineDone();        /* avoid uninitialized variable notes */        call missing(ra, obj);     end;  /* Create output data set from hash object */   rc = h.output(dataset: 'work.out');   run;  proc print data=work.out;     var ra obj;     title 'Messier Objects Sorted by Right-Ascension Values';  run; 
Output A1.1: Messier Objects Sorted by Right-Ascension Values
start example
 Messier Objects Sorted by Right-Ascension Values            1               Obs      ra       obj                 1    00 42.7    M31                 2    01 36.7    M74                 3    02 42.0    M34                 4    06 46.0    M41                 5    08 40.1    M44                 6    09 55.6    M81                 7    09 55.8    M82                 8    12 13.8    M98                 9    12 22.9    M100                10    12 29.8    M49                11    12 39.5    M68                12    12 42.0    M59                13    13 29.9    M51                14    13 42.2    M3                15    16 41.7    M13                16    16 57.1    M10                17    17 37.6    M14                18    17 56.8    M23                19    18 20.8    M17                20    18 31.6    M25                21    18 36.4    M22                22    18 53.6    M57                23    19 53.8    M71                24    20 23.9    M29                25    21 32.2    M39 
end example
 
See Also

Method:

  • DEFINEDATA Method on page 1639

Statements:

  • DECLARE Statement on page 1133

  • _NEW_ Statement on page 1325

Saving Hash Object Data in a Data Set in SAS Language Reference: Concepts

PREV Method

Returns the previous value in the underlying hash object

Applies to: Hash iterator object

Syntax

rc = object .PREV();

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash iterator object.

Details

Use the PREV method iteratively to traverse the hash object and return the data items in reverse key order.

The FIRST method returns the first data item in the hash object. The LAST method returns the last data item in the hash object.

You can use the NEXT method to return the next data item in the hash object.

Note: The PREV method sets the data variable to the value of the data item so that it is available for use after the method call.

See Also

Methods:

  • FIRST Method on page 1646

  • LAST Method on page 1648

  • NEXT Method on page 1649

Statements:

  • DECLARE Statement on page 1133

  • _NEW_ Statement on page 1325

Using the Hash Iterator Object in SAS Language Reference: Concepts

REMOVE Method

Removes the data that is associated with the specified key from the hash object

Applies to: Hash object

Syntax

rc = object .REMOVE(<KEY: keyvalue-1 ,..., KEY: keyvalue-n >);

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash object.

KEY: keyvalue

  • specifies the key value whose type must match the corresponding key variable that is specified in a DEFINEKEY method call

    The number of KEY: keyvalue pairs depends on the number of key variables that you define by using the DEFINEKEY method.

Details

The REMOVE method deletes both the key and the data from the hash object.

You can use the REMOVE method in one of two ways to remove the key and data in a hash object.

You can specify the key, and then use the REMOVE method as shown in the following code:

 data _null_;     length k ;     length d ;     if _N_ = 1 then do;        declare hash h(hashexp: 4);        rc = h.defineKey('k');        rc = h.defineData('d');        rc = h.defineDone();        /* avoid uninitialized variable notes */        call missing(k, d);     end;     rc = h.add(key: 'Joyce', data: 'Ulysses');     /* Specify the key */     k = 'Joyce';     /* Use the REMOVE method to remove the key and data */   rc = h.remove();   if (rc = 0) then        put 'Key and data removed from the hash object.';  run; 

Alternatively, you can use a shortcut and specify the key directly in the REMOVE method call as shown in the following code:

 data _null_;     length k ;     length d ;     if _N_ = 1 then do;        declare hash h(hashexp: 4);        rc = h.defineKey('k');        rc = h.defineData('d');        rc = h.defineDone();        /* avoid uninitialized variable notes */        call missing(k, d);     end;     rc = h.add(key: 'Joyce', data: 'Ulysses');     rc = h.add(key: 'Homer', data: 'Iliad');     /* Specify the key in the REMOVE method parameter */   rc = h.remove(key: 'Homer');   if (rc =0) then        put 'Key and data removed from the hash object.';  run; 
See Also

Methods:

  • ADD Method on page 1635

  • DEFINEKEY Method on page 1642

  • REPLACE Method on page 1656

Replacing and Removing Data in SAS Language Reference: Concepts

REPLACE Method

Replaces the data that is associated with the specified key with new data

Applies to: Hash object

Syntax

rc = object .REPLACE(<KEY: keyvalue-1 ,..., KEY: keyvalue-n , DATA: datavalue-1 ,..., DATA: datavalue-n >);

Arguments

rc

  • specifies whether the method succeeded or failed.

    A return code of zero indicates success; a non-zero value indicates failure. If you do not supply a return code variable for the method call and the method fails, then an appropriate error message is written to the log.

object

  • specifies the name of the hash object.

KEY: keyvalue

  • specifies the key value whose type must match the corresponding key variable that is specified in a DEFINEKEY method call.

    The number of KEY: keyvalue pairs depends on the number of key variables that you define by using the DEFINEKEY method.

DATA: datavalue

  • specifies the data value whose type must match the corresponding data variable that is specified in a DEFINEDATA method call.

    The number of DATA: datavalue pairs depends on the number of data variables that you define by using the DEFINEDATA method.

Details

You can use the REPLACE method in one of two ways to replace data in a hash object.

You can define the key and data item, and then use the REPLACE method as shown in the following code. In this example the data for the key Rottwlr is changed from 1st to 2nd .

 data work.show;     input brd -10 plc -14;  datalines;  Terrier    2nd  LabRetr    3rd  Rottwlr    1st  Collie     bis  ChinsCrstd 2nd  Newfnlnd   3rd  ;  data _null_;     length brd ;     length plc ;     if _N_ = 1 then do;        declare hash h(hashexp: 4, dataset: 'work.show');        rc = h.defineKey('brd');        rc = h.defineData('plc');        rc = h.defineDone();     end;     /* Specify the key and new data value */     brd = 'Rottwlr';     plc = '2nd';     /* Call the REPLACE method to replace the data value */   rc = h.replace();   run; 

Alternatively, you can use a shortcut and specify the key and data directly in the REPLACE method call as shown in the following code:

 data work.show;     input brd -10 plc -14;  datalines;  Terrier    2nd  LabRetr    3rd  Rottwlr    1st  Collie     bis  ChinsCrstd 2nd  Newfnlnd   3rd  ;  data _null_;     length brd ;     length plc ;     if _N_ = 1 then do;        declare hash h(hashexp: 4, dataset: 'work.show');        rc = h.defineKey('brd');        rc = h.defineData('plc');        rc = h.defineDone();        /* avoid uninitialized variable notes */        call missing(brd, plc);     end;     /* Specify the key and new data value in the REPLACE method */   rc = h.replace(key: 'Rottwlr', data: '2nd');   run; 

Note: If you call the REPLACE method and the key is not found, then the key and data are added to the hash object.

See Also

Methods:

  • DEFINEDATA Method on page 1639

  • DEFINEKEY Method on page 1642

  • REMOVE Method on page 1654

Replacing and Removing Data in SAS Language Reference: Concepts




SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
SAS 9.1 Language Reference Dictionary, Volumes 1, 2 and 3
ISBN: N/A
EAN: N/A
Year: 2004
Pages: 704

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net