Previous Page | Next Page

Hash and Hash Iterator Object Language Elements

ADD Method



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

Syntax
Arguments
Details
See Also

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 $8;
   length d $12;

   /* Declare hash object and key and data variable names */
   if _N_ = 1 then do;
      declare hash h();
      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 $8;
   length d $12;

   /* Define hash object and key and data variable names */
   if _N_ = 1 then do;
      declare hash h();
      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;

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.

If you do not specify the data variables with the DEFINEDATA method, the data variables are automatically assumed to be same as the keys.

If you use the KEY: and DATA: argument tags to specify the key and data directly, you must use both argument tags.

The ADD method does not set the value of the data variable to the value of the data item. It only sets the value in the hash object.


See Also

Statements:

DEFINEDATA Method

DEFINEKEY Method

REF Method

Storing and Retrieving Data in SAS Language Reference: Concepts

Previous Page | Next Page | Top of Page