![]() | ![]() | ![]() | ![]() | ![]() |
This sample works in SAS 9.2 only
This behavior cannot be used in SAS 9.1 where key values must be unique. See Sample 24594: Count the number of items in a hash table
Note: For detailed information regarding object dot programming in the DATA step, please refer to SAS 9.2 Language Reference: Concepts, Using DATA Step Component Objects.
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 are defined as keys. */
/************************************************************************/
data dups;
if _n_ = 1 then do;
dcl hash h(dataset:'work.test', multidata:'yes');
h.defineKey('key1', 'key2');
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.
1
2 /* Create sample starting data */
3
4 data test;
5 input key1 key2 data1;
6 datalines;
NOTE: The data set WORK.TEST has 6 observations and 3 variables.
NOTE: DATA statement used (Total process time):
real time 0.07 seconds
cpu time 0.03 seconds
13 ;
14
15 /***************************************************************************/
16 /* Duplicate KEY values are not allowed with hash objects. WORK.TEST does */
17 /* have duplicates for KEY1 and KEY2, so in this case, only the first */
18 /* occurrence of each key value will be loaded into the hash table H. */
19 /***************************************************************************/
20
21 data nodups ;
22
23 /* On the first iteration of the data step declare and instantiate the */
24 /* hash object H. Use the argument_tag DATASET: to load WORK.TEST into */
25 /* H. Define KEY1 and KEY2 as key variables for H and store DATA1 as */
26 /* data in H. CALL MISSING initializes the DATA set variables KEY1, */
27 /* KEY2 and DATA1 to missing to avoid 'variable uninitialized' notes in */
28 /* the log. An assigment statement would work as well. */
29 if _n_ = 1 then do;
30 dcl hash h(dataset:'work.test');
31 h.defineKey('key1', 'key2');
32 h.definedata('data1');
33 h.defineDone();
34 call missing(key1, key2, data1);
35 end;
36
37 /* Assign values to the data set variables KEY1 and KEY2 */
38 key1=1;
39 key2=1;
40
41 /* Use the FIND method to locate matching KEY1 and KEY2 values in */
42 /* the hash object H. A zero result indicates a match was found. */
43 rc=h.find();
44
45 /* Use the NUM_ITEMS attribute to assign the number of items in the */
46 /* hash object H into the data set variable TOTALITEMS. */
47 totalitems = h.num_items;
48
49 /* Write the values of DATA1 and TOTALITEMS to the log. */
50 put data1=;
51 put totalitems=;
52 run;
NOTE: There were 6 observations read from the data set WORK.TEST.
data1=11
totalitems=2
NOTE: The data set WORK.NODUPS has 1 observations and 5 variables.
NOTE: DATA statement used (Total process time):
real time 0.07 seconds
cpu time 0.06 seconds
53
54
55 /************************************************************************/
56 /* In this sample KEY1, KEY2 and DATA1 are defined as keys. Note DATA1 */
57 /* has 4 unique values so this time the hash object will have 4 items. */
58 /************************************************************************/
59
60 data dups ;
61 if _n_ = 1 then do;
62 dcl hash h(dataset:'work.test', multidata:'yes');
63 h.defineKey('key1', 'key2');
64 h.definedata('data1');
65 h.defineDone();
66 call missing(key1, key2, data1);
67 end;
68 /* Assign values to the data set variables KEY1 and KEY2 */
69 key1=1;
70 key2=1;
71
72 /* Use the FIND method to locate matching KEY1 and KEY2 values in */
73 /* the hash object H. A zero result indicates a match was found. */
74 rc=h.find();
75 rc = h.has_next(result:R);
76 do while (rc = 0);
77 put key1= key2= data1=;
78 rc = h.find_next();
79 end;
80
81 /* Write the values of DATA1 and TOTALITEMS to the log. */
82 put data1=;
83 totalitems = h.num_items;
84 put totalitems=;
85 run;
NOTE: There were 6 observations read from the data set WORK.TEST.
key1=1 key2=1 data1=11
key1=1 key2=1 data1=22
key1=1 key2=1 data1=33
data1=33
totalitems=6
NOTE: The data set WORK.DUPS has 1 observations and 6 variables.
NOTE: DATA statement used (Total process time):
real time 0.04 seconds
cpu time 0.04 seconds
NOTE: SAS Institute Inc., SAS Campus Drive, Cary, NC USA 27513-2414
NOTE: The SAS System used:
real time 13.97 seconds
cpu time 3.34 seconds
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Component Objects ==> hash object ==> FIND SAS Reference ==> Component Objects ==> hash object ==> NUM_ITEMS Attribute SAS Reference ==> Component Objects ==> hash object ==> DEFINEDATA SAS Reference ==> Component Objects ==> hash object ==> DEFINEKEY |
| Date Modified: | 2009-04-06 12:18:45 |
| Date Created: | 2009-02-10 15:27:09 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | z/OS | 9.2 TS1M0 | |
| Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.2 TS1M0 | |||
| Microsoft Windows Server 2003 Datacenter 64-bit Edition | 9.2 TS1M0 | |||
| Microsoft Windows Server 2003 Enterprise 64-bit Edition | 9.2 TS1M0 | |||
| Microsoft Windows XP 64-bit Edition | 9.2 TS1M0 | |||
| Microsoft® Windows® for x64 | 9.2 TS1M0 | |||
| Microsoft Windows Server 2003 Datacenter Edition | 9.2 TS1M0 | |||
| Microsoft Windows Server 2003 Enterprise Edition | 9.2 TS1M0 | |||
| Microsoft Windows Server 2003 Standard Edition | 9.2 TS1M0 | |||
| Microsoft Windows XP Professional | 9.2 TS1M0 | |||
| Windows Vista | 9.2 TS1M0 | |||
| 64-bit Enabled AIX | 9.2 TS1M0 | |||
| 64-bit Enabled HP-UX | 9.2 TS1M0 | |||
| 64-bit Enabled Solaris | 9.2 TS1M0 | |||
| HP-UX IPF | 9.2 TS1M0 | |||
| Linux | 9.2 TS1M0 | |||
| Linux for x64 | 9.2 TS1M0 | |||
| OpenVMS on HP Integrity | 9.2 TS1M0 | |||
| Solaris for x64 | 9.2 TS1M0 | |||





