Copying, Moving, and Deleting SAS Data Sets |
Copying from the Procedure Input Library |
You can use the COPY statement in the DATASETS procedure to copy all or some 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.
Before you make changes to libraries, it is important to obtain directory listings of the input and output libraries in order to visually check for duplicate data set names.
To copy files from the procedure input library (specified in the PROC DATASETS statement), use the COPY statement. Here is the syntax of the COPY statement:
COPY OUT=libref <options>; |
libref |
is the libref for the SAS data library to which you want to copy the files. You must specify an output library. |
For example, the library PRECIP contains data sets for snowfall and rainfall amounts, and the library CLIMATE contains data sets for temperature. The following program lists the contents so that they can be visually compared before any action is taken:
options pagesize=60 linesize=80 nonumber nodate; proc datasets library=precip; contents data=_all_ nods; contents data=climate._all_ nods; run;
The PROC DATASETS statement starts the procedure and specifies the procedure input library PRECIP. The first CONTENTS statement produces a directory listing of the library PRECIP. Then, the second CONTENTS statement produces a directory listing of the library CLIMATE.
The following SAS output shows the two directory listings:
Checking Directories of PRECIP and CLIMATE before Copying
The SAS System The DATASETS Procedure -----Directory----- Libref: PRECIP Engine: V8 Physical Name: external-file File Name: external-file Inode Number: 1864994 Access Permission: rwxr-xr-x Owner Name: userid File Size (bytes): 4096 File # Name Memtype Size Last Modified --------------------------------------------- 1 RAIN DATA 16384 15NOV2000:14:32:09 2 SNOW DATA 16384 15NOV2000:14:32:35
The SAS System The DATASETS Procedure -----Directory----- Libref: CLIMATE Engine: V8 Physical Name: external-file File Name: external-file Inode Number: 1864993 Access Permission: rwxr-xr-x Owner Name: userid File Size (bytes): 4096 File # Name Memtype Size Last Modified ------------------------------------------------- 1 HIGHTEMP DATA 16384 15NOV2000:14:31:17 2 LOWTEMP DATA 16384 15NOV2000:14:31:39
There are no duplicate names in the directories, so the COPY statement can be issued to achieve the desired results.
copy out=climate; run;
The following SAS log shows the messages as the data sets in the library PRECIP are copied to the library CLIMATE. There are now two copies of the data sets RAIN and SNOW: one in the PRECIP library and one in the CLIMATE library.
Messages Sent to the SAS Log during Copying
35 copy out=climate; 36 run; NOTE: Copying PRECIP.RAIN to CLIMATE.RAIN (memtype=DATA). NOTE: There were 5 observations read from the data set PRECIP.RAIN. NOTE: The data set CLIMATE.RAIN has 5 observations and 4 variables. NOTE: Copying PRECIP.SNOW to CLIMATE.SNOW (memtype=DATA). NOTE: There were 3 observations read from the data set PRECIP.SNOW. NOTE: The data set CLIMATE.SNOW has 3 observations and 4 variables.
Copying from Other Libraries |
You can copy from a library other than the procedure input library without using another PROC DATASETS statement. To do so, use the IN= option in the COPY statement to override the procedure input library. Here is the syntax for the option:
COPY OUT=libref-1 IN=libref-2; |
libref-1 |
is the libref for the SAS data library to which you want to copy files. |
libref-2 |
is the libref for the SAS data library from which you want to copy files. |
The IN= option is a useful tool when you want to copy more than one library into the output library. You can use one COPY statement for each input library without repeating the PROC DATASETS statement.
For example, the following statements copy the libraries PRECIP, STORM, CLIMATE, and USCLIM to the library WEATHER. The procedure input library is PRECIP, which was specified in the previous PROC DATASETS statement.
copy out=weather; copy in=storm out=weather; copy in=climate out=weather; copy in=usclim out=weather; run;
The following SAS log shows that the data sets from these libraries have been consolidated in the library WEATHER:
Copying Four Libraries into the Library WEATHER
54 copy out=weather; NOTE: Copying PRECIP.RAIN to WEATHER.RAIN (memtype=DATA). NOTE: There were 5 observations read from the data set PRECIP.RAIN. NOTE: The data set WEATHER.RAIN has 5 observations and 4 variables. NOTE: Copying PRECIP.SNOW to WEATHER.SNOW (memtype=DATA). NOTE: There were 3 observations read from the data set PRECIP.SNOW. NOTE: The data set WEATHER.SNOW has 3 observations and 4 variables. 55 copy in=storm out=weather; NOTE: Copying STORM.TORNADO to WEATHER.TORNADO (memtype=DATA). NOTE: There were 5 observations read from the data set STORM.TORNADO. NOTE: The data set WEATHER.TORNADO has 5 observations and 4 variables. 56 copy in=climate out=weather; NOTE: Copying CLIMATE.HIGHTEMP to WEATHER.HIGHTEMP (memtype=DATA). NOTE: There were 5 observations read from the data set CLIMATE.HIGHTEMP. NOTE: The data set WEATHER.HIGHTEMP has 5 observations and 4 variables. NOTE: Copying CLIMATE.LOWTEMP to WEATHER.LOWTEMP (memtype=DATA). NOTE: There were 5 observations read from the data set CLIMATE.LOWTEMP. NOTE: The data set WEATHER.LOWTEMP has 5 observations and 4 variables. NOTE: Copying CLIMATE.RAIN to WEATHER.RAIN (memtype=DATA). NOTE: There were 5 observations read from the data set CLIMATE.RAIN. NOTE: The data set WEATHER.RAIN has 5 observations and 4 variables. NOTE: Copying CLIMATE.SNOW to WEATHER.SNOW (memtype=DATA). NOTE: There were 3 observations read from the data set CLIMATE.SNOW. NOTE: The data set WEATHER.SNOW has 3 observations and 4 variables. 57 copy in=usclim out=weather; 58 run; NOTE: Copying USCLIM.BASETEMP to WEATHER.BASETEMP (memtype=CATALOG). NOTE: Copying USCLIM.HURRICANE to WEATHER.HURRICANE (memtype=DATA). NOTE: There were 5 observations read from the data set USCLIM.HURRICANE. NOTE: The data set WEATHER.HURRICANE has 5 observations and 5 variables. NOTE: Copying USCLIM.REPORT to WEATHER.REPORT (memtype=CATALOG). NOTE: Copying USCLIM.TEMPCHNG to WEATHER.TEMPCHNG (memtype=DATA). NOTE: There were 5 observations read from the data set USCLIM.TEMPCHNG. NOTE: The data set WEATHER.TEMPCHNG has 5 observations and 6 variables. NOTE: Copying USCLIM.USHIGH to WEATHER.USHIGH (memtype=DATA). NOTE: There were 6 observations read from the data set USCLIM.USHIGH. NOTE: The data set WEATHER.USHIGH has 6 observations and 5 variables. NOTE: Copying USCLIM.USLOW to WEATHER.USLOW (memtype=DATA). NOTE: There were 7 observations read from the data set USCLIM.USLOW. NOTE: The data set WEATHER.USLOW has 7 observations and 5 variables.
Copyright © 2012 by SAS Institute Inc., Cary, NC, USA. All rights reserved.