Sample 24639: Find all observations of a SAS data set that do not match the first key value and do match the second key value
Use the hash iterator to find observations that match
the value of the second key by using an iterative do loop
to change the value of the first key.
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.
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 */
data test;
infile datalines truncover;
input family_id age name $15.;
datalines;
1 10 Abby
1 12 Carla
1 14 Gretel
2 10 Bertha
2 12 Diana
3 12 Ethel
3 15 Fran
4 10 Marie
4 11 Nancy
4 14 Zoe
;
data y;
/* Specify the LENGTH of variables in the hash object H */
length name $ 15 age 8;
drop rc;
/* On the first iteration of the DATA step, declare and instantiate a */
/* hash object named H. Use the DATASET: argument tag to load the */
/* data set WORK.TEST into H. Declare a hash iterator called ITER. */
/* Use the DEFINEKEY and DEFINEDATA methods to specify the key or */
/* 'lookup' variables and their associated variables. */
if _n_ = 1 then do;
dcl hash h(dataset:'work.test');
dcl hiter iter('h');
h.defineKey('family_id', 'age');
h.definedata('name','family_id', 'age');
h.defineDone();
/* Set NAME and AGE to missing to avoid 'variable is uninitialized' */
/* messages in the log. */
call missing(name,age);
end;
/* Execute this DO block as long as the NEXT method is successful. */
do while(iter.next() = 0);
/* When FAMILIY_ID=1, reassign its value. Since both FAMILY_ID */
/* and AGE are key values, the FIND method will be looking for */
/* a match on both values. Output observations where the value */
/* of AGE is found in the both the BY-Group where FAMILY_ID is */
/* equal to 1, and the BY-Groups where FAMILY_ID is greater */
/* than 1. */
if family_id=1 then do family_id=2 to 4;
rc = h.find();
if (rc = 0) then output;
end;
end;
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.
family_
Obs name age id
1 Diana 12 2
2 Ethel 12 3
3 Bertha 10 2
4 Marie 10 4
5 Zoe 14 4
Use the hash iterator to find observations that match
the value of the second key by using an iterative do loop
to change the value of the first key.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Component Objects
|
| Date Modified: | 2006-06-14 03:02:55 |
| Date Created: | 2004-09-30 14:09:00 |
Operating System and Release Information
| Product Family | Product | Host | Starting Release | Ending Release |
| 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 |