Sample 26064: Performing a table lookup when the lookup data set is not indexed
Load the data that will be used to determine which subset an observation belongs to into an array. Read the input data set sequentially, performing a lookup into the array structure. Compare values in the current observation to the appropriate values from the array to determine whether they fall within a specified range. Then write the current observation to the appropriate data set.
Note: This is Example 2.5 from Combining and Modifying SAS Data Sets - Examples.
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.
/* BTEAM contains data on team members' height, weight, and body type. IDEAL */
/* shows the ideal male weight for each height, based on one of three body */
/* types. IDEAL is loaded into an array. */
data bteam;
input lname : $10. sex $ height weight type;
datalines;
Adams M 67 160 2
Alexander M 69 115 1
Apple M 69 139 1
Arthur F 66 125 2
Avery M 66 152 2
Barefoot M 68 158 2
Baucom M 70 170 3
Blair M 69 133 1
Blalock M 68 148 2
Bostic F 74 170 3
;
data ideal;
input height small medium large;
datalines;
66 126 138 149
67 130 141 154
68 134 145 158
69 138 149 162
70 142 153 167
71 146 157 172
72 150 161 177
73 154 165 181
74 158 169 185
75 162 173 189
;
/* The objective is to create subsets from the BTEAM data set, based on */
/* whether a male team member is considered to be in shape or out of shape. */
/* The IDEAL data set contains three WEIGHT values for each HEIGHT, based on */
/* an ideal male weight for each body TYPE. These values are used to */
/* determine whether an observation from the BTEAM data set should be */
/* written to the INSHAPE or OUTSHAPE data set. */
data inshape outshape;
keep lname height weight type;
/* Array WT is a two-dimensional array. The subscript (66:75,3) denotes the */
/* rows and columns. More specifically, there are 10 rows which will be */
/* referenced as rows 66 through 75 rather than the default 1 through 10. */
/* Defining the rows in this manner allows for the association with HEIGHT */
/* values ranging from 66 through 75. The 3 columns will be referenced */
/* as columns 1 through 3, corresponding to TYPEs 1 through 3. Temporary */
/* array elements are not written to the output data set and are */
/* automatically retained. */
array wt(66:75,3) _temporary_;
/* On the first iteration, load the two-dimensional temporary array from the */
/* information in IDEAL. */
if _n_=1 then
do i=1 to all;
set ideal nobs=all;
wt(height,1)=small;
wt(height,2)=medium;
wt(height,3)=large;
end;
/* Read from BTEAM. */
set bteam;
/* Determine whether a male qualifies as in shape or out of shape and write */
/* the observation to the appropriate data set. */
if sex='M' and 3 ge type ge 1 and 75 ge height ge 66;
if wt(height,type)-5 le weight le wt(height,type)+5
then output inshape;
else output outshape;
run;
proc print data=inshape;
title 'INSHAPE';
run;
proc print data=outshape;
title 'OUTSHAPE';
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.
INSHAPE
Obs height lname weight type
1 69 Apple 139 1
2 70 Baucom 170 3
3 69 Blair 133 1
4 68 Blalock 148 2
OUTSHAPE
Obs height lname weight type
1 67 Adams 160 2
2 69 Alexander 115 1
3 66 Avery 152 2
4 68 Barefoot 158 2
Subset the observations from one data set into one of two output data sets, based on specific criteria.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step Data Management ==> Manipulation and Transformation ==> Array processing
|
| Date Modified: | 2006-06-28 03:02:57 |
| Date Created: | 2006-06-21 10:38:16 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |