Skip to content
9to5sas
  • Index
  • Glossary
Facebook Twitter Instagram Email RSS

  • Start Here
  • Base SAS
  • Advanced SASExpand
    • SAS Macros
    • PROC SQL
  • SAS/STATSExpand
    • SAS Analytics
    • Statistics
  • SAS Programs
9to5sas

9to5sas » SAS PROGRAMS » How to Copy datasets in SAS?

How to Copy datasets in SAS?

BySubhro Posted onJanuary 9, 2022November 7, 2022 Last Updated:November 7, 2022
0 Comments

You can use high-level procedures PROC COPY and PROC DATASETS for moving and copying SAS datasets between libraries.They provide much more control over what data sets you want to move and this approach is more efficient.

How to Copy datasets in SAS?

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.

Page Contents

  • Coping SAS Data Set
  • Copying a single dataset using the PROC COPY Procedure
  • Copying a single dataset using the COPY Statement in PROC Dataset
  • Copy All SAS Data Sets or Copying an Entire Library using Proc Copy
  • Copy All SAS Data Sets or Copying an Entire Library using COPY Statement
  • Move SAS datasets
  • Move a single Data Set using Proc Copy Procedure
  • Move a single Data Set using the Copy Statement.
  • Move All Data Sets using the Copy Procedure.
  • Move All Data Sets using the Copy Statement.
  • EXCLUDE dataset from copy or move
  • Using CLONE | NOCLONE option
  • Difference between using the PROC COPY Procedure and the COPY Statement
  • How to create a copy of a dataset in the same library?

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.

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

JOIN OUR COMMUNITY OF SAS Programmers!

Check your inbox or spam folder to confirm your subscription.

Post Tags: #copy sas datasets#proc copy
Subhro

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.

Facebook Twitter Linkedin

Post navigation

Previous Previous
How to truncate Decimals in SAS?
NextContinue
Using SAS with Microsoft Sharepoint

SAS Tips in your inbox

Subscribe to 9to5sas for timely SAS tips and news delivered each month.
Learn about the latest articles, and code samples to keep your SAS skills fresh!

Your subscription is successful!

Recent Posts

  • Concatenate strings in SAS: The CAT Functions Demystified
  • 5 Techniques for Quickly Removing Leading Zeros in SAS
  • Troubleshoot Your Proc SQL Code Like a Pro with These 7 Automatic Macro Variables
  • 7 PROC SQL Options You Should Use to Debug Queries
  • How To Use The SAS Proc SQL Order By Statement?
9to5sas
  • Privacy Policy
  • Disclaimer
  • About
  • Contact
Facebook Twitter Instagram RSS Email Pinterest

Copyright © 2023 9TO5SAS, All rights reserved.

Scroll to top
  • 9to5sas Blueprint
  • About
  • About
  • Acceptable use policy
  • calculator
  • Confirm email
  • Contact
  • Contact
  • Cookie Policy
  • DISCLAIMER
  • Getting Started with SAS
  • Glossary
  • Index
  • Post #13801
  • Privacy Policy
  • Privacy policy
  • SAS Programs
  • Styles
  • Subscription confirmed
  • Terms and conditions
  • Thank You