Valid in: | DATA step |
Category: | File-handling |
Type: | Executable |
Restriction: | Cannot modify the descriptor portion of a SAS data set, such as adding a variable |
Note: | If you modify a password-protected data set, specify the password with the appropriate data set option (ALTER= or PW=) within the MODIFY statement, and not in the DATA statement. |
CAUTION: |
Damage
to the SAS data set can occur if the system terminates abnormally
during a DATA step that contains the MODIFY statement.
|
For sequential and matching access, the master data set
can be a SAS data file, a
For random access using POINT=, the master data set must be a SAS data file or an SQL view that references a SAS data file.
For direct access using KEY=, the master data set can be a SAS data file or the DBMS engine for the LIBNAME statement. If it is a SAS file, it must be indexed and the index name must be specified on the KEY= option.
For a DBMS, the KEY= is set to the keyword DBKEY and the column names to use as an index must be specified on the DBKEY= data set option. These column names are used in constructing a WHERE expression that is passed to the DBMS.
This variable is not added to any data set.
If duplicates exist in the master file, only the first occurrence is updated unless you use a DO-LOOP to execute a SET statement for the data set that is listed on the KEY=option for all duplicates in the master data set.
If duplicates exist in the transaction data set, and they are consecutive, use the UNIQUE option to force the search for a match in the master data set to begin at the top of the index. Write an accumulation statement to add each duplicate transaction to the observation in master. Without the UNIQUE option, only the first duplicate transaction observation updates the master.
If the duplicates in the transaction data set are not consecutive, the search begins at the beginning of the index each time, so that each duplicate is applied to the master. Write an accumulation statement to add each duplicate to the master.
You can use POINT= with compressed data sets only if the data set was created with the POINTOBS= data set option set to YES, the default value.
You can use the random access method on compressed files only with SAS version 7 and beyond.
Because POINT= reads only the specified observations, SAS cannot detect an end-of-file condition as it would if the file were being read sequentially. Because detecting an end-of-file condition terminates a DATA step automatically, failure to substitute another means of terminating the DATA step when you use POINT= can cause the DATA step to go into a continuous loop.
data master; modify master trans; by key; if _iorc_=0 then replace; else output; run;
data master; modify master; x=1; replace; replace; run;
A
through Z
for
the transaction data set, SAS updates numeric variables in the master
data set to that value.
libname invty 'SAS-library';
data invty.stock(index=(partno)); input PARTNO $ DESC $ INSTOCK @17 RECDATE date7. @25 PRICE; format recdate date7.; datalines; K89R seal 34 27jul95 245.00 M4J7 sander 98 20jun95 45.88 LK43 filter 121 19may96 10.99 MN21 brace 43 10aug96 27.87 BC85 clamp 80 16aug96 9.55 NCF3 valve 198 20mar96 24.50 KJ66 cutter 6 18jun96 19.77 UYN7 rod 211 09sep96 11.55 JD03 switch 383 09jan97 13.99 BV1E timer 26 03jan97 34.50 ;
data invty.stock; modify invty.stock; recdate=today(); run; proc print data=invty.stock noobs; title 'INVTY.STOCK'; run;
data addinv; input PARTNO $ NWSTOCK; datalines; K89R 55 M4J7 21 LK43 43 MN21 73 BC85 57 NCF3 90 KJ66 2 UYN7 108 JD03 55 BV1E 27 ;
data newp; input TOOL_OBS NEWP; datalines; 1 251.00 2 49.33 3 12.32 4 30.00 5 15.00 6 25.75 7 22.00 8 14.00 9 14.32 10 35.00 ;
libname invty 'SAS-library';
data invty.stock; set newp; modify invty.stock point=tool_obs nobs=max_obs; if _error_=1 then do; put 'ERROR occurred for TOOL_OBS=' tool_obs / 'during DATA step iteration' _n_ / 'TOOL_OBS value might be out of range.'; _error_=0; stop; end; PRICE=newp; RECDATE=today(); run;
proc print data=invty.stock noobs; title 'INVTY.STOCK'; run;
data newinv; input PARTNO $ NWSTOCK; datalines; K89R 55 M4J7 21 M4J7 26 LK43 43 MN21 73 BC85 57 NCF3 90 KJ66 2 UYN7 108 JD03 55 BV1E 27 ;
M4J7
in
NEWINV:libname invty 'SAS-library';
/* This DATA step terminates with an error! */ data invty.stock; set newinv; modify invty.stock key=partno; INSTOCK=instock+nwstock; RECDATE=today(); run;
ERROR: No matching observation was found in MASTER data set. PARTNO=M4J7 NWSTOCK=26 DESC=sander INSTOCK=166 RECDATE=08DEC10 PRICE=45.88 _ERROR_=1 _IORC_=1230015 _N_=3 NOTE: The SAS System stopped processing this step because of errors. NOTE: There were 3 observations read from the data set WORK.NEWINV. NOTE: The data set INVTY.STOCK has been updated. There were 2 observations rewritten, 0 observations added and 0 observations deleted.
M4J7
in the
MASTER data set for each occurrence of M4J7
in
the SET data set. The updated result for M4J7
in
the output shows that both values of NWSTOCK from NEWINV for M4J7
are
added to the value of INSTOCK for M4J7
in
INVTY.STOCK. An accumulation statement sums the values; without it,
only the value of the last instance of M4J7
would
be the result in INVTY.STOCK.
data newship; input PARTNO $ DESC $ NWSTOCK @17 SHPDATE date7. @25 NWPRICE; datalines; K89R seal 14 14nov96 245.00 M4J7 sander 24 23aug96 47.98 LK43 filter 11 29jan97 14.99 MN21 brace 9 09jan97 27.87 BC85 clamp 12 09dec96 10.00 ME34 cutter 8 14nov96 14.50 ;
data invty.stock; set newship; modify invty.stock key=partno; select (_iorc_); when (%sysrc(_sok)) do; INSTOCK=instock+nwstock; RECDATE=shpdate; PRICE=nwprice; replace; end; when (%sysrc(_dsenom)) do; INSTOCK=nwstock; RECDATE=shpdate; PRICE=nwprice; output; _error_=0; end; otherwise do; put 'An unexpected I/O error has occurred.'/ 'Check your data and your program'; _error_=0; stop; end; end; run;