Monday, February 13, 2017

Hack 4.12 Renaming Files and Directories from A DATA Step

SAS Programming Professionals,

Did you know that you can rename SAS data sets, SAS catalog entries, as well as OS directories and files in directory-based operating systems from inside of a SAS DATA step? 

The RENAME function lets you do exactly that!  It is great for SAS purists who do not want to use the X command or the CALL SYSTEM routine to “shell out” to the operating system to rename files and/or directories.  And, if you have PROC DATASET phobia, you can use the RENAME function to rename SAS files.

Here is a simple example:

data _null_;

rc = rename('c:\temp\test.pdf', 'c:\temp\old.pdf', 'file');

put "*****************";

put "the return code is: " rc;

put "*****************";


The example above renames “test.pdf” to “old.pdf” in a DATA step.  The nice thing about the RENAME function that differentiates it from the X command or the CALL SYSTEM routine is that you get a return code that you can query to make sure that your rename worked.

Intrigued?  Well, then read more about it in the SAS online documentation on

Best of luck in all your SAS endeavors!

(aka Michael A. Raithel)
Author of the new cult classic for computer programmers:  It Only Hurts When I Hit <ENTER>
Print edition: 
Kindle edition: 

The hack above is an excerpt from the book:  Did You Know That?  Essential Hacks for Clever SAS Programmers

No comments:

Post a Comment