Sample 24648: Perform a fuzzy merge within a range using DATA Step component objects
Use the hash iterator to search a hash object for a value
that is within a range from a second data set.
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.
/* Create sample data containing variable MILEAGE */
data one;
input lastname: $15. typeofcar: $15. mileage;
datalines;
Jones Toyota 7435
Smith Toyota 13001
Jones2 Ford 3433
Smith2 Toyota 15032
Shepherd Nissan 4300
Shepherd2 Honda 5582
Williams Ford 10532
;
/* Create sample data containing STARTRANGE and ENDRANGE */
data two;
input startrange endrange typeofservice & $35.;
datalines;
3000 5000 oil change
5001 6000 overdue oil change
6001 8000 oil change and tire rotation
8001 9000 overdue oil change
9001 11000 oil change
11001 12000 overdue oil change
12001 14000 oil change and tire rotation
14001 14999 overdue oil change
15000 15999 15000 mile check
;
/* Load WORK.TWO into a hash table to perform a lookup for */
/* each value of MILEAGE from WORK.ONE. */
data out(keep=lastname typeofcar mileage typeofservice);
/* Define the lengths and types of the variables in the data */
/* set which is loaded into the hash table. */
length startrange endrange 8 typeofservice $35;
/* On the first iteration of the step, declare and instantiate */
/* the hash object H and hash iterator HITER. Use the DATASET: */
/* argument tag to load WORK.TWO into H. Define the 'lookup key' */
/* for the hash table H using DEFINEKEY. Define the KEY's */
/* associated values with DEFINEDATA. The KEY is not */
/* automatically output to the data set, so add STARTRANGE as */
/* data so it will be included in WORK.OUT. */
if _n_=1 then do;
declare hash h(dataset:'two');
h.definekey('startrange');
h.definedata('startrange','endrange','typeofservice');
declare hiter hiter('h');
h.definedone();
/* Use CALL MISSING to initialize the specified variables to missing */
call missing(startrange, endrange, typeofservice);
end;
set one;
/* Move to the first item in the hash table using the method FIRST()*/
rc=hiter.first();
/* For each observation in WORK.ONE, iterate through the hash table */
/* until the value of MILEAGE is between the value of STARTRANGE */
/* and ENDRANGE. */
do while (rc=0);
if startrange le mileage le endrange then leave;
rc=hiter.next();
end;
run;
proc print;
run;
Obs typeofservice lastname typeofcar mileage
1 oil change and tire rotation Jones Toyota 7435
2 oil change and tire rotation Smith Toyota 13001
3 oil change Jones2 Ford 3433
4 15000 mile check Smith2 Toyota 15032
5 oil change Shepherd Nissan 4300
6 overdue oil change Shepherd2 Honda 5582
7 oil change Williams Ford 10532
Use the hash iterator to search a hash object for a value
that is within a range from a second data set.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Component Objects SAS Reference ==> Component Objects ==> hash iterator object
|
| Date Modified: | 2006-08-02 03:03:14 |
| Date Created: | 2004-09-30 14:09:00 |
Operating System and Release Information
| 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 |