You can use the PROC COPY statement in SAS to copy a dataset from one library to another.
This statement uses the following basic syntax:
proc copy in=folder1 out=folder2 memtype=data;
select my_data;
run;
Here is what each statement does:
- IN: The library where the dataset currently exists
- OUT: The library to copy the dataset to
- MEMTYPE: Specifies that only dataset should be copied
- SELECT: The name of the dataset to copy
The following step-by-step example shows how to use PROC COPY in practice to copy a dataset from one library to another.
Step 1: Create the Dataset
First, let’s create the following dataset called my_data that contains information about various basketball players:
/*create dataset*/
data my_data;
input team $ points assists;
datalines;
Mavs 14 9
Spurs 23 10
Rockets 38 6
Suns 19 4
Kings 30 4
Blazers 19 6
Lakers 22 14
Heat 19 5
Magic 14 8
Nets 27 8
;
run;
/*view dataset*/
proc print data=my_data;
Step 2: Save the Dataset in One Library
Next, we’ll use the LIBNAME statement to specify the library where our dataset should be saved:
/*define library where dataset should be saved*/
libname folder1 '/home/u13181/folder1/';
/*save dataset to library called folder1*/
data folder1.my_data;
set my_data;
run;
If I navigate to my folders and files, I can see that my_data has indeed been saved in folder1:
Step 3: Use PROC COPY to Copy the Dataset to Another Library
Next, I can use the PROC COPY statement to copy this dataset from folder1 to folder2:
/*define library where dataset should be copied to*/
libname folder2 '/home/u13181/folder2/';
/*copy my_data to library called folder2*/
proc copy in=folder1 out=folder2 memtype=data;
select my_data;
run;
If I navigate to my folders and files once again, I can see that my_data has indeed been copied to folder2:
Note: When using PROC COPY, the dataset you are copying will still remain in the original library that it came from.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
How to Delete Datasets in SAS
How to Check if Dataset Exists in SAS
How to Concatenate Datasets in SAS