Declares a hash or hash iterator object; creates an instance of and initializes data for a hash or hash iterator object.
Valid in: | DATA step |
Category: | Action |
Type: | Executable |
Alias: | DCL |
Applies to: | Hash object, Hash iterator object |
specifies the component object. It can be one of the following values:
specifies a hash object. The hash object provides a mechanism for quick data storage and retrieval. The hash object stores and retrieves data based on lookup keys.
See | Using the Hash Object in SAS Language Reference: Concepts |
specifies a hash iterator object. The hash iterator object enables you to retrieve the hash object's data in forward or reverse key order.
See | Using the Hash Object in SAS Language Reference: Concepts |
specifies the object reference name for the hash or hash iterator object.
specifies the information that is used to create an instance of the hash object.
Specifies the name of a SAS data set to load into the hash object.
dcl hash h (dataset: 'x (where = (i > 10))');
Note | If the data set contains duplicate keys, the default is to keep the first instance in the hash object; subsequent instances are ignored. To store the last instance in the hash object or an error message written to the SAS log if there is a duplicate key, use the DUPLICATE argument tag. |
determines whether to ignore duplicate keys when loading a data set into the hash object. The default is to store the first key and ignore all subsequent duplicates. Option can be one of the following values:
stores the last duplicate key record.
reports an error to the log if a duplicate key is found.
brown
for
the key 620 and blue
for the key 531.
If you use the default, green
would
be stored for 620 and yellow
would
be stored for 531.
data table; input key data $; datalines; 531 yellow 620 green 531 blue 908 orange 620 brown 143 purple run; data _null_; length key 8 data $ 8; if (_n_ = 1) then do; declare hash myhash(dataset: "table", duplicate: "r"); rc = myhash.definekey('key'); rc = myhash.definedata('data'); myhash.definedone(); end; rc = myhash.output(dataset:"otable"); run;
The hash object's internal table size, where the size of the hash table is 2n.
Default | 8, which equates to a hash table size of 28 or 256 |
specifies the name of a variable that tracks the key summary for all keys. A key summary is a count of how many times a key has been referenced on a FIND method call.
Note | The key summary is in the output data set. |
Example | Adding the Key Summary to the Output Data Set |
Specifies whether or how the data is returned in key-value order if you use the hash object with a hash iterator object or if you use the hash object OUTPUT method.
Data is returned in
ascending key-value order. Specifying 'ascending
'
is the same as specifying 'yes
'.
Data is returned in descending key-value order.
Data is returned in
ascending key-value order. Specifying 'yes
'
is the same as specifying 'ascending
'.
Data is returned in some undefined order.
Default | NO |
Tip | The argument can also be enclosed in double quotation marks. |
specifies whether multiple data items are allowed for each key.
Multiple data items are allowed for each key.
Only one data item is allowed for each key.
Default | NO |
Tip | The argument value can also be enclosed in double quotation marks. |
See | Non-Unique Key and Data Pairs in SAS Language Reference: Concepts |
maintains a summary count of hash object keys. The SUMINC argument tag is given a DATA step variable, which holds the sum increment. The sum increment is how much to add to the key summary for each reference to the key.
See | Maintaining Key Summaries in SAS Language Reference: Concepts |
Example | A key summary changes using the current value of the
DATA step variable. dcl hash myhash(suminc: 'count'); |
See | Initializing Hash Object Data Using a Constructor in SAS Language Reference: Concepts and Declaring and Instantiating a Hash Iterator Object in SAS Language Reference: Concepts |
declare hash h;
h = _new_ hash( );
declare hash h( );
declare hash h; h = _new_ hash( );
declare hash h(hashexp: 4);
dcl hash h(dataset: 'x (where = (i > 10))');
data _null_; length k $15; length d $15; if _N_ = 1 then do; /* Declare and instantiate hash object "myhash" */ declare hash myhash; myhash = _new_ hash( ); /* Define key and data variables */ rc = myhash.defineKey('k'); rc = myhash.defineData('d'); rc = myhash.defineDone( ); /* avoid uninitialized variable notes */ call missing(k, d); end; /* Create constant key and data values */ rc = myhash.add(key: 'Labrador', data: 'Retriever'); rc = myhash.add(key: 'Airedale', data: 'Terrier'); rc = myhash.add(key: 'Standard', data: 'Poodle'); /* Find data associated with key and write data to log */ rc = myhash.find(key: 'Airedale'); if (rc = 0) then put d=; else put 'Key Airedale not found'; run;
data _null_;
length k $15;
length d $15;
if _N_ = 1 then do;
/* Declare and instantiate hash object "myhash" */
declare hash myhash( );
rc = myhash.defineKey('k');
rc = myhash.defineData('d');
rc = myhash.defineDone( );
/* avoid uninitialized variable notes */
call missing(k, d);
end;
/* Create constant key and data values */
rc = myhash.add(key: 'Labrador', data: 'Retriever');
rc = myhash.add(key: 'Airedale', data: 'Terrier');
rc = myhash.add(key: 'Standard', data: 'Poodle');
/* Find data associated with key and write data to log*/
rc = myhash.find(key: 'Airedale');
if (rc = 0) then
put d=;
else
put 'Key Airedale not found';
run;
data _null_;
length k $15;
length d $15;
if _N_ = 1 then do;
/* Declare and instantiate hash object "myhash". */
/* Set hash table size to 16. */
declare hash myhash(hashexp: 4);
rc = myhash.defineKey('k');
rc = myhash.defineData('d');
rc = myhash.defineDone( );
/* avoid uninitialized variable notes */
call missing(k, d);
end;
/* Create constant key and data values */
rc = myhash.add(key: 'Labrador', data: 'Retriever');
rc = myhash.add(key: 'Airedale', data: 'Terrier');
rc = myhash.add(key: 'Standard', data: 'Poodle');
rc = myhash.find(key: 'Airedale');
/* Find data associated with key and write data to log*/
if (rc = 0) then
put d=;
else
put 'Key Airedale not found';
run;
data x; retain j 999; do i = 1 to 20; output; end; run; /* Using the WHERE option. */ data _null_; length i 8; dcl hash h(dataset: 'x (where =(i > 10))', ordered: 'a'); h.definekey('i'); h.definedone(); h.output(dataset: 'out'); run; /* Using the DROP option. */ data _null_; length i 8; dcl hash h(dataset: 'x (drop = j)', ordered: 'a'); h.definekey(all: 'y'); h.definedone(); h.output(dataset: 'out (where =( i < 8))'); run; /* Using the FIRSTOBS option. */ data _null_; length i j 8; dcl hash h(dataset: 'x (firstobs=5)', ordered: 'a'); h.definekey(all: 'y'); h.definedone(); h.output(dataset: 'out'); run; /* Using the OBS option. */ data _null_; length i j 8; dcl hash h(dataset: 'x (obs=5)', ordered: 'd'); h.definekey(all: 'y'); h.definedone(); h.output(dataset: 'out (rename =(j=k))'); run;
data key;
length key data 8;
input key data;
datalines;
1 10
2 11
3 20
5 5
4 6
run;
data _null_;
length key data r i sum 8;
length ks 8;
i = 0;
dcl hash h(dataset:'key', suminc: 'i', keysum: 'ks');
h.definekey('key');
h.definedata('key', 'data');
h.definedone();
i = 1;
do key = 1 to 5;
rc = h.find();
end;
do key = 1 to 3;
rc = h.find();
end;
rc = h.output(dataset:'out');
run;
proc print data=out;
run;