![]() | ![]() | ![]() | ![]() | ![]() |
The first method uses the hash iterator to search a hash object for all items that have the same value of one key and different values of a second key. The entire hash table is processed by the iterator object.
The second method uses new methods available in SAS 9.2 to find the key value needed. This method does not use a hash iterator object. Only the rows that match the specified key value are processed.
Note:
For detailed information regarding object dot programming
in the DATA step, please refer to SAS 9.1 Language
Reference: Concepts, Using DATA Step Component Objects or SAS(R) 9.2 Language Reference: Dictionary: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.
Method 2 - SAS 9.2 permits duplicate data items for a key value. This allows you to read only the section of the table with the key that you request.
Each method can be run independently.
/* METHOD 1 */
/* Create sample data */
data work.sales;
input prod $1-5 size qty $10-15;
datalines;
tie 10 39007
tie 11 398487
shirt 20 384223
pants 30 329559
pants 31 329559
;
data tie1;
/* On the first iteration of the DATA step, declare the hash object H */
/* and load WORK.SALES. The ORDERED: argument tag indicates the values */
/* should be loaded in sorted order, ascending. Declare the hash iterator */
/* ITER. Since duplicate keys are not allowed in a hash table, the */
/* variables PROD and SIZE are both defined as the key variables. Key */
/* variables are not output by default, so they are also defined as */
/* associated data, along with QTY. CALL MISSING intializes the values */
/* to missing to avoid a message in the log that these variables are */
/* unitialized. */
if _N_ = 1 then do;
length prod $10 qty $6 size 8;
declare hash h(dataset: 'work.sales', ordered: 'yes');
declare hiter iter('h');
/* Define key and data variables */
h.defineKey('prod', 'size');
h.defineData('qty','prod','size');
h.defineDone();
call missing(prod,qty,size);
end;
/* The FIND() method looks for the value of BOTH keys. */
/* Since we are interested in the value of only one key, */
/* we can use the iterator to iterate through the */
/* hash object and output data values where the key */
/* PROD='tie'. ' */
rc = iter.first();
do while (rc = 0);
if prod='tie' then output;
rc = iter.next();
end;
run;
proc print;
run;
/* METHOD 2 */
/* Create sample data */
data work.sales;
input prod $1-5 size qty $10-15;
datalines;
tie 10 39007
tie 11 398487
shirt 20 384223
pants 30 329559
pants 31 329559
;
data tie2;
/* On the first iteration of the DATA step, declare the hash object H */
/* and load WORK.SALES. The ORDERED: argument tag indicates the values */
/* should be loaded in sorted order, ascending. Declare the hash iterator */
/* ITER. Duplicate keys are now allowed in a hash table, the */
/* variables PROD is defined as the key variable. Key */
/* variables are not output by default, so it is also defined as */
/* associated data, along with QTY. */
if _N_ = 1 then do;
if 0 then set work.sales; /* shorthand method to define variables */
declare hash h(dataset: 'work.sales', ordered: 'yes', multidata:'Y');
/* Define key and data variables */
h.defineKey('prod' );
h.defineData('prod','size','qty');
h.defineDone();
end;
/* The FIND() method looks for the value of the key. */
/* use has_next method to see if multiple data values for this key */
rc = h.find(key:'tie');
if rc = 0 then do;
output;
rc2 = h.has_next(RESULT:R);
do while (R ne 0);
rc = h.find_next();
output;
rc2 = h.has_next(RESULT:R);
end;
end;
keep prod size qty;
run;
proc print;
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.
Obs prod size qty 1 tie 10 39007 2 tie 11 398487
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Component Objects SAS Reference ==> Component Objects ==> hash iterator object |
| Date Modified: | 2010-05-17 13:44:01 |
| Date Created: | 2004-09-30 14:09:00 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | Tru64 UNIX | 9.1 TS1M0 | n/a |
| OpenVMS Alpha | 9.1 TS1M0 | n/a | ||
| HP-UX IPF | 9.1 TS1M0 | n/a | ||
| Linux | 9.1 TS1M0 | n/a | ||
| 64-bit Enabled Solaris | 9.1 TS1M0 | n/a | ||
| 64-bit Enabled HP-UX | 9.1 TS1M0 | n/a | ||
| 64-bit Enabled AIX | 9.1 TS1M0 | n/a | ||
| Microsoft® Windows® for 64-Bit Itanium-based Systems | 9.1 TS1M0 | n/a | ||
| z/OS | 9.1 TS1M0 | n/a | ||





