How to Copy datasets in SAS?

As a SAS programmer, you may need to move or copy data between SAS libraries. This article shows you different options to move or copy datasets in SAS.

You may be familiar with moving or copying data using the data step below.

data cars;
set sashelp.cars;
run;

Using the Data Step is the most widely used method for copying SAS datasets. Although this approach is straightforward, but not an efficient approach.

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 any data into memory.

A more efficient method to move or copy datasets between libraries is the PROC COPY and PROC DATASETS.

Copy Specific 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 there are duplicate data set names, then you do not receive a warning message before copying starts.

The MEMTYPE=DATA option tells SAS to copy only the SAS datasets.

The below example copies the cars dataset from SASHELP to the WORK library using the PROC COPY Procedure.

proc copy in=sashelp out=work memtype=data;
   select cars;
run;

You may also use the PROC Dataset procedure as below to copy all the datasets from one library to another.

You may also want to read our article on Ten Quick Uses of Proc Datasets.

proc datasets nolist;
    copy in=sashelp out=work memtype=data;
    run;
quit;

Copy All SAS Data Sets or Copying an Entire Library

To copy all SAS datasets, you would need to specify the source library name in the IN and the destination library in the OUT statement. This will copy all data sets that are in the IN library to the OUT library.

proc copy in=sashelp out=work memtype=data;
run;

/*Proc datasets with nowarn option and excludin the in= statement*/

proc datasets library=sashelp nowarn;
copy out=work;
run;

Move Specified Data Set

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

When you don’t specify the Select statement, SAS moves all data sets from the IN to the OUT library.

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 that is specified in the IN= option in the COPY statement.

Also, you will not be able to use the SELECT and EXCLUDE statements together while copying the dataset.

The below example while copying all the datasets from SASHELP to WORK library excludes the cars dataset.

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;

FAQ’s

So, this was our side of the different options you have to copy or move datasets between SAS Libraries. We really hope that you must have found it useful.

Download the SAS code from here.

Moreover, if you have any other suggestions, suggest them below the comment section. We would really 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.

Every week we'll send you SAS tips and in-depth tutorials

JOIN OUR COMMUNITY OF SAS Programmers!

Subhro Kar is an Analyst with over five years of experience. As a programmer specializing in SAS (Statistical Analysis System), Subhro also offers tutorials and guides on how to approach the coding language. His website, 9to5sas, offers students and new programmers useful easy-to-grasp resources to help them understand the fundamentals of SAS. Through this website, he shares his passion for programming while giving back to up-and-coming programmers in the field. Subhro’s mission is to offer quality tips, tricks, and lessons that give SAS beginners the skills they need to succeed.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap