Monday, April 11, 2016

Hack 3.2 Copying A SAS Data Set to the Same SAS Library

SAS Programming Professionals,

Did you know that you can make an exact copy of a SAS data set in the same library as the original using a SAS procedure?

If you are thinking that you cannot use PROC COPY to do this, then you are absolutely right!  PROC COPY does not have a facility for specifying a new name for the data set that is being copied.

The SAS procedure to use is… (drum roll, please): PROC APPEND!  The technique to use is to append the data set you want to have “copied” to a non-existent data set with the new name that you want for the copy.  For example, say that we have a SAS data set named HEART and want to create a copy of that data set named HEART_BACKUP in the same library.  We would code the following:


libname ctemp "c:\temp";

proc append base=ctemp.heart_backup
            data=ctemp.heart;
run;

That would result in the following being written to the SAS log:

2    proc append base=ctemp.heart_backup
3                data=ctemp.heart;
4    run;

NOTE: Appending CTEMP.HEART to CTEMP.HEART_BACKUP.
NOTE: BASE data set does not exist. DATA file is being copied to BASE file.
INFO: Engine's block-read method is in use.
INFO: Engine's block-write  method is in use.
NOTE: There were 5209 observations read from the data set CTEMP.HEART.
NOTE: The data set CTEMP.HEART_BACKUP has 5209 observations and 17 variables.

I highlighted the second NOTE which states that the BASE data set—HEART_BACKUP in this case—doesn’t exist.  That NOTE is confirmation that you did not overwrite an existing SAS data set, but created a copy from the original.  Now you are one of the very few SAS professionals who know the secret to using a SAS procedure to copy a SAS data set to same data library as the original!

Best of luck in all your SAS endeavors!

----MMMMIIIIKKKKEEEE

(aka Michael A. Raithel)

Excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers


I plan to post each and every one of the hacks in the book to social media on a weekly basis.  Please pass them along to colleagues who you know would benefit.

No comments:

Post a Comment