![]() | ![]() | ![]() | ![]() | ![]() |
Source: "Combining and Modifying SAS Data Sets" Example 3.6.
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.
/* Both X and Y are common to the MASTER and TRANS data sets. MASTER */
/* contains multiple obseravtions with duplicate values for the key */
/* variable X. Because the program depends on directly accessing */
/* observations in MASTER by using KEY=X, MASTER must be indexed on */
/* the variable X. */
data master (index=(x));
input x y;
datalines;
1 2
1 3
2 4
3 5
1 2
;
data trans;
input x y;
datalines;
1 8
3 9
5 2
;
/* Open MASTER for update and execute the iterative DO loop to */
/* sequentially process observations from TRANS using direct access */
/* by observation number. */
data master;
do p = 1 to totobs;
flag = 0;
_IORC_ = 0;
set trans(keep=x) point=p nobs=totobs;
/* Update MASTER in place based on the value of the key variable X. */
/* The DO WHILE loop executes and processes observations from MASTER */
/* as long as values of X in MASTER match the current value of X in TRANS. */
do while(_IORC_=%sysrc(_sok));
modify master key=x;
/* When the value of _IORC_ corresponds to _SOK, the value of X in the */
/* observation being read from MASTER matches the current X value from TRANS. */
/* The value of the POINT= variable allows you to reread the current */
/* observation from TRANS, this time reading all of the variables so their */
/* values can overlay the existing values from MASTER. */
select (_IORC_);
when (%sysrc(_sok)) do;
set trans point=p;
flag=1;
replace;
end;
/* When the value of _IORC_ corresponds to _DSENOM, no observation in */
/* MASTER contain the current value of X. The _ERROR_ automatic variable */
/* is reset to 0 to prevent an error condition that would write the contents */
/* of the program data vector to the SAS log. The value of FLAG determines */
/* which note is written to the log. */
when (%sysrc(_dsenom)) do;
if flag then put
'NOTE: No more matches for KEY = ' x;
ELSE PUT 'NOTE: No match for KEY = ' x;
_ERROR_ = 0;
end;
/* When _IORC_ corresponds to anything other than _DSENOM or _SOK, an */
/* unexpected condition has been encountered, so an error message is written */
/* to the SAS log and the STOP statement terminates the DATA step. _ERROR_ */
/* is reset to zero to prevent an error condition that would dump the contents */
/* of the PDV to the SAS log. The second STOP statement is necessary to end the */
/* DATA step upon exiting the iterative DO loop because there is no end-of-file */
/* condition to step the DATA step when the SET statement uses the POINT=. */
otherwise do;
put 'ERROR: _IORC_ = ' _IORC_ / 'Program halted.';
_ERROR_ = 0;
stop;
end;
end; /* ends SELECT group */
end; /* ends DO WHILE loop */
end; /* ends iterative DO loop */
stop;
run;
proc print data=master;
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 x y 1 1 8 2 1 8 3 2 4 4 3 9 5 1 8
| Type: | Sample |
| Topic: | SAS Reference ==> DATA Step SAS Reference ==> Statements ==> File-handling ==> MODIFY SAS Reference ==> Statements ==> File-handling ==> MODIFY ==> with KEY= Common Programming Tasks |
| Date Modified: | 2008-01-25 14:33:59 |
| Date Created: | 2004-09-30 14:08:59 |
| Product Family | Product | Host | SAS Release | |
| Starting | Ending | |||
| SAS System | Base SAS | All | n/a | n/a |




