The behavior of this sample can be modified to return the occurrence of all items by using the MULTIDATA argument on the DECLARE statement. The MULTIDATA argument was introduced with SAS 9.2. See sample 34757
For additional information, click on the link below.
SAS 9.2 Language Reference: Concepts, Using DATA Step Component Objects.
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/* Create sample starting data */
data test;
input key1 key2 data1;
datalines;
1 1 11
1 1 22
1 2 12
1 2 122
;
/***************************************************************************/
/* Duplicate KEY values are not allowed with hash objects. WORK.TEST does */
/* have duplicates for KEY1 and KEY2, so in this case, only the first */
/* occurrence of each key value will be loaded into the hash table H. */
/***************************************************************************/
data nodups;
/* On the first iteration of the data step declare and instantiate the */
/* hash object H. Use the argument_tag DATASET: to load WORK.TEST into */
/* H. Define KEY1 and KEY2 as key variables for H and store DATA1 as */
/* data in H. CALL MISSING initializes the DATA set variables KEY1, */
/* KEY2 and DATA1 to missing to avoid 'variable uninitialized' notes in */
/* the log. An assigment statement would work as well. */
if _n_ = 1 then do;
dcl hash h(dataset:'work.test');
h.defineKey('key1', 'key2');
h.definedata('data1');
h.defineDone();
call missing(key1, key2, data1);
end;
/* Assign values to the data set variables KEY1 and KEY2 */
key1=1;
key2=1;
/* Use the FIND method to locate matching KEY1 and KEY2 values in */
/* the hash object H. A zero result indicates a match was found. */
rc=h.find();
/* Use the NUM_ITEMS attribute to assign the number of items in the */
/* hash object H into the data set variable TOTALITEMS. */
totalitems = h.num_items;
/* Write the values of DATA1 and TOTALITEMS to the log. */
put data1=;
put totalitems=;
run;
/************************************************************************/
/* In this sample KEY1, KEY2 and DATA1 are defined as keys. Note DATA1 */
/* has 4 unique values so this time the hash object will have 4 items. */
/************************************************************************/
data dups;
if _n_ = 1 then do;
dcl hash h(dataset:'work.test');
h.defineKey('key1', 'key2', 'data1');
h.defineDone();
call missing(key1, key2, data1);
end;
totalitems = h.num_items;
put totalitems=;
run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
OUTPUT from WORK.NODUPS data1=11 totalitems=2 OUTPUT from WORK.DUPS totalitems=4
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Component Objects SAS Reference ==> Component Objects ==> hash object SAS Reference ==> Component Objects ==> hash object ==> DEFINEDONE SAS Reference ==> Component Objects ==> hash object ==> DEFINEKEY SAS Reference ==> Component Objects ==> hash object ==> NUM_ITEMS Attribute |
Date Modified: | 2012-04-02 13:35:51 |
Date Created: | 2004-09-30 14:08:56 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | Tru64 UNIX | 9.1 TS1M0 | |
OpenVMS Alpha | 9.1 TS1M0 | |||
Linux | 9.1 TS1M0 | |||
HP-UX IPF | 9.1 TS1M0 | |||
64-bit Enabled Solaris | 9.1 TS1M0 | |||
64-bit Enabled HP-UX | 9.1 TS1M0 | |||
64-bit Enabled AIX | 9.1 TS1M0 | |||
Microsoft Windows XP Professional | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Standard Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter Edition | 9.1 TS1M0 | |||
Microsoft Windows NT Workstation | 9.1 TS1M0 | |||
Microsoft Windows 2000 Professional | 9.1 TS1M0 | |||
Microsoft Windows 2000 Server | 9.1 TS1M0 | |||
Microsoft Windows 2000 Datacenter Server | 9.1 TS1M0 | |||
Microsoft Windows 2000 Advanced Server | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.1 TS1M0 | |||
Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.1 TS1M0 | |||
z/OS | 9.1 TS1M0 | |||
Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M0 |