Sample 24787: Use a look-up table to 'chain' through another data set
Using SET with KEY= logic, read each observation from WORK.TWO looking for a match of OLD in the indexed data set WORK.ONE. When a match is found, assign the value of NEW into OLD and search again. 'Chain' through WORK.ONE until no new matches are 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.
data one(index=(old));
input old $ new $;
datalines;
123 456
456 789
444 999
789 ***
333 777
777 ***
;
data two;
input old $;
datalines;
123
444
333
789
;
data new;
set two;
length new $8 chain $20;
/* Reset with each new observation */
new= ' ';
chain=' ';
do until (done=1);
set one key=old;
/* If there is NO match on OLD */
if _iorc_=%sysrc(_dsenom) then do;
_error_=0;
done=1;
end;
/* If there is a match on OLD...*/
else if _iorc_=%sysrc(_sok) then do;
chain=trim(chain)||' '||old;
old=new;
if new='***' then output;
end;
end;
keep chain;
run;
proc print data=new;
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 chain
1 123 456 789
2 333 777
3 789
Using SET with KEY= logic, read each observation from WORK.TWO looking for a match of OLD in the indexed data set WORK.ONE. When a match is found, assign the value of NEW into OLD and search again. 'Chain' through WORK.ONE until no new matches are found.
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Statements ==> File-handling ==> SET SAS Reference ==> Statements ==> File-handling ==> SET ==> with KEY= Data Management ==> Manipulation and Transformation ==> Combining and Modifying Data Sets
|
| Date Modified: | 2006-02-07 03:03:11 |
| Date Created: | 2004-09-30 14:09:14 |
Operating System and Release Information
| SAS System | Base SAS | All | n/a | n/a |