When you work with data in SAS, you probably have a lot of data sets in many different libraries. Therefore, it is important to structure your libraries properly and keep the data sets in the correct libraries. This means we have to be able to efficiently move data between libraries. This example page shows you four different approaches to moving and copying data between libraries using PROC COPY and PROC DATASETS.
A common pitfall moving data between SAS libraries is using the Data Step. Probably because users are familiar with it and it allows you to easily read data from one library and write it to another. However, this approach is far from efficient. The Data Step reads data observation by observation. When we simply want to move entire data sets, there is no need to move data into memory for processing. Instead, we use high-level SAS procedures to move data sets without reading any data into memory. Below, four common examples of moving and copying data are presented.
Copy Specified SAS Data Set
First we copy a single data set from one library to another. I use the Copy Procedure and specify the IN= and OUT= options to be the libraries from which I want to copy data from and to respectively. Finally, I use the Select Statement to specify the names of the data sets I want to copy.
proc copy in=sashelp out=work memtype=data; select class; run;
Copy All SAS Data Sets
Next, I execute almost the same code as in the example above. However, here I omit the Select Statement. Consequently, all data sets in the IN library are copied to the OUT library.
proc copy in=sashelp out=work memtype=data; run;
Move Specified Data Set
Now, if we want to move a SAS data set from one library to another, removing the data set from its original library, we use the Datasets Procedure. The syntax is almost similar to the one of PROC COPY, though here we specify the IN and OUT libraries in a Copy Statement. The MOVE option tells SAS that we do not want to keep the data set in the IN library.
proc datasets nolist; copy in=work out=MyLib memtype=data move; select class; run;quit;
Move All Data Sets
Finally, in the same manner as in PROC COPY, when we omit the Select Statement, SAS moves all data sets from the IN to the OUT library. Specify the MEMTYPE=DATA option to tell SAS that only data sets are to be copied.
proc datasets nolist; copy in=work out=MyLib memtype=data move; run;quit;
When you want to move or copy data sets between SAS libraries, do not use the Data Step. Instead, use the high-level procedures PROC COPY and PROC DATASETS. They provide much more control over what data sets are to be moved and how. Furthermore, no data is actually processed, thus reducing elapsed time. This page shows you examples of four very common tasks of copying/moving data between libraries.
You can download the entire SAS program from this example page here.