Sample 24768: Subset a data set based on matching a key variable from another data set
Load a lookup table into a hash object. For every
observation of a large data set, use the FIND
method to determine if the key value has a match in the
hash object. Output the observation if a match exists.
Note:
For detailed information regarding object dot programming
in the DATA step, please refer to SAS 9.3 Component Objects: Reference.
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 data sets SMALL and LARGE */
data small;
input id name $5. phone x;
datalines;
125 Bob 5550001 1
120 Sue 9991000 3
100 Fran 5552000 4
295 Sam 9991100 2
285 Jeff 5551100 8
275 Ed 9999999 2
300 Ron 5551111 4
;
data large;
do id=100 to 300 by 5;
output;
end;
run;
data match;
/* Specify the variables defined in the hash object for the DATA set's PDV */
length id 8 name $5 phone 8;
/* On the first iteration of the DATA step, create a new hash object called */
/* H. Use the DATASET: argument tag to load WORK.SMALL into H. Use the */
/* DEFINEKEY method to define ID as the 'lookup key'. Define NAME and PHONE */
/* as the key values associated values using the DEFINEDATA method. */
/* DEFINEDONE indicates you are finished defining the hash object H. To */
/* avoid 'variable is uninitialized' NOTES in the SAS log, use CALL MISSING */
/* to quickly initialize the key and data values to missing in the PDV of */
/* the data set MATCH. */
if _N_ = 1 then do;
declare hash h(dataset: "work.small");
h.defineKey('id');
h.defineData('name','phone');
h.defineDone();
call missing(id, name, phone);
end;
/* Use the SET statement to read in observations from LARGE */
set large;
/* FIND looks for a match on the ID value coming from LARGE in */
/* the hash object H. If a match is found, the return code is */
/* zero. If a match is found, then OUTPUT. */
rc = h.find();
if (rc = 0) then output;
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 id name phone rc
1 100 Fran 5552000 0
2 120 Sue 9991000 0
3 125 Bob 5550001 0
4 275 Ed 9999999 0
5 285 Jeff 5551100 0
6 295 Sam 9991100 0
7 300 Ron 5551111 0
Load a lookup table into a hash object. For every
observation of a large data set, use the FIND
method to determine if the key value has a match in the
hash object. Output the observation if a match exists.
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Component Objects SAS Reference ==> Component Objects ==> hash object
|
Date Modified: | 2012-04-02 14:51:47 |
Date Created: | 2004-09-30 14:09:12 |
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 |