Sample 24777: How to do a Table lookup using a DO loop with a DO UNTIL
Combining two SAS data sets using a DO loop. Use a DO UNTIL to SET a data set until a match is found.
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 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
;
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
;
/* Read the first observation from the SAS data set outside the */
/* DO loop. Assign the FOUND variable to 0. Start the DO loop */
/* reading observations from the SAS data set inside the DO loop.*/
/* Process the IF condition; if the IF condition is true, OUTPUT */
/* the observation and set the FOUND variable to 1. Assigning */
/* the FOUND variable to 1 will cause the DO loop to stop */
/* processing because of the UNTIL (FOUND) that is coded on the */
/* DO loop. Go back to the top of the DATA step and read the */
/* next observation from the data set outside the DO loop and */
/* process through the DATA step again until all observations */
/* from the data set outside the DO loop have been read. */
data combine;
set one;
found=0;
do i=1 to nobs until (found);
set two point=i nobs=nobs;
if startrange <= mileage <= endrange then do;
output;
found=1;
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.
Obs lastname typeofcar mileage found startrange endrange typeofservice
1 Jones Toyota 7435 0 6001 8000 oil change and tire rotation
2 Smith Toyota 13001 0 12001 14000 oil change and tire rotation
3 Jones2 Ford 3433 0 3000 5000 oil change
4 Smith2 Toyota 15032 0 15000 15999 15000 mile check
5 Shepherd Nissan 4300 0 3000 5000 oil change
6 Shepherd2 Honda 5582 0 5001 6000 overdue oil change
7 Williams Ford 10532 0 9001 11000 oil change
Combining two SAS data sets using a DO loop. Use a DO UNTIL to SET a data set until a match is found.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Statements ==> File-handling ==> SET ==> with POINT= SAS Reference ==> Statements ==> File-handling ==> SET Data Management ==> Manipulation and Transformation ==> Combining and Modifying Data Sets
|
| Date Modified: | 2005-12-08 11:34:33 |
| Date Created: | 2004-09-30 14:09:13 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |