You may need to move or copy data between SAS libraries as a SAS programmer. This article shows you different options to move or copy datasets in SAS.
Using the data step below, you may be familiar with moving or copying data.
Using the Data Step is the most widely used method for copying SAS datasets. However, this approach is straightforward but not efficient.
In SAS, the Data Step reads data observation by observation. When we want to move entire data sets, we need not move the entire data into memory for processing. Instead, we can use high-level SAS procedures to move data sets without reading data into memory.
A more efficient method to move or copy datasets between libraries is the PROC COPY and PROC DATASETS.
Coping SAS Data Set
You can use the PROC COPY Procedure or the COPY statement in the PROC DATASET procedure to copy all or specific SAS data sets from one library to another.
When copying data sets, SAS duplicates the contents of each file, including the descriptor information, and updates information in the directory for each library.
During the copy process, SAS automatically writes the data from the input library into an output data set of the same name. If duplicate data set names to exist, you do not receive a warning message before copying starts.
The MEMTYPE=DATA
option tells SAS to copy only the SAS datasets.
Copying a single dataset using the PROC COPY Procedure
The below example copies the cars dataset from SASHELP to the WORK library using the PROC COPY Procedure.
proc datasets nolist;
copy in=sashelp out=work memtype=data;
select cars;
run;
Notes in the SAS log verify that the dataset is copied.
NOTE: Copying SASHELP.CARS to WORK.CARS (memtype=DATA). NOTE: There were 428 observations read from the data set SASHELP.CARS. NOTE: The data set WORK.CARS has 428 observations and 15 variables.
Copying a single dataset using the COPY Statement in PROC Dataset
proc datasets nolist;
copy in=sashelp out=work memtype=data;
select cars;
run;
Copy All SAS Data Sets or Copying an Entire Library using Proc Copy
To copy all SAS datasets, you must specify the source library name in the IN and the destination library in the OUT statement. This will copy all data sets in the IN library to the OUT library.
proc copy in=sashelp out=work memtype=data;
run;
Copy All SAS Data Sets or Copying an Entire Library using COPY Statement
/*Proc datasets with nowarn option and excludin the in= statement*/
proc datasets library=sashelp nowarn;
copy out=work;
run;
Move SAS datasets
To move a data set from one library to another, rather than copying it, you can use the MOVE option with either Proc DATASETS or Proc Copy.
Move a single Data Set using Proc Copy Procedure
proc copy in=work out=sasdsn memtype=data move;
select cars;
run;
Notes in the SAS log verify that the dataset is moved.
NOTE: Moving WORK.CARS to SASDSN.CARS (memtype=DATA). NOTE: There were 428 observations read from the data set WORK.CARS. NOTE: The data set SASDSN.CARS has 428 observations and 15 variables.
Move a single Data Set using the Copy Statement.
If you want to move a SAS data set from one library to another, use the Datasets Procedure and specify the dataset that you want to move in the Select 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=sasdsn memtype=data move;
select cars;
run;
quit;
Move All Data Sets using the Copy Procedure.
When you don’t specify the Select statement, SAS moves all data sets from the IN to the OUT library.
proc copy in=sashelp out=sasdsn memtype=data move;
run;
Move All Data Sets using the Copy Statement.
proc datasets nolist;
copy in=work out=MyLib memtype=data move;
run;quit;
EXCLUDE dataset from copy or move
You can also specify one or more SAS datasets to exclude from the copy operation. Note that the datasets listed in the exclude statement must be in the library specified in the IN= option in the COPY statement.
Also, you will be unable to use the SELECT and EXCLUDE statements together while copying the dataset.
The example below excludes the cars dataset while copying all the datasets from SASHELP to the WORK library.
proc copy in=sashelp out=work memtype=data;
exclude cars;
run;
In the below example, the cars dataset is excluded from moving between the libraries.
proc datasets nolist;
copy in=work out=sasdsn memtype=data move;
exclude cars;
run;
quit;
Using CLONE | NOCLONE option
The CLONE | NOCLONE option specifies whether to copy data set attributes.
proc copy in=sashelp out=work memtype=data clone;
select class;
run;
Difference between using the PROC COPY Procedure and the COPY Statement
The COPY procedure and the COPY statement in the PROC DATASETS procedure are similar in their operations. However, there are a few differences that you may want to note.
- The IN= argument is required with PROC COPY. In the COPY statement, IN= is optional.
- PROC DATASETS cannot work with libraries that allow only sequential data access.
- The COPY statement honours the NOWARN option, but PROC COPY does not.
How to create a copy of a dataset in the same library?
PROC COPY does not have an option that would allow you to make a copy of a data set in the same library. However, when the output data set is non-existent in a PROC APPEND, the procedure uses the same code base as PROC COPY.
PROC APPEND has the capability of naming an input and output data set. Using this capability, it is simple to make a copy of your data set in the same library:
proc append data=mylib.mydata
base=mylib.newdata;
run;
So, this was our side of the different options to copy or move datasets between SAS Libraries. We hope that you must have found it useful.
Download the SAS code from here.
Moreover, suggest suggestions in the comment section below if you have other suggestions. We will take those lists in our further blog post.
Thanks for reading!
If you liked this article, you might also want to read How to delete datasets in SAS? and Using Proc Contents to describe SAS datasets.
Do you have any tips to add? Let us know in the comments.
Please subscribe to our mailing list for weekly updates. You can also find us on Instagram and Facebook.