![]() | ![]() | ![]() | ![]() | ![]() |
SAS users often need to copy or backup one or more SAS data sets. There are several ways to copy a data set. However, you should be aware that the different methods do not produce the same results. Here's a tip that may provide some help so you can avoid unsuspected surprises when copying a data set that has one or more indexes associated with it.
The SAS System provides several ways to copy a data set, but not all methods are designed to copy a data set with one or more indexes associated with it.
Let's first look at a commonly used approach -- one that may not produce the desired results. The DATA and SET statements provide an easy way to copy a data set by performing a simple process of reading and writing one observation at a time to either an existing or new data set. Although this approach is convenient at times, it is not the most efficient technique for making data set copies and more importantly it can be dangerous particularly when the data set has one or more indexes associated with it.
The DATA and SET statements can be used to copy the observations in one data set to another (or the same data set), but it has no way of copying indexes that are associated with the data set. All it does is copy the data minus any indexes. The only real advantage for using this approach is that it copies data sets within the same library or changes the name of a data set as it is being copied.
A better and more efficient approach that will achieve all of your copying objectives is to use the COPY procedure. Its purpose is to copy data sets quickly and completely including any associated indexes. Although it cannot make copies within the same library or change the name of the data set as it is copied, these limitations should NOT limit your use of this procedure for all your copying and backup needs.
The following syntax illustrates how the COPY procedure is used:
PROC COPY IN=libref OUT=libref MTYPE=(DATA PROGRAM) ; SELECT dataset1 dataset2 ... datasetn ; |
The IN= and OUT= parameters specify the input and output locations using the assigned librefs. The MTYPE= parameter, if specified, informs the SAS System to copy only data sets (and indexes) and not any other type of file that can be in a SAS data library. Other values that can be specified for MTYPE= include CATALOG for catalogs, PROGRAM for compiled DATA steps, and VIEW for views. The SELECT statement is optional and if present tells the SAS System what data sets, or files, to copy from the input location to the output location. If SELECT is omitted, the COPY procedure copies all files, including indexes, from the input to output location.
Another way to copy data sets and indexes is by using the DATASETS procedure. The following syntax illustrates how the DATASETS procedure is used:
PROC DATASETS LIBRARY=libref MTYPE=DATA ; COPY OUT=libref ; SELECT dataset1 dataset2 ... datasetn ; |
The LIBRARY= parameter specifies the input location using the assigned libref. The MTYPE= parameter works the same as in the COPY procedure. The COPY OUT= statement specifies the output location using the assigned libref. The SELECT statement works the same as in the COPY procedure.
This tip is from Kirk Paul Lafler, Senior Consultant and founder of Software Intelligence Corp., a SAS Institute Quality Partner organization in Spring Valley, California. Lafler has 22 years of SAS programming and training experience, and is active in several SAS users groups.
If you have a SAS tip you'd like to share, send it to techeditor@sas.com.
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.
PROC COPY IN=libref OUT=libref MTYPE=(DATA PROGRAM) ;
SELECT dataset1 dataset2 ... datasetn ;
PROC DATASETS LIBRARY=libref MTYPE=DATA ;
COPY OUT=libref ;
SELECT dataset1 dataset2 ... datasetn ;
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.
Type: | Sample |
Topic: | SAS Reference ==> Procedures ==> COPY Non SAS Authors ==> Kirk Paul Lafler SAS Reference ==> Procedures ==> DATASETS |
Date Modified: | 2005-01-31 10:47:35 |
Date Created: | 2004-10-08 08:21:33 |
Product Family | Product | Host | SAS Release | |
Starting | Ending | |||
SAS System | Base SAS | All | n/a | n/a |